[英]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种方法可以做到这一点
在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]短代码
要么
在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.