首页 > 解决方案 > 用于特定类别提要的 Wordpress 自定义模板

问题描述

我正在尝试为播客提要使用自定义模板。我的播客是由其类别定义的标准帖子。我想通过标准网址访问它:mysite.com/category/podcast1/feed

我可以让新模板工作,但下面的代码删除了标准类别过滤,因此我使用新模板从所有类别中获取帖子。当我尝试添加新查询并限制帖子时,它会覆盖所有查询。当我尝试使用$query->is_feedthen 限制查询时,它也不起作用。当我 var_dump $query 时,pre_get_posts 钩子运行查询两次,第一次以 is_feed 为 true,第二次为 false。它运行的最后一个查询是“showposts”。这是我的代码:

function my_custom_rss($query) {
    if ( 'podcast1' === get_query_var( 'category_name' ) ) {
        get_template_part( 'feed', 'podcast' );
    } 
    else {
        get_template_part( 'feed', 'rss2' );
    }
}
remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', 'my_custom_rss', 10, 1 );

function feed_category_query($query)
{
    if ($query->is_feed && $query->is_category() ) {
        $tax_query = array(
            'relation' => 'OR',
                array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => get_cat_ID('podcast1')
                ),
                array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => get_cat_ID('podcast2')
                ),
            );

            $query->set('post_type','post');
            $query->set('orderby','post_date');
            $query->set('order','DESC');
            $query->set('tax_query', $tax_query);
            return $query;
    } 
}
add_filter('pre_get_posts','feed_category_query');

如何将查询限制为仅提要,或恢复我认为已删除的标准查询remove_all_acions('do_feed_rss2')

我宁愿用一个自定义插件来做这一切,但现在,不管怎样都行。谢谢您的帮助。

标签: wordpressrssfeed

解决方案


我在我的新提要模板中发现了这个问题。在顶部,我有:

$posts = query_posts('showposts=' . $postCount);

这是创建覆盖我的 pre_get_posts 查询的第二个查询。


推荐阅读