dongmi8980 2015-06-21 05:03
浏览 76
已采纳

Wordpress自定义图像控件不起作用

I am currently creating a Wordpress theme and I have run into a snag. I'm using the theme customize page to upload a main logo and a mobile logo. Those logo's should then be used on the page. I got it to work for twenty minutes but them it stopped working.

Here is my customize register function in function.php:

function wpalpha_customize_register( $wp_customize ) {

  $wp_customize->add_section( 'wpalpha_logo_section' , array(
    'title'                 =>  __('Logos', 'wpalpha'),
    'description'           =>  __('Upload logos to replace the default site logos.'),
    'panel'                 =>  'design_settings',
    'priority'              =>  10,
    'capability'            =>  'edit_theme_options',
    'theme_supports'        =>  '',
  ) );

  $wp_customize->add_setting('wpalpha_desktop_logo', array(
    'type'                  =>  'theme_mod',
    'capability'            =>  'edit_theme_options',
    'theme_supports'        =>  '',
    'default'               =>  get_template_directory_uri().'/img/header/shield_text.png',
    'sanitize_callback'     =>  '',
    'sanitize_js_callback'  =>  '',
    'transport'             =>  'refresh',
  ));
  $wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'wpalpha_desktop_logo', array(
    'label'                 =>  __('Choose Logo', 'wpalpha'),
    'section'               =>  'wpalpha_logo_section',
    'settings'              =>  'wpalpha_desktop_logo',
    'context'               =>  'wpalpha-desktop-logo',
  )));

  $wp_customize->add_setting('wpalpha_mobile_logo', array(
    'type'                  =>  'theme_mod',
    'capability'            =>  'edit_theme_options',
    'theme_supports'        =>  '',
    'default'               =>  get_template_directory_uri().'/img/header/shield.png',
    'sanitize_callback'     =>  '',
    'sanitize_js_callback'  =>  '',
    'transport'             =>  'refresh',
  ));
  $wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'wpalpha_mobile_logo', array(
    'label'                 =>  __('Choose Logo', 'wpalpha'),
    'section'               =>  'wpalpha_logo_section',
    'settings'              =>  'wpalpha_mobile_logo',
    'context'               =>  'wpalpha-mobile-logo',
  )));
}

add_action('customize_register', 'wpalpha_customize_register');

And here is where I'm using it in header.php:

<a class="navbar-brand" href="<?php echo home_url(); ?>">

  <?php if(get_theme_mod('wpalpha_desktop_logo') != ''): ?>
  <img class="desktop dlp_logo" src="<?php echo esc_url(get_theme_mod('wpaplpha_desktop_logo')); ?>">
  <?php endif; ?>

  <?php if(get_theme_mod('wpalpha_mobile_logo') != ''): ?>
  <img class="mobile dlp_logo" src="<?php echo esc_url(get_theme_mod('wpaplpha_mobile_logo')); ?>">
  <?php endif; ?>

</a>

This is what my code outputs as:

<img class="desktop dlp_logo" src="">
<img class="mobile dlp_logo" src="">

This image shows that the options work on the customize page but my logos no longer show up (The little box in the top left corner is supposed to be the logo): (Apparently I'm not good enough for this site to post a picture so you'll have follow this link...)

http://i.stack.imgur.com/C38O6.jpg

Please help! Thanks

Update #1:

I looks like my database is no longer being updated when I change the logos on the Wordpress backend site. Am I forgetting update options? I thought that was built into these functions.

Update #2:

<?php echo var_dump(get_theme_mod('wpalpha_desktop_logo')); ?>

This var_dump() returns 'string(82) "http://localhost:8888/wp-content/themes/adenicboy-alpha/img/header/shield_text.png"' so I'm really confused why this isn't working...

  • 写回答

2条回答 默认 最新

  • dougan4884 2015-06-21 19:26
    关注

    I've come up with a solution!

    I was able to make it work by calling get_theme_mod() and storing the result in a string instead of calling it inside of the src area. I then echo the string into the src area.

    <a class="navbar-brand" href="<?php echo home_url(); ?>">
    
      <?php if(get_theme_mod('wpalpha_desktop_logo') != ''): ?>
        <?php $wpalpha_desktop_logo = esc_url(get_theme_mod('wpalpha_desktop_logo')); ?>
        <img class="desktop dlp_logo" src="<?php echo $wpalpha_desktop_logo; ?>">
      <?php endif; ?>
    
      <?php if(get_theme_mod('wpalpha_mobile_logo') != ''): ?>
        <?php $wpalpha_mobile_logo = esc_url(get_theme_mod('wpalpha_mobile_logo')); ?>
        <img class="mobile dlp_logo" src="<?php echo $wpalpha_mobile_logo; ?>">
      <?php endif; ?>
    
    </a>
    

    I highly recommend using var_dump() on your get_theme_mod() if anyone has a similar issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频