带有多个控件/设置的自定义注册(_R)-如何获取值?

时间:2015-05-01 作者:vm7488

这里是PHP新手,我的函数中有这个。php

function mcs_social($wp_customize){

    $wp_customize->add_section(\'mcs_social_handle\', array(
        \'title\'    => __(\'Social Network Handles\', \'mcs\'),
        \'description\' => \'i.e., Acme Company\\\'s Facebook is https://facebook.com/acmecompany then enter "acmecompany"\',
        \'priority\' => 70,
    ));

    //  =============================
    //  = Facebook                  =
    //  =============================
    $wp_customize->add_setting(\'mcs_fb_op\', array(
        \'default\'        => \'\',
        \'capability\'     => \'edit_theme_options\',
        \'type\'           => \'option\',

    ));

    $wp_customize->add_control(\'mcs_fb\', array(
        \'label\'      => __(\'Facebook Handle\', \'mcs\'),
        \'section\'    => \'mcs_social_handle\',
        \'settings\'   => \'mcs_fb_op\',
    ));

        //  =============================
    //  = Twitter                  =
    //  =============================
    $wp_customize->add_setting(\'mcs_tw_op\', array(
        \'default\'        => \'\',
        \'capability\'     => \'edit_theme_options\',
        \'type\'           => \'option\',

    ));

    $wp_customize->add_control(\'mcs_tw\', array(
        \'label\'      => __(\'Twitter Handle\', \'mcs\'),
        \'section\'    => \'mcs_social_handle\',
        \'settings\'   => \'mcs_tw_op\',
    ));

    //  =============================
    //  = RSS                       =
    //  =============================
     $wp_customize->add_setting(\'mcs_rs_op\', array(
        \'default\'        => \'rss2_url\',
        \'capability\'     => \'edit_theme_options\',
        \'type\'           => \'option\',

    ));
    $wp_customize->add_control( \'mcs_rs\', array(
        \'settings\' => \'mcs_rs_op\',
        \'label\'   => \'RSS Feed\',
        \'section\' => \'mcs_social_handle\',
        \'type\'    => \'select\',
                \'choices\'       => array(
                    \'rdf_url\'       => \'RDF/RSS 1.0 feed\',
                    \'rss2_url\'  => \'RSS 2.0 feed\',
                    \'atom_url\'  => \'Atom feed\',
        ),
    ));
}

//add
add_action( \'customize_register\', \'mcs_social\' );
正如你所看到的,这是WordPress Codex.

现在,这非常有效。当我进入外观->自定义时,我会看到社交手柄部分,可以添加和保存到文本框,并从选择中进行选择。

我现在的问题是,我似乎无法获取要在模板中使用的值。我还有其他自定义程序部分,在自定义程序中有1个设置(即徽标图像),它们工作正常,但当我对“社交”设置使用相同的get\\u theme\\u mod()时,什么都不会出现。

<div class="blue-social-box">
          <h5>Follow us on:</h5>
          <ul>
          <?php if ( get_theme_mod( \'mcs_fb_op\' ) ) : ?>
            <li>
              <figure><a href="https://www.facebook.com/<?php echo esc_url( get_theme_mod( \'mcs_fb_op\' ) ); ?>" target="_blank"><img src="<?php echo $fbImg; ?>" /></a></figure>
              <label><a href="https://www.facebook.com/<?php echo esc_url( get_theme_mod( \'mcs_fb_op\' ) ); ?>" target="_blank">Facebook</a></label>
            </li>
          <?php elseif ( get_theme_mod( \'mcs_fb_op\' ) ) : ?>
                        <li>
              <figure><a href="https://www.twitter.com/<?php echo esc_url( get_theme_mod( \'mcs_tw_op\' ) ); ?>" target="_blank"><img src="<?php echo $twImg; ?>" /></a></figure>
              <label><a href="https://www.twitter.com/<?php echo esc_url( get_theme_mod( \'mcs_tw_op\' ) ); ?>" target="_blank">Twitter</a></label>
            </li>
          <?php 
                    elseif ( get_theme_mod( \'mcs_rs_op\' ) ) : 
                    $rssType = esc_url(get_theme_mod(\'mcs_rs_op\'));
                        ?>
                        <li>
              <figure><a href="<?php bloginfo($rssType); ?>" target="_blank"><img src="<?php echo $rsImg; ?>" /></a></figure>
              <label><a href="<?php bloginfo($rssType); ?>" target="_blank">RSS Feed</a></label>
            </li>
                    <?php else : //Nothing ?>
          <?php endif; ?>
          </ul>
        </div>
我在文档中没有发现任何有用的东西,那么获取这些值的正确方法是什么呢?

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

你的代码很好,除了add_setting. 您已分配option 对于type 参数它将为每个字段保存单独的选项。如果您使用theme_mod, 这会解决你的问题。

\'type\' => \'theme_mod\',

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register