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