如何在添加自定义分类后添加Customize_Register操作

时间:2014-06-13 作者:Geoffrey Burdett

customize\\u register仅适用于customize\\u register挂钩,无论我使用哪个挂钩添加自定义分类法,它总是在customize\\u register之后发生。

(我想循环浏览自定义分类法中的术语列表,为每个术语创建类别和自定义选项)

编辑:

这是我的模板函数文件中的淡化代码:

register_taxonomy( \'brand\', $object_types, $args );
add_action( \'muplugins_loaded\', \'brand\', 0 );
// ????? What Hook can I use here^ that will run before the \'customize_register\' hook ????? //

function iartz_customize_register( $wp_customize ) {
    $brands = get_terms(\'brand\');
    foreach($brands AS $brand){
        //Add color options for each term of taxtonomy \'brand\'
    }
}
add_action( \'customize_register\', \'iartz_customize_register\' );

2 个回复
SO网友:Nicolai Grossherr

AFAIK在customizer_register 挂钩状态。因此,这个问题可能有不同的原因,但从您的问题中看,它们并不明显。此外,我不确定你想要实现什么,所以你可能想根据@Rarst的问题来填补信息空白。除此之外,还有多种方法,例如,向定制器添加一个分类下拉列表,我选择了一个并链接了另一个,请参见下文。

<小时/>Disclaimer:
这不是我的代码,出于完整性原因,我将其添加到此处,并添加了指向源代码的链接。我自己还没有试过,但据我所知,它没有任何问题,而且对人们有用。

主题定制器的分类代码下拉列表→ Source 根据要点→ 根据博客article 作者:Eric Juden

这将进入functions.php

add_action(\'customize_register\', \'my_customize_register\');
function my_customize_register($wp_customize){
require_once(TEMPLATEPATH . \'/class/wp_customizer_taxonomy_dropdown.php\');
 
$wp_customize->add_section(\'my_theme_blog_featured_categories\', array(
    \'title\' => __(\'Blog: Featured Categories\'),
    \'priority\' => 36,
));

$wp_customize->add_setting(\'featured_category_1\', array(
    \'default\' => get_option(\'default_category\', \'\'),
));

$wp_customize->add_control( new Taxonomy_Dropdown_Customize_Control($wp_customize, \'featured_category_1\', array(
    \'label\' => __(\'Featured Area 1\'),
    \'section\' => \'my_theme_blog_featured_categories\',
    \'settings\' => \'featured_category_1\',
    \'args\' => array(), // arguments for wp_dropdown_categories function...optional. array(\'taxonomy\' => \'my_taxonomy\')
)));
 
return $wp_customize;
}
该文件名为wp_customizer_taxonomy_dropdown.php 位于相对于功能的文件夹中。php,称为class:

class Taxonomy_Dropdown_Customize_Control extends WP_Customize_Control {
    public $type = \'taxonomy_dropdown\';
    var $defaults = array();
    public $args = array();
 
    public function render_content(){
        // Call wp_dropdown_cats to ad data-customize-setting-link to select tag
        add_action(\'wp_dropdown_cats\', array($this, \'wp_dropdown_cats\'));
 
        // Set some defaults for our control
        $this->defaults = array(
            \'show_option_none\' => __(\'None\'),
            \'orderby\' => \'name\', 
            \'hide_empty\' => 0,
            \'id\' => $this->id,
            \'selected\' => $this->value(),
        );
 
        // Parse defaults against what the user submitted
        $r = wp_parse_args($this->args, $this->defaults);
 
?>
    <label><span class="customize-control-title"><?php echo esc_html($this->label); ?></span></label>
<?php  
        // Generate our select box
        wp_dropdown_categories($r);
    }
 
    function wp_dropdown_cats($output){
        // Search for <select and replace it with <select data-customize=setting-link="my_control_id"
        $output = str_replace(\'<select\', \'<select \' . $this->get_link(), $output);
        return $output;
    }
}
这应该与附加信息相联系,并能自我解释。

其他类似方法

WordPress Theme Customizer Custom Controls 作者@bueltge
→ 链接到source 分类学下拉列表示例的Gist
→ 他的answer 关于这个话题

→ 还有一些教程、文章等,如果需要的话,您可以自己轻松找到。

SO网友:Geoffrey Burdett

简短回答:这两个函数都可以挂接以自定义\\u寄存器,它们可以正常工作。customize\\u register挂钩只在customize页面上起作用,但我需要在这两个页面上都使用register\\u taxonomy函数。因此,为了在前端页面调用中使用register\\u taxonomy函数,我也在init hook中调用了它。

“所以这个问题可能有不同的原因”

ialocin,你说得对。。。

我希望customize\\u register钩子在每次页面调用时都能调用(因为文档中是这么说的),但事实并非如此,这使我的问题解决方向完全错误。

谢谢你的帮助!

结束

相关推荐

Shotcode error on functions

您好,我有一个生成错误的短代码。有人能帮忙吗?add_shortcode(\'do-action\', \'do_action_shortcode\'); function do_action_shortcode($atts, $content = \'\') {   ob_start();   do_action($content);   $out = ob_get_contents();   ob_end_clean();   return $