我想在主页上显示最后更新的帖子顺序,如何正确使用它以及在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
add this code in the functions.php file
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
OR
add this code in 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
<?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
<?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();
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.