Add Links to Customizer

时间:2017-09-18 作者:Stephen Sabatini

我使用主题定制器对WP样板进行了很多更改。然而,有些事情需要一个ACF选项页面。为了将主题定制器绑定到选项页面中,我只想创建一个名为“索引/归档设置”的部分,当您单击该部分时,只需创建一个链接到每个选项页面的超链接列表。这将把一切联系在一起,至少在某种程度上不会让你玩猜谜游戏。有人知道怎么做吗?或者如果可能的话?

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

您需要从添加register 操作,然后添加section, settingcontrol. 请确保将$my\\u主题更改为主题的slug,以便此选项仅显示在主题上。

class SampleAddonCustomizer {

   public $my_theme = \'enter-your-theme-slug-here\'; // <- Enter your theme slug here

   public function hooks() {

     $current_theme = wp_get_theme();

     if( $current_theme->template == $this->my_theme ) {
       add_action( \'customize_register\', array( $this, \'register\' ), 99 );
     }

   }

   public function register( $wp_customize ) {

         $wp_customize->add_section( \'my_custom_section_links\', array(
           \'capability\' => \'edit_theme_options\',
           \'priority\'   => 50,
           \'title\'      => __( \'Index/Archive\', \'sample-customizer-addon\' )
         ) );


         $wp_customize->add_setting(  \'special\', array(
           \'capability\' => \'edit_theme_options\',
           \'type\'       => \'hidden\',
           \'autoload\'   => false
         ) );

         $wp_customize->add_control( \'special\', array(
             \'label\'   => \'Links to settings\',
             \'description\' => $this->get_links(),
             \'section\' => \'my_custom_section_links\',
             \'type\'    => \'hidden\',
         ) );
   }

   public function get_links() {

     $links = array(
       array(\'url\' => \'http://example.com/\', \'text\' => \'Example\', \'desc\' => \'Just an example\'),
       array(\'url\' => \'https://yahoo.com/\', \'text\' => \'Yahoo!\', \'desc\' => \'More entertainment\'),
       array(\'url\' => \'https://google.com/\', \'text\' => \'Google\', \'desc\' => \'Just Search\'),
       array(\'url\' => \'https://bing.com/\', \'text\' => \'Bing\', \'desc\' => \'Nice pictures\'),
     );

     $html = \'\';

     foreach ($links as $link) {
       $html .= \'<p>\'.$link[\'desc\'].\'<br>\'.PHP_EOL;
       $html .= sprintf(\'<a href="%s">%s</a>\', $link[\'url\'], $link[\'text\']) . \'<br>\' . PHP_EOL;
       $html .= \'</p>\'.PHP_EOL;
     }


     return $html;
   }

 }

 $sampleAddonCustomizer = new SampleAddonCustomizer();
 $sampleAddonCustomizer->hooks();

结束

相关推荐