简体   繁体   English

无法获取某个类别的 wordpress 帖子

[英]Could not get wordpress posts for a category

Here is the code that I am using to get posts for the category "noticeboard".这是我用来获取“告示板”类别的帖子的代码。 But I am unable to get the results even though I have one post that is categorized in that category.但是即使我有一篇归入该类别的帖子,我也无法获得结果。

<?php
//Noticeboard Posts
$posts = array();
$args = array( 'category_name' => 'noticeboard', 'nopaging'=>true, 'posts_per_page'=>5 );
$posts_query = new WP_Query( $args );
if ( $posts_query->have_posts() ) {
    while ( $posts_query->have_posts() ) {
        $posts_query->the_post();
        if(has_post_thumbnail()){
            $temp = array();
            $temp['title'] = get_the_title();
            $temp['excerpt'] = get_excerpt(80); //This is a custom function that I have made
            $temp['url'] = get_the_permalink();
            $posts[] = $temp;
        }
    }
}
wp_reset_postdata();

        //Content List
        foreach($posts as $post){ extract($post);
            ?>
            <div class="noticeboard-element">
                <h5><?php echo $title ?></h5>
                <div><?php echo $excerpt; ?><a href="<?php echo $url; ?>" class="float-right"><u>Read More</u></a></div>
                <svg height="1" width="100%">
                    <line x1="25%" y1="0" x2="75%" y2="0" style="stroke:rgb(255,255,255);stroke-width:2" />
                </svg>
            </div>
            <?php
        }
    ?>

感谢@sohrab 和我从另一个函数复制后犯的一个愚蠢的错误,我忘记删除 has_post_thumbnail() 检查。

 <?php  
  $args = array( 'numberposts' => 5, 'category_name' => 'noticeboard' );
  $posts = get_posts( $args );
  foreach( $posts as $post ): setup_postdata($post);  
 ?>  
     <div class="noticeboard-element">
        <h5><?php the_title(); ?></h5>
          <div><?php echo $excerpt; ?><a href="<?php the_permalink(); ?>" class="float-right"> Read More</a></div>
         <svg height="1" width="100%">
          <line x1="25%" y1="0" x2="75%" y2="0" style="stroke:rgb(255,255,255);stroke-width:2" />
         </svg>
     </div>
<?php endforeach; ?> 

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

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