向php函数添加另一个变量

时间:2021-07-08 作者:plushjudy

I have added this snippet to functions.php to be able to order Slider Revolution slides:

// orderby slideshows
函数modify\\u slider\\u order($query,$slider\\u id){

// only alter the order for slider with "x" ID
// https://tinyurl.com/zb6hzpc

if($slider_id == 17 ) {

    // Custom Meta key/name
    $query[\'meta_key\'] = \'numeric_order\';

    // Order by Custom Meta values
    $query[\'orderby\'] = \'numeric_order\';

    // Calculate order based on:
    // \'NUMERIC\', \'CHAR\', \'DATE\', \'DATETIME\', \'TIME\'
    $query[\'meta_type\'] = \'NUMERIC\';

}


return $query;
}

添加过滤器(\'revslider\\u get\\u posts\',\'modify\\u slider\\u order\',10,2);

I would like to reference either all slideshows, or another (id=16). I have tried commenting out the "if($slider_id..." part, and also tried adding the other slide id separated by a comma next to the 17, but WP is kicking up errors. Thank you.

2 个回复
SO网友:plushjudy

Well I figured out I could duplicate the function and rename the title so it runs twice. But for future, is there a more elegant way of doing this in one instance? thanks!

// orderby slideshows
函数modify\\u slider\\u order($query,$slider\\u id){

// only alter the order for slider with "x" ID
// https://tinyurl.com/zb6hzpc

if($slider_id == 17 ) {

    // Custom Meta key/name
    $query[\'meta_key\'] = \'numeric_order\';

    // Order by Custom Meta values
    $query[\'orderby\'] = \'numeric_order\';

    // Calculate order based on:
    // \'NUMERIC\', \'CHAR\', \'DATE\', \'DATETIME\', \'TIME\'
    $query[\'meta_type\'] = \'NUMERIC\';

}


return $query;
}

添加过滤器(\'revslider\\u get\\u posts\',\'modify\\u slider\\u order\',10,2);

//orderby幻灯片2

函数modify\\u slider\\u order2($query,$slider\\u id){

// only alter the order for slider with "x" ID
// https://tinyurl.com/zb6hzpc

if($slider_id == 16 ) {

    // Custom Meta key/name
    $query[\'meta_key\'] = \'numeric_order\';

    // Order by Custom Meta values
    $query[\'orderby\'] = \'numeric_order\';

    // Calculate order based on:
    // \'NUMERIC\', \'CHAR\', \'DATE\', \'DATETIME\', \'TIME\'
    $query[\'meta_type\'] = \'NUMERIC\';

}


return $query;
}

添加过滤器(\'revslider\\u get\\u posts\',\'modify\\u slider\\u order2\',10,2);

SO网友:DoubleU

可能是延迟回复,但您可以通过使用逻辑运算符来实现这一点(请参见https://www.php.net/manual/en/language.operators.logical.php 有关此主题的更多信息)

因此,让我们更改if语句以添加第二个滑块

// orderby slideshows
function modify_slider_order($query, $slider_id) {

    if($slider_id == 17 || $slider_id == 16 ) {

        // Custom Meta key/name
        $query[\'meta_key\'] = \'numeric_order\';

        // Order by Custom Meta values
        $query[\'orderby\'] = \'numeric_order\';

        // Calculate order based on:
        // \'NUMERIC\', \'CHAR\', \'DATE\', \'DATETIME\', \'TIME\'
        $query[\'meta_type\'] = \'NUMERIC\';

    }

    return $query;

}
add_filter(\'revslider_get_posts\', \'modify_slider_order\', 10, 2);
看到我们在那里做了什么吗是OR的逻辑运算符。因此,如果ID等于17或16,则函数将执行。

相关推荐

将静态页面重定向到home.php的问题

我的WordPress网站主题有一些问题。在创建的某个点。主题使用的php文件出错了。基本上,如果我在wordpress back office的设置中使用帖子标题创建永久链接,我的所有静态页面似乎都会重定向到home.php. 我尝试了几种方法:重新生成永久链接,更改现有页面的段塞,创建新页面。这些都有相同的结果:访问静态页面仍然会将我重定向到home.php...但是,当我改变永久链接的类型时,例如使用简单的永久链接(url中有帖子的id),事情就会得到解决。如果我保留;职位名称“;permalink