我的快捷码没有调用该函数

时间:2018-07-25 作者:Ben Turner

我想用两个参数做一个短代码$number_posts$category_name.

短代码正在调用另一个名为bg_make_post_grid 将这些参数用作变量。

我补充道wp_die() 查看函数是否被调用,它不会杀死WordPress,因此我认为函数没有被调用。

我做错了什么?

// Add Shortcode
add_shortcode( \'bg_recent_post_grid\', \'bg_recent_post_grid_shortcode\' );
function bg_recent_post_grid_shortcode( $atts ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            \'category_name\' => \'uncategorized\',
            \'number_posts\' => \'1\',
        ), $atts );

    return bg_make_post_grid ( $atts[\'number_posts\'], $atts[\'category_name\'] );

}


function bg_make_post_grid ( $number_posts, $category_name ) {

    $args = array(
        \'numberposts\' => $number_posts,
        \'category\' => $category_name,
        \'orderby\' => \'post_date\',
        \'order\' => \'DESC\',
        \'post_type\' => \'post\',
    );

    $recent_posts = wp_get_recent_posts( $args );

    wp_die();

}

1 个回复
SO网友:Trilok

请用下面的代码替换旧代码,它将显示它正在工作

add_shortcode( \'bg_recent_post_grid\', \'bg_recent_post_grid_shortcode\' );
function bg_recent_post_grid_shortcode( $atts ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            \'category_name\' => \'uncategorized\',
            \'number_posts\' => \'1\',
        ), $atts );

    return bg_make_post_grid ( $atts[\'number_posts\'], $atts[\'category_name\'] );

}


function bg_make_post_grid ( $number_posts, $category_name ) {

    $args = array(
        \'numberposts\' => $number_posts,
        \'category\' => $category_name,
        \'orderby\' => \'post_date\',
        \'order\' => \'DESC\',
        \'post_type\' => \'post\',
    );

    $recent_posts = wp_get_recent_posts( $args );
    print_r( $recent_posts); die;

} 
 echo do_shortcode(\'[bg_recent_post_grid]\');

结束

相关推荐

Shortcode with foreach

我不能对foreach使用shortcode。它给了我语法错误。我错在哪里?function create_galeri_shortcode($atts) { $atts = shortcode_atts( array( ), $atts, \'galeri\' ); if ( has_post_format( \'gallery\' )) { $images = get_post_