admin管理员组文章数量:1319026
I'm trying to create a shortcode to display the top 6 recent posts. The shortcode has to be able to capture a true/false attribute of today though I don't necessarily need this to be able to activate right away. It also has to be able to display the thumbnail, post title, and excerpt. I had it going mostly fine until I added most of the attributes (minus today) and the output setup and that's when I got an error from WordPress. I'm still fairly new to PHP so what I have is a combo of quite a few tips from tutorials on writing shortcodes but I still figure I'm missing something or entered something wrong.
function torque_hello_world_shortcode( $atts, $content = null) {
// set up default parameters
$args = shortcode_atts( array(
'num' => '6',
'order' => 'DESC',
'orderby' => 'post_date',
'today' => 'true'
), $atts, );
$args['today'] = 'true' === $args['today'];
$output = '';
$posts = get_posts($args);
foreach($posts as $post) {
setup_postdata($post) ;
$output .='<li><a href="'. get_the_permalink() .'">'.get_the_title() .' '.get_the_post_thumbnail() .' '.get_the_excerpt() .'</a></li>';
}
wp_reset_postdata();
return '<ul>'. $output .'</ul>;
}
add_shortcode ( 'helloworld', 'torque_hello_world_shortcode');
本文标签: shortcode to display specific recent posts
版权声明:本文标题:shortcode to display specific recent posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742051331a2418076.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论