【问题标题】:Custom single-post pulling from wrong custom post type从错误的自定义帖子类型中提取自定义单帖子
【发布时间】:2014-09-29 01:10:16
【问题描述】:

所以我正在使用两种自定义帖子类型文章和新闻设置自定义 WordPress 主题。每个人还使用自定义single-post.php 页面single-articles.phpsingle-news.php。但是,在主页上拉出文章帖子并将其永久链接插入“阅读更多”链接后,当我单击文章的一个链接时,它会将我带到他们的永久链接 URL,但不是显示文章帖子,而是显示新闻帖子内容. single-articles.phpsingle-news.php 都在主题文件夹中,而不是在子文件夹中。

这是我如何设置自定义帖子的示例...

add_action( 'init', 'articles_custom_postypes' );
function articles_custom_postypes() {
$args = array (
'label' => $labels,
'public' => true,
'menu_position' => 100,
'menu_icon' => 'dashicons-format-aside',
'label' => 'Articles',
'has_archive' => false,
'supports' => array( 'title','editor', 'thumbnail'),

);

register_post_type( 'articles', $args);

}

在自定义单个帖子页面上,我只是调用the_title()the_content 等。我应该使用while 循环还是什么?任何建议都会很棒。谢谢!

【问题讨论】:

    标签: wordpress wordpress-theming custom-post-type


    【解决方案1】:

    为此,您的主题文件夹中必须有两个文件。一个是single-articles.php,另一个是content-articles.php。如果创建了文件,请将以下代码添加到 single-articles.php

    <?php
        while ( have_posts() ) : the_post();
            get_template_part( 'content-articles', get_post_format() );
        endwhile;
    ?>
    

    还将以下内容添加到content-articles.php

    <?php
        the_title();
        the_content();
    ?>
    

    【讨论】:

    • 这是我的首页,显示前三篇文章。但是,如果有人单击该文章的链接并转到 single-articles.php 页面,则该页面将显示新闻帖子而不是文章帖子。上面的代码不会只是拉出并列出我发布的最后十篇文章,而不是实际查询我刚刚单击的文章的链接并将其显示在我的自定义单篇文章模板页面上吗?
    • 嗨雅各布,我编辑了我的答案。请浏览一下。它会解决你的问题。
    • 这成功了!谢谢!任何线索为什么这有效?如果有帖子,我会将模板的一部分拉入页面,但与直接将 the_auto、the_content 等调用到单篇文章页面有何不同?它与 while have_posts 循环有关吗?为什么我不将其包含在 single-articles.php 和 single-news.php 的顶部,而不是将其放在单独的模板文件中并包含在每个页面中?对不起,我学会了最好的提问。再次感谢!
    • single-{post type}.php是一个post类型的页面,content-{post type}.php是显示相应内容的页面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多