最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Shortcode atts for WP Query args

programmeradmin3浏览0评论

I need to create a shortcode for display CPT posts. I want to add an atts that print the number of posts_per_page I need in the args for Wp_query.

But If I use

[short_events number=5]

it prints only one post. Where I'm Wrong?

function dis_short_events($atts, $content = null){

    ob_start();
    $numero = extract(shortcode_atts(array(
        'number' => '-1',
     ), $atts));

    $args =array(
        'post_type'=>'eventi',
        'posts_per_page' => $numero

    );
}

add_shortcode('short_events', 'dis_short_events');

I need to create a shortcode for display CPT posts. I want to add an atts that print the number of posts_per_page I need in the args for Wp_query.

But If I use

[short_events number=5]

it prints only one post. Where I'm Wrong?

function dis_short_events($atts, $content = null){

    ob_start();
    $numero = extract(shortcode_atts(array(
        'number' => '-1',
     ), $atts));

    $args =array(
        'post_type'=>'eventi',
        'posts_per_page' => $numero

    );
}

add_shortcode('short_events', 'dis_short_events');
Share Improve this question edited Mar 24, 2020 at 10:29 WordPress Speed 2,2833 gold badges19 silver badges34 bronze badges asked Mar 23, 2020 at 23:11 Micki BenciMicki Benci 212 bronze badges 1
  • 3 The shortcode parameters are contained in the first parameter passed to the shortcode function ($atts), so you could use $atts['number'] to get the "number" parameter. But you extracted the parameters (see extract()), so you could also use $number. So you don't use $numero, but $number. – Sally CJ Commented Mar 24, 2020 at 0:53
Add a comment  | 

1 Answer 1

Reset to default 0

It's typically best practice to not extract your shortcode atts.

function dis_short_events($atts, $content = null){

ob_start();
$numero = shortcode_atts(array(
    'number' => '-1',
 ), $atts);

$args =array(
    'post_type'=>'eventi',
    'posts_per_page' => $numero['number']

);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论