WordPress 儿童主题

发布于 2024-11-25 10:11:24 字数 5142 浏览 0 评论 0原文

我正在尝试在 WordPress 子主题中重新声明父主题已在使用的函数。但是,在尝试这样做时,我收到“致命错误:无法重新声明”消息。

另外,我尝试使用以下内容,但没有成功:

if (!function_exists('jr_load_scripts')) {
  // do fancy things here...
}

如果您想拥有一个,这里是链接快速查看...

编辑:这是完整的代码:

if (!function_exists('jr_load_scripts')) {
function jr_load_scripts() {
global $app_abbr;

$http = (is_ssl()) ? 'https' : 'http';

// load google cdn hosted scripts if enabled
if (get_option($app_abbr.'_google_jquery') == 'yes') :

    wp_deregister_script('jquery');
    wp_register_script('jquery', (''.$http.'://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'), false, '1.4.2');
    wp_register_script('jquery-ui-custom', ''.$http.'://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js', false, '1.8');

else :

    wp_register_script('jquery-ui-custom', get_bloginfo('template_directory').'/includes/js/jquery-ui-1.8.custom.min.js', false, '1.8');

endif;

wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-custom');

wp_enqueue_script('jquery-tag', get_bloginfo('template_directory').'/includes/js/jquery.tag.js', array('jquery'), '');
wp_enqueue_script('smoothscroll', get_bloginfo('template_directory').'/includes/js/smoothscroll.js', array('jquery'), '');
wp_enqueue_script('lazyload', get_bloginfo('template_directory').'/includes/js/jquery.lazyload.mini.js', array('jquery'), '1.5.0');
wp_enqueue_script('elastic', get_bloginfo('template_directory').'/includes/js/jquery.elastic.js', array('jquery'), '1.0');
wp_enqueue_script('fancybox', get_bloginfo('template_directory').'/includes/js/jquery.fancybox-1.3.4.pack.js', array('jquery'), '1.3.4');
wp_enqueue_script('qtip', get_bloginfo('template_directory').'/includes/js/jquery.qtip.min.js', array('jquery'), '1.0.0-rc3');
wp_enqueue_script('general', get_bloginfo('template_directory').'/includes/js/theme-scripts.js', array('jquery'), '3.0');

$jr_enable_indeed_feeds = get_option('jr_enable_indeed_feeds');
if ($jr_enable_indeed_feeds=='yes') :

     wp_enqueue_script('indeed-api', ''.$http.'://www.indeed.com/ads/apiresults.js');

wp_enqueue_script('jqtransform', get_bloginfo('template_directory') . '/includes/jqtransformplugin/jquery.jqtransform.js', array('jquery'),'');

endif;
}

编辑2:根据要求 - 主题排队文件的内容:

<?php
/**
 * These are scripts used within the JobRoller theme
 * To increase speed and performance, we only want to
 * load them when needed
 *
 * @package JobRoller
 * @version 1.0
 *
 */

function jr_load_scripts() {
    global $app_abbr;

    $http = (is_ssl()) ? 'https' : 'http';

    // load google cdn hosted scripts if enabled
    if (get_option($app_abbr.'_google_jquery') == 'yes') :

        wp_deregister_script('jquery');
        wp_register_script('jquery', (''.$http.'://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'), false, '1.4.2');
        wp_register_script('jquery-ui-custom', ''.$http.'://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js', false, '1.8');

    else :

        wp_register_script('jquery-ui-custom', get_bloginfo('template_directory').'/includes/js/jquery-ui-1.8.custom.min.js', false, '1.8');

    endif;

    wp_enqueue_script('jquery');
    wp_enqueue_script('jquery-ui-custom');

    wp_enqueue_script('jquery-tag', get_bloginfo('template_directory').'/includes/js/jquery.tag.js', array('jquery'), '');
    wp_enqueue_script('smoothscroll', get_bloginfo('template_directory').'/includes/js/smoothscroll.js', array('jquery'), '');
    wp_enqueue_script('lazyload', get_bloginfo('template_directory').'/includes/js/jquery.lazyload.mini.js', array('jquery'), '1.5.0');
    wp_enqueue_script('elastic', get_bloginfo('template_directory').'/includes/js/jquery.elastic.js', array('jquery'), '1.0');
    wp_enqueue_script('fancybox', get_bloginfo('template_directory').'/includes/js/jquery.fancybox-1.3.4.pack.js', array('jquery'), '1.3.4');
    wp_enqueue_script('qtip', get_bloginfo('template_directory').'/includes/js/jquery.qtip.min.js', array('jquery'), '1.0.0-rc3');
    wp_enqueue_script('general', get_bloginfo('template_directory').'/includes/js/theme-scripts.js', array('jquery'), '3.0');

    $jr_enable_indeed_feeds = get_option('jr_enable_indeed_feeds');
    if ($jr_enable_indeed_feeds=='yes') :

         wp_enqueue_script('indeed-api', ''.$http.'://www.indeed.com/ads/apiresults.js');

    endif;
}

// this function is called when submitting a new job listing
function jr_load_form_scripts() {
    // only load the tinymce editor when html is allowed
    if (get_option('jr_html_allowed') == 'yes') {
        wp_enqueue_script('tiny_mce', get_bloginfo('url').'/wp-includes/js/tinymce/tiny_mce.js');
        wp_enqueue_script('tiny_mce-wp-langs-en', get_bloginfo('url').'/wp-includes/js/tinymce/langs/wp-langs-en.js');
    }
}

// to speed things up, don't load these scripts in the WP back-end (which is the default)
if(!is_admin()) {
    add_action('wp_print_scripts', 'jr_load_scripts');
    // add_action('wp_print_styles', 'cp_load_styles');
}

I'm trying to redeclare a function in a wordpress child theme that is already in use by the parent theme. However, I get a "Fatal error: Cannot redeclare" message when trying to do so.

Also, I've tried using the following with no luck:

if (!function_exists('jr_load_scripts')) {
  // do fancy things here...
}

Here's the link if you want to have a quick look...

EDIT: Here's the full code:

if (!function_exists('jr_load_scripts')) {
function jr_load_scripts() {
global $app_abbr;

$http = (is_ssl()) ? 'https' : 'http';

// load google cdn hosted scripts if enabled
if (get_option($app_abbr.'_google_jquery') == 'yes') :

    wp_deregister_script('jquery');
    wp_register_script('jquery', (''.$http.'://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'), false, '1.4.2');
    wp_register_script('jquery-ui-custom', ''.$http.'://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js', false, '1.8');

else :

    wp_register_script('jquery-ui-custom', get_bloginfo('template_directory').'/includes/js/jquery-ui-1.8.custom.min.js', false, '1.8');

endif;

wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-custom');

wp_enqueue_script('jquery-tag', get_bloginfo('template_directory').'/includes/js/jquery.tag.js', array('jquery'), '');
wp_enqueue_script('smoothscroll', get_bloginfo('template_directory').'/includes/js/smoothscroll.js', array('jquery'), '');
wp_enqueue_script('lazyload', get_bloginfo('template_directory').'/includes/js/jquery.lazyload.mini.js', array('jquery'), '1.5.0');
wp_enqueue_script('elastic', get_bloginfo('template_directory').'/includes/js/jquery.elastic.js', array('jquery'), '1.0');
wp_enqueue_script('fancybox', get_bloginfo('template_directory').'/includes/js/jquery.fancybox-1.3.4.pack.js', array('jquery'), '1.3.4');
wp_enqueue_script('qtip', get_bloginfo('template_directory').'/includes/js/jquery.qtip.min.js', array('jquery'), '1.0.0-rc3');
wp_enqueue_script('general', get_bloginfo('template_directory').'/includes/js/theme-scripts.js', array('jquery'), '3.0');

$jr_enable_indeed_feeds = get_option('jr_enable_indeed_feeds');
if ($jr_enable_indeed_feeds=='yes') :

     wp_enqueue_script('indeed-api', ''.$http.'://www.indeed.com/ads/apiresults.js');

wp_enqueue_script('jqtransform', get_bloginfo('template_directory') . '/includes/jqtransformplugin/jquery.jqtransform.js', array('jquery'),'');

endif;
}

EDIT2: As requested - the contents of the theme-enqueue file:

<?php
/**
 * These are scripts used within the JobRoller theme
 * To increase speed and performance, we only want to
 * load them when needed
 *
 * @package JobRoller
 * @version 1.0
 *
 */

function jr_load_scripts() {
    global $app_abbr;

    $http = (is_ssl()) ? 'https' : 'http';

    // load google cdn hosted scripts if enabled
    if (get_option($app_abbr.'_google_jquery') == 'yes') :

        wp_deregister_script('jquery');
        wp_register_script('jquery', (''.$http.'://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'), false, '1.4.2');
        wp_register_script('jquery-ui-custom', ''.$http.'://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js', false, '1.8');

    else :

        wp_register_script('jquery-ui-custom', get_bloginfo('template_directory').'/includes/js/jquery-ui-1.8.custom.min.js', false, '1.8');

    endif;

    wp_enqueue_script('jquery');
    wp_enqueue_script('jquery-ui-custom');

    wp_enqueue_script('jquery-tag', get_bloginfo('template_directory').'/includes/js/jquery.tag.js', array('jquery'), '');
    wp_enqueue_script('smoothscroll', get_bloginfo('template_directory').'/includes/js/smoothscroll.js', array('jquery'), '');
    wp_enqueue_script('lazyload', get_bloginfo('template_directory').'/includes/js/jquery.lazyload.mini.js', array('jquery'), '1.5.0');
    wp_enqueue_script('elastic', get_bloginfo('template_directory').'/includes/js/jquery.elastic.js', array('jquery'), '1.0');
    wp_enqueue_script('fancybox', get_bloginfo('template_directory').'/includes/js/jquery.fancybox-1.3.4.pack.js', array('jquery'), '1.3.4');
    wp_enqueue_script('qtip', get_bloginfo('template_directory').'/includes/js/jquery.qtip.min.js', array('jquery'), '1.0.0-rc3');
    wp_enqueue_script('general', get_bloginfo('template_directory').'/includes/js/theme-scripts.js', array('jquery'), '3.0');

    $jr_enable_indeed_feeds = get_option('jr_enable_indeed_feeds');
    if ($jr_enable_indeed_feeds=='yes') :

         wp_enqueue_script('indeed-api', ''.$http.'://www.indeed.com/ads/apiresults.js');

    endif;
}

// this function is called when submitting a new job listing
function jr_load_form_scripts() {
    // only load the tinymce editor when html is allowed
    if (get_option('jr_html_allowed') == 'yes') {
        wp_enqueue_script('tiny_mce', get_bloginfo('url').'/wp-includes/js/tinymce/tiny_mce.js');
        wp_enqueue_script('tiny_mce-wp-langs-en', get_bloginfo('url').'/wp-includes/js/tinymce/langs/wp-langs-en.js');
    }
}

// to speed things up, don't load these scripts in the WP back-end (which is the default)
if(!is_admin()) {
    add_action('wp_print_scripts', 'jr_load_scripts');
    // add_action('wp_print_styles', 'cp_load_styles');
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无敌元气妹 2024-12-02 10:11:24

您无法覆盖该函数,因为父主题的声明是在您声明之后执行的,并且它不会尝试检查该函数是否已存在。

唯一的方法是将父主题的函数声明包装在 if 条件内。

if (!function_exists('jr_load_scripts')) {

}

You cannot override that function, as the parent theme's declaration is executed after your declaration, and it doesn't try to check if the function already exists.

The only way would be to wrap the parent theme's function declaration inside the if condition.

if (!function_exists('jr_load_scripts')) {

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文