douzhuican0041
douzhuican0041
2014-04-14 19:53

Wordpress自定义程序 - 替换get_theme_mod中的文本

已采纳

I have footer in Wordpress theme, and in the footer paragraph.

It looks like this:

<p><?php echo get_theme_mod( 'site_intro' ); ?></p>

And when you in customizer there is option to change text in footer.
I want to be displayed by default for example:
Copyright 2014.

This is default text, and if user change this, text will be replace Copyright 2014 and it will be text that user has setup.

Where I need to make some code, in footer.php, functions.php where are the rest of the code for customizer?

Is this made with if else statement, or there is some premade code in Wordpress?

This is functions.php

function theme_customize_register( $wp_customize ) {

    if ( class_exists( 'WP_Customize_Control' ) ) {
        class PTD_Textarea_Control extends WP_Customize_Control {
            public function render_content() {?>
                <label>
                <span class="customize-control-title"><?php echo esc_html( $this->label );?></span>
                <textarea class="large-text" cols="20" rows="5" <?php $this->link(); ?>>
                    <?php echo esc_textarea( $this->value() ); ?>
                </textarea>
                </label>
                <?php
            }
        }
    }

    $wp_customize->add_setting( 'site_intro', array(
        'default'           => '',
        'transport'         => 'postMessage'
    ));
    $wp_customize->add_section( 'theme_site_info', array(
        'title'             => 'Footer informaation', 'theme',
        'description'       => 'Custom Footer', 'theme',
        'priority'          => 20,
    ));
    $wp_customize->add_control( new PTD_Textarea_Control( $wp_customize, 'site_intro_control', array(
        'label'             => 'Website Footer', 'theme',
        'section'           => 'theme_site_info',
        'settings'          => 'site_intro'
    )));

}
add_action( 'customize_register', 'theme_customize_register' );
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongyoulou4829 dongyoulou4829 6年前

    You can use the second parameter of get_theme_mod function. You can pass a default value as a second parameter, which will be returned, in case your setting has not been saved previously.

    <p><?php echo get_theme_mod('site_intro', 'Copyright '.date('Y'));
    
    点赞 评论 复制链接分享
  • dqlhsm9820 dqlhsm9820 7年前

    I don't see any code so I can't tell you how to update the function to get what you want. But I'll give the solution based on what I have seen so far.

    You can do this in your footer.

    <?php
         $text = get_theme_mod('site_intro');
         if(empty($text){
                $text = 'Copyright '.date('Y'); //If you just want 2014 use Copyright 2014
         }
    ?>
    
    <p><?php echo $text; ?></p>
    
    点赞 评论 复制链接分享

为你推荐