简体   繁体   English

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

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

我想在主页上显示最后更新的帖子顺序,如何正确使用它以及在wordpress中将该功能粘贴到何处?

Create your plugin and paste this function in plugin file. 创建您的插件并将此功能粘贴到插件文件中。

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] 

There are 3 ways to do this 有3种方法可以做到这一点

  1. add this code in the functions.php file 在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'); 

and add simply this [latest_posts] shortcode in the page editor that you have assign for the front page in cms 并在页面编辑器中添加您已为cms中的首页分配的这个[latest_posts]短代码

OR 要么

  1. add this code in functions.php 在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'); 

and add this code at the place where you want to show the post in the template that assign for or made for a home page like home.php or front-page.php 并在您要在模板中显示帖子的位置添加此代码,该模板为home.php或front-page.php等主页分配或制作

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

OR 3. simply add this code at the place where you want to show the post in the template that assign for or made for a home page like home.php or front-page.php 或者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