1

I have defined the following function in my Wordpress site's functions.php file, it should give the option to turn comments on or off:

$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',
        ),
) );

I then have this in my single.php file, which is the page that shows an individual blog post:

<?php if (get_theme_mod ( 'mytheme_comments' == 'On' ) ) : ?>
<?php comments_template(); ?>
<?php elseif (get_theme_mod ( 'mytheme_comments' == 'Off' ) ) : ?>
<?php endif ?>

The comments are off by default, but choosing 'on' from the dropdown, does not have any effect.

Any ideas what I might be doing wrong?

1 Answer 1

3

You shuld change

if (get_theme_mod ( 'bistheme_comments' == 'On' ) )

To

if (get_theme_mod ( 'bistheme_comments') == 'On'  )

AND

elseif (get_theme_mod ( 'mytheme_comments' == 'Off' ) )

To

elseif (get_theme_mod ( 'mytheme_comments') == 'Off'  )

Better way to rewrite your code

$var = get_theme_mod('mytheme_comments');
if ($var == 'On') {
    comments_template();
} else if ($var == 'Off') {
    // Var is Off
} else {
    // Var was not set 
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this. I've tried making the changes, but for some reason it still is not registering a change.
@user18577 can i see your updated code .. add the full code to pastbin
You code looks fine .... what exactly is the error ???? what do you want to register
I've figured it out, sorry - in single.php it should be 'value1' and 'value2' not 'on' and 'off'! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.