自定义post_type模板,包含get_template_part

时间:2015-05-11 16:59:33

标签: php html wordpress custom-post-type

因此,我的父主题使用single.php调用带有函数get_template_part()的content-single.php。

single.php的代码:

get_header(); ?>

<div class="container">
    <main id="main" class="postItem" role="main">

    <?php while ( have_posts() ) : the_post(); ?>

        <a href="<?php the_permalink(); ?>"><?php the_title(); ?>

        <?php get_template_part( 'content', 'single' ); ?>

        <?php codex_coder_content_nav( 'nav-below' ); ?>


    <?php endwhile; // end of the loop. ?>

    </main><!-- #main -->
<!--<?php get_sidebar(); ?>

我使用名为&#34; ourNews&#34;的自定义post_type。在wordpress文档之后,我创建了两个文件:single-ourNews.php和content-single-ourNews.php

有了这个,在我的&#34; single-ourNews.php&#34;我更改了以下行:

 <?php get_template_part( 'content', 'single-ourNews' ); ?>

但它不断加载文件&#34; content-single.php&#34;。我做错了什么?

其他问题是:如何在此自定义模板上放置具有相对路径的图像?我创建了一个文件夹&#34; img&#34;而我正打电话使用:      

但是说找不到图像。我读了一点,一些地方说我不能使用相对路径,但为什么不,如果主题使用?我很困惑。

2 个答案:

答案 0 :(得分:0)

Wordpress仍在加载加载content-single.php

的single.php

您的自定义帖子不是我们的新闻。这可能是标签,但不是slu .. slu is总是小写的,用 - 分隔。尝试将您的文件重命名为single-our-news.php。

此外,每次注册自定义帖子类型时,都应将固定链接选项重置为默认值,然后再重新设置为您希望的类型。

对于相对路径,您不应该使用它。你应该使用。'/ img / name-of-image.jpg

答案 1 :(得分:0)

使用content时,第一个参数定义基本模板名称(例如content.php,加载get_template_part('content', 'single-ourNews'); ),第二个参数加载后缀版本(如果可用)。因此你的电话

content-single-ourNews.php

首先查找名为content.php的文件,然后在第一个文件不可用的情况下回退到get_template_part。我不确定single-ournews函数是否将后缀参数转换为类似single-our-newssingle.php的内容,然后再将其附加到第一个参数,请务必测试其中的一些变体。 / p>

我不能100%确定该功能在子主题和父主题中的行为是否不同。一种选择是覆盖子主题中的父if ($post->post-type === 'ourNews') { get_template_part('content', 'single-ourNews'); } else { get_template_part('content'); } ,并使用

直接修改它
single-[cptslug].php

最后,在为自定义帖子类型的单个视图加载模板之前,WordPress会查找文件{{1}}。

相关问题