根据wordpress中的帖子类别显示上次修改的帖子

时间:2018-01-12 00:40:39

标签: php wordpress

正如标题所说,我对基于当前帖子类别的最后修改帖子的输出有疑问。我找到了基于最后修改的帖子或类别的解决方案,但不是两者都在一起。

由于我是更多具有HTML和CSS知识的Illustrator / Designer,我不知道如何将它们整合到php中。对于那些熟悉编码的人,我敢肯定,这是一个几秒钟的问题。我现在拥有的是:

function wpb_lastupdated_posts() { 

// Query Arguments

$lastupdated_args = array(
'orderby' => 'modified',
'ignore_sticky_posts' => '1',
'showposts' => 2
);

//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('d.m,') . get_the_modified_time() .' ) </li>';
$counter++;
endwhile; 
$string .= '</ul>';
return $string;
wp_reset_postdata(); 
} 

//add a shortcode
add_shortcode('lastupdated-posts', 'wpb_lastupdated_posts');

我在侧边栏中添加了短代码,它按预期发布了我最后修改过的帖子,但遗憾的是它缺少了基于类别的内容,后面的内容是php。

可以请有人帮我这个吗? 感谢

1 个答案:

答案 0 :(得分:0)

更新短代码以接受类别ID的参数。

在您的wp查询参数上,使用该ID。

function wpb_lastupdated_posts( $atts ) { 

// Query Arguments

$lastupdated_args = array(
 'orderby' => 'modified',
 'ignore_sticky_posts' => '1',
 'showposts' => 2,
 'cat' => $atts['catid']
);

调用短代码时,传递当前类别ID。

[lastupdated-posts catid="current_cat_id_here"]