簡體   English   中英

無法獲取某個類別的 wordpress 帖子

[英]Could not get wordpress posts for a 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