douyigua5381 2015-12-30 04:26
浏览 73

自定义主题自定义程序选项似乎没有显示在主页上

I am quite new to php and I'm creating a section in theme customiser (in a custom WP theme) to change the top banner image. All appears to work well - the options show in the customizer, I can select and upload images, and save the changes. All changes show in the theme customizer window. But when refreshing the main page, all I see is what is in index.php outside of the php tags that are supposed to retrieve the images / text etc. Below is the code in the theme-customizer.php file (I've exluded the sections showing code for adding text as if I can get the image to show the rest should follow):

    add_action('customize_register', 'k1_customize_register');
function k1_customize_register($wp_customize) {

    // Top Banner Image
    $wp_customize->add_section('k1_banner', array(
        'title' => __('K1 Top Banner', 'k1-framework'),
        'description' => __('Allows you to upload a banner image to display underneath the main navigation.', 'k1-framework'),
        'priority' => 36
    ));

 // Add setting for checkbox for banner image display
    $wp_customize->add_setting('k1_custom_settings[display_k1_banner]', array(
        'default' => 0,
        'type' => 'option'
    ));

    // Add control for checkbox for banner image display
    $wp_customize->add_control('k1_custom_settings[display_k1_banner]', array(
        'label' => __('Display the Top Banner Image?', 'k1-framework'),
        'section' => 'k1_banner',
        'settings' => 'k1_custom_settings[display_k1_banner]',
        'type' => 'checkbox'
    ));


    // Add setting for top banner image
    $wp_customize->add_setting('k1_banner', array(
        'default' => 'http://lorempixel.com/1200/300',
        'type' => 'theme_mod',
        'active_callback' => 'is_front_page',
        'sanitize_callback' => 'esc_url_raw'
    ));

    // Add control for top banner image
    $wp_customize->add_control('k1_banner', array(
        'label' => __('Upload the Top Banner Image', 'k1-framework'),
        'section' => 'k1_banner'


    ));

    // Add setting for banner heading
    $wp_customize->add_setting('k1_banner_heading', array(
        'default' => 'What can K1 do for you?',
        'type' => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field'
    ));

    // Add control for banner heading
    $wp_customize->add_control('k1_banner_heading', array(
        'label' => __('Banner heading text', 'k1-framework'),
        'section' => 'k1_banner',
        'type' => 'text'
    ));

    // Add setting for banner description
    $wp_customize->add_setting('k1_banner_desc', array(
        'default' => 'Lorem ipsum dolor sit amet',
        'type' => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field'
    ));

    // Add control for banner description
    $wp_customize->add_control('k1_banner_desc', array(
        'label' => __('Banner description text', 'k1-framework'),
        'section' => 'k1_banner',
        'type' => 'text'
    ));

    // Add setting for banner link
    $wp_customize->add_setting('k1_banner_link', array(
        'default' => '#',
        'type' => 'theme_mod',
        'sanitize_callback' => 'esc_url_raw'
    ));

    // Add control for banner link
    $wp_customize->add_control('k1_banner_link', array(
        'label' => __('Banner read more link', 'k1-framework'),
        'section' => 'k1_banner',
        'type' => 'text'
    ));

    //adding setting for banner link text area
    $wp_customize->add_setting('k1_banner_link_text', array(
        'default'  => 'More',
        'sanitize_callback' => 'sanitize_text_field'
     ));
    $wp_customize->add_control('k1_banner_link_text', array(
        'label'   => 'Read more link text here',
        'section' => 'k1_banner',
        'type'    => 'text',
    ));

}

And here is the code in index.php:

And in index.php where the php code is called:

    <div class="row">

        <div class="col-lg-12 top-banner">


<?php if($options['display_k1_banner'] != '') : ?>  

            <div class="top-banner-inner">


            <img src="<?php echo get_theme_mod( 'k1_banner' ); ?>" alt="Banner image" />


                <h1><?php echo get_theme_mod( 'k1_banner_heading' ); ?></h1>
                    <p><?php echo get_theme_mod( 'k1_banner_desc' ); ?></p>
                    <a class="button" href="<?php echo get_theme_mod( 'k1_banner_link' ); ?>"><em><?php echo get_theme_mod( 'k1_banner_link_text' ); ?></em></a>


            </div> <!-- end top-banner-inner -->
             <?php endif; ?> 




        </div> <!-- end top-banner -->

    </div> <!-- end row -->

The original html where the banner was to be is:

<!-- TOP BANNER -->

<div class="container">

        <div class="row">

            <div class="col-lg-12 top-banner">

                <div class="top-banner-inner">

                <img src="http://lorempixel.com/1200/300" alt="Banner image" />
                    <h1>Heading text</h1>
                        <p>Lorem ipsum dolor sit amet</p>
                        <a class="button" href=""><em>More</em></a>



                </div> <!-- end top-banner-inner -->


            </div> <!-- end top-banner -->

        </div> <!-- end row -->

    </div> <!-- end container -->

If anyone has any ideas on how to get this to work I would really appreciate it.

  • 写回答

2条回答 默认 最新

  • duanlidi1051 2015-12-30 10:36
    关注

    I tested your code in my environment, and it seems that the customizer code itself (the first block) works fine. The problem is just with the placement of the second block. You probably just have it in a place that is not visible (like I did at first).

    To help you with the placement, more info is needed on where exactly the banner should / should not be shown.

    EDIT:

    The most natural place to start with is to add the content to the bottom of the theme header.php. Then, the banner will appear always on the top of the page.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog