如何才能只添加到父主题函数,而不是重新声明整个函数

时间:2016-12-17 作者:rudtek

我正在使用一个商业主题,它在WordPress的外观/定制器中有主题设置。他们通过这样的函数调用设置:(这只是函数的一部分,大约有400行长)

function customizer_library_gateway_options() {
    // Home
    $section = \'home\';

    $sections[] = array(
        \'id\'            => $section,
        \'title\'         => __( \'Home\', \'gateway\' ),
        \'priority\'      => \'20\',
        \'description\'   => __( \'Home Page Options.\', \'gateway\' ),
        \'panel\'         => $panel
    );

    $options[\'home_hero_bg\'] = array(
        \'id\'                => \'home_hero_bg\',
        \'label\'             => __( \'Home Background Image\', \'gateway\' ),
        \'section\'           => $section,
        \'type\'              => \'image\',
        \'default\'           => $imagepath . \'hero-bg.jpg\',
        \'active_callback\'   => \'is_front_page\'
    );

    $options[\'bg_attachement\'] = array(
        \'id\'                => \'bg_attachement\',
        \'label\'             => __( \'Select the behavior of the background image.\', \'gateway\' ),
        \'section\'           => $section,
        \'type\'              => \'select\',
        \'choices\'           => $bg_attachment,
        \'default\'           => \'fixed\',
        \'active_callback\'   => \'is_front_page\'
    );

    $options[\'home_hero_bg_color\'] = array(
        \'id\'                => \'home_hero_bg_color\',
        \'label\'             => __( \'Home Hero background color if no image is being used.\', \'gateway\' ),
        \'section\'           => $section,
        \'type\'              => \'color\',
        \'default\'           => $primary_color,
        \'active_callback\'   => \'is_front_page\',
        \'transport\'         => \'postMessage\'
    );

    $options[\'home_posts_title\'] = array(
        \'id\'                => \'home_posts_title\',
        \'label\'             => __( \'Enter the home featured posts section title\', \'gateway\' ),
        \'section\'           => $section,
        \'type\'              => \'text\',
        \'default\'           => \'Featured Posts\',
        \'active_callback\'   => \'is_front_page\',
        \'transport\'         => \'postMessage\'
    );

    $options[\'home_posts_cat\'] = array(
        \'id\'                => \'home_posts_cat\',
        \'label\'             => __( \'Home Posts Category\', \'gateway\' ),
        \'section\'           => $section,
        \'type\'              => \'select\',
        \'choices\'           => $options_cats,
        \'default\'           => \'\',
        \'active_callback\'   => \'is_front_page\'
        );
    }
add_action( \'init\', \'customizer_library_gateway_options\' );

My question is: How can I add to this array through my child theme so I can add a logo option? I don\'t necessarily want to re-declare the whole function if I can just add my array insertion:

我在考虑添加过滤器,但我不确定。这是我在我的子主题函数中尝试的。php:

function customizer_library_gateway_home_logo() {

    $options[\'home_logo\'] = array(
        \'id\'        => \'home_logo\',
        \'label\'     => __( \'Logo\', \'gateway\' ),
        \'section\'   => \'home\',
            \'type\'      => \'image\',
            \'default\'   => $imagepath . \'logo.png\'
        ); 

    }

    add_filter( \'customizer_library_gateway_options\', \'customizer_library_gateway_home_logo\' );
但是,主题自定义程序没有更改。如果我直接在主题的原始文件中添加home\\u徽标条目,它确实会起作用。也许我应该采取不同的行动?

1 个回复
SO网友:filipecsweb

我可能会给你一个解决方案。

但是,首先考虑使用默认的WordPress自定义徽标支持。您只需要将其添加到函数中。php文件:


function theme_prefix_setup() {

    add_theme_support( \'custom-logo\' );

}

add_action( \'after_setup_theme\', \'theme_prefix_setup\' );

查看有关的更多详细信息add_theme_support( \'custom-logo\' );https://codex.wordpress.org/Theme_Logo.

现在,如果单击“外观”(Appearance)和“自定义”(Customize),您将在“站点标识”(Site Identity)下使用新的徽标字段。

如果上述解决方案不适合您,请继续阅读。。。我测试了下面的代码,效果很好(当然,我修改了它以满足您的一些需要)。


/**
 * @param object    $instance
 */
function customize_customize_page( $instance ) {

    // Set the args. You do not need to set \'type\' key here.
    $args = array(
        \'priority\' => 0,
        \'section\' => \'home\',
        // \'settings\' => \'home\',
        \'label\' => __( \'Logo\' ),
        \'description\' => __( \'Description\' ),
    );

    // Here we instance WP_Customize_Image_Control class and set its paramaters.
    // See that here is where you choose your option ID.
    $instance->add_control( new WP_Customize_Image_Control( $instance, \'home_logo\', $args ) );

}

add_action( \'customize_register\', \'customize_customize_page\', 199, 1 );

如果上述代码不起作用,应尝试取消注释键“settings”,然后重试。

相关推荐

如何在Functions.php中链接style.css

我是WordPress的新手;我刚开始学习WordPress。我想把风格联系起来。函数中的css。php,但我无法解决这里可能存在的问题。谁能给我指出正确的方向吗?指数php<?php get_header(); ?> <?php if ( have_posts() ) { while ( have_posts() ) { the_post();