简体   繁体   English

尝试仅显示来自Wordpress中一个类别的帖子

[英]Trying to display posts only from one category in wordpress

I am using the magnet theme and am trying to display posts from a single category on the homepage. 我正在使用磁贴主题,并试图在首页上显示单个类别的帖子。 I have read through several post but have had no luck using the suggested methods I found. 我已经阅读了几篇文章,但是使用我发现的建议方法并没有运气。

Here is the snippet of code from the home-page-grid.php file that appears to be adding posts to the homepage 这是home-page-grid.php文件中的代码片段,似乎在主页上添加了帖子

   <!-- Start content -->
   <div class="grid_8" id="content">



   <div class="widget_container content_page"> 
                 <div class="post_list_medium_widget">
                <div class="post_list_medium_style1">

<?php
global $paged;

if ( get_query_var('paged') ) {
     $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
     $paged = get_query_var('page');
} else {
    $paged = 1;
}
$query = new WP_Query( array ( 'paged' => $paged, 'orderby' => 'date', 'order' => 'DESC' ) );
$row_count=0;
while ( $query->have_posts() ) {
$row_count++;
$query->the_post();
$post_id = get_the_ID();            

?>

Any thoughts as to what needs to be done to get this to display a single category and all its sub categories? 关于要使它显示单个类别及其所有子类别需要做些什么?

Try adding this array item: 'cat' => '14' . 尝试添加以下数组项: 'cat' => '14' 14 is the category id 14是类别ID

$query = new WP_Query( array ( 'paged' => $paged, 'cat' => '14', 'orderby' => 'date', 'order' => 'DESC' ) );
<?php 
 $catPost = get_posts(get_cat_ID("your_category_name")); //change this with your category
   foreach ($catPost as $post) : setup_postdata($post); ?>
       <div>
             <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
             <p><?php the_content(); ?></p>
       </div>
<?php  endforeach;?>

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

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