获取旋转滑块短代码

时间:2016-03-16 15:04:47

标签: php wordpress advanced-custom-fields

我正在尝试新的东西。我有一个Wordpress自定义主题。我有高级自定义字段所有设置。我在带有“选项”的“选择”字段上使用高级自定义字段的load_field函数。我想要做的基本上是将旋转滑块短代码显示为此字段的选项......这是我在functions.php文件中的代码。任何帮助将非常感谢! :)

<?php

function my_acf_load_field( $field ) {

    $field['choices'] = array(
        <-- WANT REVOLUTION SLIDER SHORTCODES HERE -->
    );

    return $field;

}


// all
// add_filter('acf/load_field', 'my_acf_load_field');

// type
add_filter('acf/load_field/type=select', 'my_acf_load_field');

// name
// add_filter('acf/load_field/name=my_select', 'my_acf_load_field');

// key
// add_filter('acf/load_field/key=field_508a263b40457', 'my_acf_load_field');

?>

3 个答案:

答案 0 :(得分:2)

无论出于何种原因,来自Andrew M的答案的我的$ sliders变量没有返回任何内容,所以我想出了基于Themepunch's documentation的此解决方案,用于随机显示任何滑块,this article用于检查如果该类首先存在以避免错误。

<强>的functions.php

function my_acf_load_field( $field ) {
    if ( class_exists( 'RevSlider' ) ) {
        $rev_slider = new RevSlider();
        $slider_aliases = $rev_slider->getAllSliderAliases();
        if(count($slider_aliases) > 0) {
            foreach($slider_aliases as $slider_alias)
            {
                $field['choices'][$slider_alias] = $slider_alias;
            }
        } else {
            $field['choices'] = array( 'none' => 'No sliders exist. You must create one first.' );
        }
    } else {
        $field['choices'] = array( 'none' => 'Slider Revolution plugin was not found.' );
    }
    return $field;
}
//Only place these options on a specific select field with the alias "rev_slider"
add_filter('acf/load_field/name=rev_slider', 'my_acf_load_field');

然后在模板或您正在使用自定义字段的任何页面上,我将实际的短代码放在那里。

<强> page.php文件

$slider_alias = get_field('rev_slider');
if(!empty($slider_alias)) {
    echo do_shortcode(sprintf('[rev_slider alias="%s"]', $slider_alias));
}

答案 1 :(得分:1)

您可以做的是以下内容。 Revolution滑块项存储在名为wp_revslider_sliders的表中(wp_部分可能会根据您设置数据库的方式而改变 - 首先检查表名)

您可以使用Wordpress $ wpdb 全局查询此表,并返回别名字段 - 用作短代码。所以在你的加载字段函数的主体中,你可以尝试这样的东西

function my_acf_load_field( $field ) {
    global $wpdb;
    $query = sprintf('select r.id, r.alias from %srevslider_sliders r',$wpdb->prefix);
    $sliders = $wpdb->get_results($query,OBJECT);

    foreach($sliders as $slider)
    {
        //This just formats the string with the correct short code
        $field['choices'][$slider->alias] = sprintf('[rev_slider alias="%s"]',$slider->alias);
    }

    return $field;
}

这应该使用正确的选项填充您的下拉列表 - 或者至少让您走上正确的道路

答案 2 :(得分:0)

从Slider Revolution V6开始,方法“ getAllSliderAliases”不再可用。 新代码是:

functions.php

function my_acf_load_field($field)
{
    if (class_exists('RevSlider')) {
        $rev_slider = new RevSlider();
        $slider_aliases = $rev_slider->get_sliders();
        if (count($slider_aliases) > 0) {
            foreach ($slider_aliases as $slider_alias) {
                $field['choices'][$slider_alias->alias] = $slider_alias->alias;
            }
        } else {
            $field['choices'] = array('none' => 'No sliders exist. You must create one first.');
        }
    } else {
        $field['choices'] = array('none' => 'Slider Revolution plugin was not found.');
    }
    return $field;
}
//Only place these options on a specific select field with the alias "rev_slider"
add_filter('acf/load_field/name=rev_slider', 'my_acf_load_field');

page.php

$slider_alias = get_field('rev_slider');
if(!empty($slider_alias)) {
    echo do_shortcode(sprintf('[rev_slider alias="%s"]', $slider_alias));
}