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

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 01: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥20 机械振动学课后习题求解答
  • ¥15 IEC61850 客户端和服务端的通讯机制
  • ¥15 MAX98357a(关键词-播放音频)
  • ¥15 Linux误删文件,请求帮助
  • ¥15 IBMP550小型机使用串口登录操作系统
  • ¥15 关于#python#的问题:现已知七自由度机器人的DH参数,利用DH参数求解机器人的逆运动学解目前使用的PSO算法
  • ¥15 发那科机器人与设备通讯配置
  • ¥15 Linux环境下openssl报错
  • ¥15 我在使用VS编译并执行之后,但是exe程序会报“无法定位程序输入点_kmpc_end_masked于动态链接库exe上“,请问这个问题有什么解决办法吗
  • ¥15 el-select光标位置问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部