doupuzhimuhan9216 2018-08-03 08:44
浏览 64
已采纳

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

I have have added a custom post type called "Blog" by adding the following code to my functions.php theme file:

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' );

This has works fine, when viewing www.mysite.co.za/blog/, this page shows all of my posts. However, when I view a category page such as www.mysite.co.za/category/blog/tips/ it displays no posts. This despite having (1) next to the category name displayed in the sidebar and having "This entry was posted on 03 August 2018 under Tips" at the bottom of the post.

Here is the category.php template file:

<?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(); ?>

What is causing the category page to not display any posts?

Thanks in advance
Willem

  • 写回答

2条回答 默认 最新

  • dow98764 2018-08-03 09:23
    关注

    You are now sharing WordPress default Post's categories in your new CPT and 'category.php' in default will show 'Posts' and not your CPT. If you are not going to use WordPress default Posts anymore, you can add the following to your theme's '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;
        }
    }
    

    But I would recommend you to create a custom taxonomy for your CPT using register_taxonomy(). You can read more here - https://www.smashingmagazine.com/2012/01/create-custom-taxonomies-wordpress/.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line