【发布时间】:2017-07-05 22:04:50
【问题描述】:
我想在主页上显示帖子的最后更新顺序,如何为它设置正确的功能以及在 wordpress 中粘贴该功能的位置?
【问题讨论】:
我想在主页上显示帖子的最后更新顺序,如何为它设置正确的功能以及在 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');
并在页面编辑器中简单地添加这个 [latest_posts] 短代码,您已在 cms 中为首页指定
或
在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();
?>
【讨论】: