最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Adding Custom Image To Header Via New Customizer Setting

programmeradmin1浏览0评论

I'm trying to add a custom image setting to a custom panel/section for the header.

In functions.php, I used:

$wp_customize->add_setting('swag_header_logo');
$wp_customize->add_control(new WP_Customize_Upload_Control($wp_customize,'swag_header_logo',array(
     'label'      => __('Logo', 'frontend-theme'),
     'section'    => 'swag_header_content_section',
     'settings'   => 'swag_header_logo'
 )));

Which shows up and seems to work in the customizer. However, when trying to get it to show up in the header, it gives the error:

Fatal error : Call to undefined function swag_header_logo() in /home/xxxxxx/public_html/wp-content/themes/frontend-theme/header.php on line 551

The code I used in the header.php:

$swag_header_logo = get_theme_mod('swag_header_logo');

and

<?php swag_header_logo(); ?>

How do I fix the error?

I'm trying to add a custom image setting to a custom panel/section for the header.

In functions.php, I used:

$wp_customize->add_setting('swag_header_logo');
$wp_customize->add_control(new WP_Customize_Upload_Control($wp_customize,'swag_header_logo',array(
     'label'      => __('Logo', 'frontend-theme'),
     'section'    => 'swag_header_content_section',
     'settings'   => 'swag_header_logo'
 )));

Which shows up and seems to work in the customizer. However, when trying to get it to show up in the header, it gives the error:

Fatal error : Call to undefined function swag_header_logo() in /home/xxxxxx/public_html/wp-content/themes/frontend-theme/header.php on line 551

The code I used in the header.php:

$swag_header_logo = get_theme_mod('swag_header_logo');

and

<?php swag_header_logo(); ?>

How do I fix the error?

Share Improve this question asked May 5, 2019 at 11:01 XarcellXarcell 1035 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

First part of your code looks correct - it's hard to say if it will work, because it's only part of it, but there is nothing wrong with the part you've shown.

Problem lies in this line:

<?php swag_header_logo(); ?>

You haven't defined such function anywhere in your code, so you can't call it - and that's what the error is saying ("Call to undefined function swag_header_logo()").

So how to fix it?

In this line:

$swag_header_logo = get_theme_mod('swag_header_logo');

you get the ID of attachment set as swag_header_logo and you store this ID in $swag_header_logo variable.

So if you want to display the image, just use:

echo wp_get_attachment_image( $swag_header_logo, '<SIZE>' ); // change size to real value

or if you want only the URL of the image:

echo wp_get_attachment_image_url( $swag_header_logo, '<SIZE>' ); // change size to real value
发布评论

评论列表(0)

  1. 暂无评论