如何将定制的短码提取变量(分类)传递给WordPress RoyalSlider的查询函数?

时间:2015-09-24 作者:Ryan Dorn

有问题的插件:http://dimsemenov.com/plugins/royal-slider/wordpress/

背景:这是一个很棒的插件,它有一个“post”版本,滑块可以从帖子中提取内容。然而,它所拉的帖子的分类必须在WordPress中的滑块设置级别进行设置,不能通过短代码(我们知道)来完成。插件作者确实提供了一种覆盖此处默认查询的方法:http://help.dimsemenov.com/kb/wordpress-royalslider-advanced/wp-modifying-order-of-posts-in-slider

目标:创建一个自定义短代码,通过它我们可以传递自定义属性,特别是分类法,post类型。

短代码的示例:[rscustom rsid=“2”rstax=“home”rspt=“carousel slide”]

我们的问题:可能是一个愚蠢的问题,但我无法将shortcode变量“传递”到查询函数中,我的代码:

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

    // Extract variables from shortcode attributes: post type, id, taxonomy
    $a = shortcode_atts( array(
        \'rspt\' => \'\',
        \'rsid\' => \'\',
        \'rstax\' => \'\',
    ), $atts );

    $rspt = trim($a[\'rspt\']);
    $rsid = trim($a[\'rsid\']);
    $rstax = trim($a[\'rstax\']);

    // Execute the Royal Slider Query Override
    add_filter(\'new_royalslider_posts_slider_query_args\', \'newrs_custom_query\', 10, $rsid);

    function newrs_custom_query($args) {

        $args = array( 
            \'post_type\' =>  \'carousel-slide\',
            \'orderby\' => array(
                \'menu_order\' => \'ASC\'
            ),
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'carousel-slide-category\',
                    \'field\'    => \'slug\',
                    \'terms\'    => "$rstax", // THIS VARIABLE ISN\'T PASSING INTO THE FUNCTION
                ),
            ),
        ); 
        return $args;
    };

    return get_new_royalslider($rsid);
}
add_shortcode( \'rscustom\', \'rscustom_shortcode\' );
我的队伍。。。

\'条款\'=>“$rstax”

。。。似乎是我挂断电话的地方。当我把这句话作为。。。

\'术语\'=>\'主页\'

。。。一切都很好。我已经研究了全局变量,但只是设法让自己更加困惑。

我们将非常感谢您的任何帮助。提前感谢!

2 个回复
SO网友:TheDeadMedic

两个字-variable scope. 使用closure 取而代之并通过$rstax 通过use:

add_filter(
    \'new_royalslider_posts_slider_query_args\',
    function ( $args ) use ( $rstax ) {
        return array( 
            \'post_type\' =>  \'carousel-slide\',
            \'orderby\' => array(
                \'menu_order\' => \'ASC\'
            ),
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'carousel-slide-category\',
                    \'field\'    => \'slug\',
                    \'terms\'    => $rstax,
                ),
            ),
        )
    }
);

SO网友:Ryan Dorn

谢谢你,TheDeadMedic。传奇

快速运行:

1) 为了让下面的代码正常工作,我创建了一个自定义帖子类型“carousel slide”,它还具有一个自定义分类“carousel slide category”

2) 在RoyalSlider滑块管理中,我创建了一个“Posts滑块”(管理菜单>RoyalSlider>编辑滑块>创建新滑块)。在这种情况下,滑块的ID为2。

3) 在主题函数中添加了下面的代码。php文件

4) 通过快捷码执行滑块

工作代码:

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

    // Extract variables from shortcode attributes: post type, id, taxonomy
    $a = shortcode_atts( array(
        // \'rspt\' => \'\', Dont\' need now, but in case need to use other CPTs in future
        \'rsid\' => \'\',
        \'rstax\' => \'\',
    ), $atts );

    $rspt = trim($a[\'rspt\']);
    $rsid = trim($a[\'rsid\']);
    $rstax = trim($a[\'rstax\']);

    // Add Query Argument Filter
    add_filter( \'new_royalslider_posts_slider_query_args\',
        function ( $args ) use ( $rstax, $rspt ) {
            return array( 
                \'post_type\' =>  \'carousel-slide\', // $rspt 
                \'orderby\' => array(
                    \'menu_order\' => \'ASC\'
                ),
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'carousel-slide-category\',
                        \'field\' => \'slug\',
                        \'terms\' => $rstax,
                    ),
                ),
            );
        } 
    );

    // Execute the Royal Slider Query Override
    function newrs_custom_query($args) {
        return array( 
            \'post_type\' =>  \'carousel-slide\',
            \'orderby\' => array(
                \'menu_order\' => \'ASC\'
            ),
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'carousel-slide-category\',
                    \'field\' => \'slug\',
                    \'terms\' => $rstax,
                ),
            ),
        ); 
    };

    return get_new_royalslider($rsid);
}
add_shortcode( \'rscustom\', \'rscustom_shortcode\' );

相关推荐

redirect if shortcode exists

WordPress初学者。我试图检查用户请求的页面中是否存在短代码,如果存在,则在用户未登录时重定向。function redirect_to_home() { if (has_shortcode(get_the_content(), \'shortcode\')) { if(!is_admin() && !is_user_logged_in()) { //redirect exit(); }