WordPress自定义帖子类型-类别不起作用

时间:2018-08-03 08:44:17

标签: php wordpress custom-post-type

我通过将以下代码添加到我的functions.php主题文件中,添加了一个自定义帖子类型“博客”:

function create_posttype() {
  register_post_type( 'blog',
    array(
      'labels' => array(
        'name' => __( 'Blog' ),
        'singular_name' => __( 'Blog' )
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'blog'),
      'taxonomies'  => array( 'category' ),
    )
  );
}
add_action( 'init', 'create_posttype' );

此方法工作正常,当查看www.mysite.co.za/blog/时,此页面显示我的所有帖子。但是,当我查看诸如www.mysite.co.za/category/blog/tips/之类的类别页面时,它不会显示任何帖子。尽管在边栏中显示的类别名称旁边有(1),并且帖子底部的标题为“此条目于2018年8月3日发布在Tips下”。

这是category.php模板文件:

<?php get_header(); ?>

    <div id="wrap">
        <div id="content" class="column">
<hr class="split style-two">

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
  <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div class="post-header">
        <h1><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
        <p class="subhead"> Posted by <?php the_author_posts_link(); ?> on <?php the_time( 'd M Y' ); ?> under <?php the_category(', ') ?>
    <!--<div class="date"><?php the_time( 'M j y' ); ?></div>
        <div class="author"><?php the_author(); ?></div>-->
    </div><!--end post header-->
    <div class="entry clear">
<div class="image-frame">   
<a href="<?php the_permalink(); ?>"><?php if ( function_exists( 'add_theme_support' ) ) the_post_thumbnail(); ?></a>
</div>
        <?php the_content('<br><span class="moretext">Read More</span>'); ?>
        <?php wp_link_pages(); ?>
    </div><!--end entry-->
    <div class="post-footer">
        <div class="comments"><?php comments_popup_link( 'Leave a Comment', '1 Comment', '% Comments', 'commentbutton' ); ?></div>
    </div><!--end post footer-->
  </div><!--end post--><br><hr class="split style-two">
<?php endwhile; /* rewind or continue if all posts have been fetched */ ?>
  <div class="navigation index">
    <div class="alignleft"><?php next_posts_link( 'Older Entries' ); ?></div>
    <div class="alignright"><?php previous_posts_link( 'Newer Entries' ); ?></div>
  </div><!--end navigation-->

<?php else : ?>
<?php endif; ?>

        </div>

        <div id="sidebar" class="column">
            <?php get_sidebar(); ?>
        </div>
    </div>

</div>

<?php get_footer(); ?>

是什么导致类别页面不显示任何帖子?

预先感谢
威廉

2 个答案:

答案 0 :(得分:0)

尝试将自定义帖子类型注册到主要查询中,看看是否可行

// Show posts of 'post', 'page' and 'blog' post types
function add_custom_post_types_to_query( $query ) {
    $query->set( 'post_type', array( 'post', 'page', 'blog' ) );
  return $query;
}
add_action( 'pre_get_posts', 'add_custom_post_types_to_query' );

上面的代码放在functions.php文件中

答案 1 :(得分:0)

您现在在新的CPT中共享WordPress默认帖子的类别,默认情况下,“ category.php”将显示“帖子”而不是您的CPT。如果您不再使用WordPress默认帖子,则可以将以下内容添加到主题的“ functions.php”中。

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
    if((is_category() || is_tag()) && !is_admin()) {
        $query->set('post_type','blog');
        return $query;
    }
}

但是我建议您使用register_taxonomy()为您的CPT创建自定义分类法。您可以在此处了解更多信息-point 5b of cppreference's explanation on dynamic_cast

相关问题