簡體   English   中英

如何在Wordpress主頁上顯示帖子,按上次更新排序

[英]How to display Posts on Wordpress home page, Order by last updated

我想在主頁上顯示最后更新的帖子順序,如何正確使用它以及在wordpress中將該功能粘貼到何處?

創建您的插件並將此功能粘貼到插件文件中。

function wpb_lastupdated_posts() 
{ 
    // Query Arguments
    $lastupdated_args = array(
    'orderby' => 'modified',
    'ignore_sticky_posts' => '1'
    );
    //Loop to display 5 recently updated posts
    $lastupdated_loop = new WP_Query( $lastupdated_args );
    $counter = 1;
    $string .= '<ul>';
    while( $lastupdated_loop->have_posts() && $counter < 5 ) : $lastupdated_loop->the_post();
    $string .= '<li><a href="' . get_permalink( $lastupdated_loop->post->ID ) . '"> ' .get_the_title( $lastupdated_loop->post->ID ) . '</a> ( '. get_the_modified_date() .') </li>';
    $counter++;
    endwhile; 
    $string .= '</ul>';
    return $string;
    wp_reset_postdata(); 
} 
//add a shortcode
add_shortcode('lastupdated-posts', 'wpb_lastupdated_posts');

use this shortcode : [lastupdated-posts] 

有3種方法可以做到這一點

  1. 在functions.php文件中添加此代碼

     function shortcode_latest_homepage_posts(){ $lquery = new WP_Query(array('order' => 'DESC')); if($lquery->have_posts()){ while($lquery->have_posts()){ $lquery->the_post(); the_title(); the_content(); } } wp_reset_postdata(); } add_shortcode('latest_posts', 'shortcode_latest_homepage_posts'); 

並在頁面編輯器中添加您已為cms中的首頁分配的這個[latest_posts]短代碼

要么

  1. 在functions.php中添加此代碼

     function latest_homepage_posts(){ $lquery = new WP_Query(array('order' => 'DESC')); if($lquery->have_posts()){ while($lquery->have_posts()){ $lquery->the_post(); the_title(); the_content(); } } wp_reset_postdata(); } add_action('latest_post', 'latest_homepage_posts'); 

並在您要在模板中顯示帖子的位置添加此代碼,該模板為home.php或front-page.php等主頁分配或制作

 <?php do_action('latest_post');?>

或者3.只需在您要在模板中顯示帖子的地方添加此代碼,該模板為home.php或front-page.php等主頁分配或制作

<?php 


    $lquery = new WP_Query(array('order' => 'DESC'));

    if($lquery->have_posts()){
       while($lquery->have_posts()){
          $lquery->the_post();
          the_title();
          the_content();
      }

  } 

  wp_reset_postdata();
  ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM