GET_THEME_MOD始终返回默认值

时间:2015-05-17 作者:MisterBla

我正在一个客户的网站上工作,我正在尝试向定制者添加社交媒体链接,然后可以通过get_theme_mod, 通过短代码。然而,当我试图给他们打电话时,我总是得到默认值。

当我检查时get_theme_mods, 他们似乎根本没有定义。。。然而,在主题定制器中,字段是存在的。

我试着使用以下问题,但这对我没有帮助:
get_theme_mod(); returns nothing
https://wordpress.stackexchange.com/questions/144544/get-theme-mod-return-a-blank-value-instead-of-saved-value

添加的自定义程序代码为

// Add social links setting
$wp_customize->add_section( \'ta_pluton_social_links\', array(
    \'title\'       => __( \'Social Media Links\', \'ta_pluton\' ),
    \'description\' => __( \'Links for the Social Media buttons on the home page.\', \'ta_pluton\' ),
    \'priority\'    => 10
) );

$social_links_default_settings = array(
        \'default\' => \'#\',
);

$wp_customize->add_setting(\'ta_pluton_social_link[facebook]\', $social_links_default_settings);
$wp_customize->add_setting(\'ta_pluton_social_link[twitter]\', $social_links_default_settings);
$wp_customize->add_setting(\'ta_pluton_social_link[googleplus]\', $social_links_default_settings);
$wp_customize->add_setting(\'ta_pluton_social_link[tumblr]\', $social_links_default_settings);
$wp_customize->add_setting(\'ta_pluton_social_link[youtube]\', $social_links_default_settings);

$wp_customize->add_control(\'ta_pluton_social_link[facebook]\', array(
    \'label\'      => __(\'Facebook URL\', \'ta_pluton\'),
    \'section\'    => \'ta_pluton_social_links\',
    \'settings\'   => \'ta_pluton_social_link[facebook]\',
));

$wp_customize->add_control(\'ta_pluton_social_link[twitter]\', array(
    \'label\'      => __(\'Twitter URL\', \'ta_pluton\'),
    \'section\'    => \'ta_pluton_social_links\',
    \'settings\'   => \'ta_pluton_social_link[twitter]\',
));

$wp_customize->add_control(\'ta_pluton_social_link[googleplus]\', array(
    \'label\'      => __(\'Google+ URL\', \'ta_pluton\'),
    \'section\'    => \'ta_pluton_social_links\',
    \'settings\'   => \'ta_pluton_social_link[googleplus]\',
));

$wp_customize->add_control(\'ta_pluton_social_link[tumblr]\', array(
    \'label\'      => __(\'Tumblr URL\', \'ta_pluton\'),
    \'section\'    => \'ta_pluton_social_links\',
    \'settings\'   => \'ta_pluton_social_link[tumblr]\',
));

$wp_customize->add_control(\'ta_pluton_social_link[youtube]\', array(
    \'label\'      => __(\'YouTube URL\', \'ta_pluton\'),
    \'section\'    => \'ta_pluton_social_links\',
    \'settings\'   => \'ta_pluton_social_link[youtube]\',
));
这是shortcode函数:

/**
 * Add a social media link shortcode
 */
if( !function_exists( \'ta_pluton_social_media_shortcode\' ) ) {
    function ta_pluton_social_media_shortcode( $atts ) {
        $attributes = shortcode_atts( array(
            \'default\' => \'#\'
        ), $atts );

        if ( !array_key_exists( \'media\' ) ) {
            trigger_error(\'The social_media_link shortcode requires a medium to be selector, eg: [social_media_link media="facebook"]\' );
        }

        return get_theme_mod( \'ta_pluton_social_link[\' . strtolower( $attributes[\'media\'] ) . \']\', $attributes[\'default\'] ); 
    }
    add_shortcode( \'social_media_link\', \'ta_pluton_social_media_shortcode\' );
}

1 个回复
最合适的回答,由SO网友:Nilambar Sharma 整理而成

在自定义程序代码中,需要执行以下更改才能正常工作。你不需要ta_pluton_social_link[facebook] 就像控制中的数组一样。检查以下示例,并相应地修改其他字段。

  $wp_customize->add_control(\'facebook\', array(
      \'label\'      => __(\'Facebook URL\', \'ta_pluton\'),
      \'section\'    => \'ta_pluton_social_links\',
      \'settings\'   => \'ta_pluton_social_link[facebook]\',
  ));
获取您可以使用的URLget_theme_mod. 检查下面的示例。下面的代码片段演示如何获取Facebook URL。

$ta_pluton_social_link = get_theme_mod( \'ta_pluton_social_link\' );
$facebook_url = $ta_pluton_social_link[\'facebook\'];

结束

相关推荐