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

时间:2014-04-14 19:53:08

标签: php wordpress

我在Wordpress主题中有页脚,在页脚段落中。

它看起来像这样:

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

当您在自定义程序中时,可以选择更改页脚中的文本 我希望默认显示例如:
版权所有2014。

这是默认文字,如果用户更改此内容,则文字将替换版权所有2014,并且该文本将是用户已设置的文字。

我需要在footer.php中创建一些代码,functions.php定制器的其余代码在哪里?

这是用if else语句创建的,还是在Wordpress中有一些预制代码?

这是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 个答案:

答案 0 :(得分:3)

您可以使用get_theme_mod函数的第二个参数。您可以传递默认值作为第二个参数,如果之前未保存您的设置,将返回该参数。

<p><?php echo get_theme_mod('site_intro', 'Copyright '.date('Y'));

答案 1 :(得分:1)

我没有看到任何代码,因此我无法告诉您如何更新功能以获得您想要的功能。但我会根据目前所见的情况给出解决方案。

您可以在页脚中执行此操作。

<?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>