dongxia19772008 2011-02-09 22:53
浏览 63
已采纳

Wordpress自定义帖子类型类别

Hey. I am using a custom post type in wordpress. I register this custom post type like this:

        register_post_type("lifestream", array(
            'label' => 'Lifestream',
            'public' => true,
            'hierarchical' => true,
            'menu_position' => 5,
            'supports' => array('title','editor','author','thumbnail','comments','custom-fields'),
            'taxonomies' => array('category','post_tag'),
            'query_var' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'caller_get_posts' => 1

        ));
        register_taxonomy_for_object_type('category', 'lifestream');
        register_taxonomy_for_object_type('post_tag', 'lifestream');

In the theme (the loop template) I like to combine posts and my custom post type, for that I am using query_posts() with these parameters:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => array('post', 'lifestream'),
    'paged' => $paged,
    'cat' => $wp_query->get('cat'),
    'tag' => $wp_query->get('tag'),
    'year' => $wp_query->get('year'),
    'monthnum' => $wp_query->get('monthnum'),
    'post_status' => 'publish',
    'showposts' => 3
);
query_posts($args); 


# the loop     
while ( have_posts() ) : the_post(); 
# markup
endwhile;


if($wp_query->max_num_pages > 1): 
# next_posts_link / previous_posts_link
endif;

wp_reset_query();   

This is working so far. But, I got problems with the category and tags pages. If I call the frontpage everything is fine and I can paginate through the pages getting the correct results.

And, if I call a paged URL, e.g. /category/mycat/page/2 a 404 is thrown. But there definitly should be posts. No matter if there are custom type posts or normale posts in the category. I suppose that my parameters for query_posts() aren´t correct, but don´t know ...

It seems that $wp_query->max_num_pages has the wrong value. But why? Do I register the taxonomies (I like to use categories and tags for my custom post types) correctly?

Do you have any Idea what to do? Thanks a lot!

  • 写回答

3条回答 默认 最新

  • dongyi9783 2013-08-13 16:58
    关注

    I have just encountered the exact same problem and couldn't find the solution anywhere! The internets are full of resources about the topic but none provided the correct answer to the issue.

    Here's the correct answer for anyone searching. Put the below code in functions.php in your theme's root directory.

    function init_category($request) {
        $vars = $request->query_vars;
        if (is_category() && !is_category('Blog') && !array_key_exists('post_type', $vars)) :
            $vars = array_merge(
                $vars,
                array('post_type' => 'any')
            );
            $request->query_vars = $vars;
        endif;
        return $request;
    }
    add_filter('pre_get_posts', 'init_category');
    

    All credits go to Mike who posted this on Wordpress.com. Cheers!

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

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答