最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Category.php template for custom posts

programmeradmin1浏览0评论

I'm trying to use my category.php file to display all posts of a certain custom post type (say "Company") with a given category. However, when I try to use it by navigating to domain/category/company/category1, which is the link automatically generated by wp_list_categories(), no posts appear.

The code I'm using is:

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

What am I doing wrong?

I'm trying to use my category.php file to display all posts of a certain custom post type (say "Company") with a given category. However, when I try to use it by navigating to domain/category/company/category1, which is the link automatically generated by wp_list_categories(), no posts appear.

The code I'm using is:

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

What am I doing wrong?

Share Improve this question asked Jul 10, 2011 at 0:33 daysrunawaydaysrunaway 3013 gold badges9 silver badges20 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 3

I figured it out! For anyone having the same problem, I solved it by adding

$cat_id = get_query_var('cat'); 
query_posts("post_type=company&cat=$cat_id");

right in front of the loop. Anyone having the same problem would probably also benefit from looking at this, too.

The correct way is to use pre_get_posts for that.

You can either add to show at the search.php and other pages from template hierarchy, like so:

add_action('pre_get_posts', function($query) {
    if ( ! is_admin() && $query->is_main_query() ) {

        if ( is_archive() || is_category() ) {
            $query->set( 'post_type', 'company' );
        }

        if ( $query->is_search() ) {
            $query->set( 'post_type', array( 'company' ) );
        }
    }
});

To understand more about pre_get_posts() you should watch this talk of Andrew Nacin (one of the leader developers of WP)

Are you using the core category taxonomy, by applying it to your Custom Post Type, or have you defined a custom taxonomy?

If you're using a custom taxonomy, you need to use the template file taxonomy-{taxonomy}.php, or taxonomy-{taxonomy}-{term}.php.

But really, using category.php or taxonomy.php really may not be what you want to do. Have you tried using archive-{post-type}.php, for displaying the archive index list of posts from your Custom Post Type?

You also have an issue with this:

<?php get_template_part( 'type-company', get_post_format() ); ?>

The Post Format taxonomy only applies to the Post post-type; not to any, arbitrary post-type. Again: did you custom-apply the Post Format taxonomy to your Custom Post Type?

EDIT

Note: you may need to modify the Loop query in category.php and/or tag.php, in order to display your CPTs.

发布评论

评论列表(0)

  1. 暂无评论