WordPress 自定义帖子类型类别

发布于 2024-10-16 20:21:47 字数 1758 浏览 0 评论 0 原文

嘿。我在 WordPress 中使用自定义帖子类型。我像这样注册这个自定义帖子类型:

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

在主题(循环模板)中,我喜欢将帖子和我的自定义帖子类型结合起来,因为我使用带有这些参数的 query_posts() :

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

到目前为止这是有效的。但是,我在类别和标签页面上遇到了问题。如果我调用首页,一切都很好,我可以对页面进行分页以获得正确的结果。

而且,如果我调用分页 URL,例如 /category/mycat/page/2,则会抛出 404。但肯定应该有帖子。无论类别中有自定义类型的帖子还是普通的帖子。我想我的 query_posts() 参数不正确,但不知道......

$wp_query->max_num_pages 似乎有错误的值。但为什么?我是否正确注册了分类法(我喜欢为自定义帖子类型使用类别和标签)?

你有什么想法要做什么吗?多谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

So尛奶瓶 2024-10-23 20:21:47

我刚刚遇到了完全相同的问题,但在任何地方都找不到解决方案!互联网上充满了有关该主题的资源,但没有一个提供该问题的正确答案。

这是任何搜索者的正确答案。将以下代码放入主题根目录的 functions.php 中。

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

所有积分均归于在 Mike自定义帖子类型分页类别存档-404" rel="nofollow">Wordpress.com。干杯!

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!

晌融 2024-10-23 20:21:47

确保将其添加

'paged' => get_query_var('paged')

到 $args 中,它应该可以接受分页。

更多此处

Make sure you add this:

'paged' => get_query_var('paged')

to your $args and it should accept the paging okay.

More here

隱形的亼 2024-10-23 20:21:47

在 archive.php 中尝试使用以下内容:

query_posts( array(
  'post_type' => 'lifestream',
  'posts_per_page' => 6,
  'orderby' => 'menu_order',
  'orderby' => 'date',
  'order' => 'ASC',
  'paged' => '' . get_query_var('paged')
));

if ( have_posts() ) : while ( have_posts() ) : the_post();

endwhile; else:
endif;

对于我使用的下一页和上一页链接:

next_posts_link('Older Entries', $wp_query->max_num_pages);
previous_posts_link('Newer Entries', $wp_query->max_num_pages);

In the archive.php try using the following:

query_posts( array(
  'post_type' => 'lifestream',
  'posts_per_page' => 6,
  'orderby' => 'menu_order',
  'orderby' => 'date',
  'order' => 'ASC',
  'paged' => '' . get_query_var('paged')
));

if ( have_posts() ) : while ( have_posts() ) : the_post();

endwhile; else:
endif;

For the next and previous page links I use:

next_posts_link('Older Entries', $wp_query->max_num_pages);
previous_posts_link('Newer Entries', $wp_query->max_num_pages);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文