繁体   English   中英

如何在页面上仅显示一个类别的帖子? [WordPress]

[英]How to show only posts from one category on page? [WordPress]

我使用视频教程创建了自己的 WordPress 主题。

在这个主题中,我创建了一个page-ID.php ,其中应该只显示一个类别的帖子。

我怎样才能做到这一点?

我尝试调用类别的当前代码(ID 12):

<?php 
                    
$number_of_posts = get_option('post_per_page', 10);
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$offset = ($paged - 1) * number_of_posts;
                    
$args = array(
    post_type => 'post',
    posts_per_page => '5',
    offset => $offset,
    paged => $paged,
    cat => '-12' // ID of category
);
                    
$loop2 = new WP_Query($args);
                    
if ( $loop2->have_posts() ) : while ( $loop2->have_posts() ) : $loop2->the_post();?>
                    
    <?php if (has_category()) { get_template_part('template_parts/content'); };?>
                    
<?php endwhile; else : ?>
                    
    <?php get_template_part('template_parts/content','error');?>
                    
<?php endif;?>
                            
<?php previous_posts_link('Vorherige Seite', $loop2->max_num_pages);?>
<?php next_posts_link('Nächste Seite', $loop2->max_num_pages);?>
                    
<?php wp_reset_postdata();?>

如果我像这篇文章一样使用它,会显示 ID 1 和 ID 13 等其他类别,但不会显示已定义的类别(ID 1 = 未分类;ID 12 = 最新;ID 13 = 事件)。 我的目标是相反的。

你能告诉我我做错了什么吗?

您的代码中有cat => '-12' - 即类别 ID减去12。将其更改为cat => '12'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM