我在我的Wordpress网站的functions.php文件中定义了以下函数,它应该提供打开或关闭注释的选项:
$wp_customize->add_section( 'display_comments', array(
'title' => 'Comments',
'priority' => 36,
) );
$wp_customize->add_setting( 'mytheme_comments' );
$wp_customize->add_control( 'mytheme_comments', array(
'label' => 'Comments on or off',
'section' => 'display_comments',
'type' => 'select',
'default' => 'Off',
'choices' => array(
'value1' => 'On',
'value2' => 'Off',
),
) );
然后我在我的single.php文件中有这个,这是显示单个博客文章的页面:
<?php if (get_theme_mod ( 'mytheme_comments' == 'On' ) ) : ?>
<?php comments_template(); ?>
<?php elseif (get_theme_mod ( 'mytheme_comments' == 'Off' ) ) : ?>
<?php endif ?>
默认情况下评论是关闭的,但从下拉列表中选择“开启”则没有任何效果。
任何想法我可能做错了什么?
答案 0 :(得分:3)
你应该改变
if (get_theme_mod ( 'bistheme_comments' == 'On' ) )
到
if (get_theme_mod ( 'bistheme_comments') == 'On' )
和
elseif (get_theme_mod ( 'mytheme_comments' == 'Off' ) )
到
elseif (get_theme_mod ( 'mytheme_comments') == 'Off' )
重写代码的更好方法
$var = get_theme_mod('mytheme_comments');
if ($var == 'On') {
comments_template();
} else if ($var == 'Off') {
// Var is Off
} else {
// Var was not set
}