WordPress.org

Support

Support » Themes and Templates » [Resolved] conditional for pages/sub-pages

[Resolved] conditional for pages/sub-pages

  • I’d like to display a comment form on Sub-Pages only.
    I currently have this for my conditional code, (within page.php):

    <?php
    if (is_page('7') ) {}
    else {
    comments_template();
    }
    ?>

    Note: ID 7 is my Parent Page and I don’t want to show the comment form there. It currently works, but is that correct? or can someone show me a better way of writing it?

Viewing 3 replies - 1 through 3 (of 3 total)
  • This does the same as above:

    <?php if (!is_page('7')) comments_template(); ?>

    The exclamation mark means NOT, so if not page 7 then display the comments template.
    Also if you are only executing ONE statement inside an IF/ELSE block only then you don’t need to use the curly braces { } around it. If you are executing more than one then you need to use curly braces. For example:

    <?php
    //Here we don't need curly braces because we are only executing one statement
    if($test) echo "Hello"; 
    
    //Here we do need them because we are executing two statements
    if($test){ echo "Hello"; echo "Hello Again"; }
    
    ?>

    Thanks. I wonder though if I was thinking about it the wrong way…
    I rewrote it so that comments would only display on the Sub Page IDs, versus _NOT_ displaying comments on the Parent Pages, thus only showing comments on Sub Pages.

    Here’s what I got, an array of IDs.

    <?php if (is_page(array(26,58))) comments_template(); ?>

    This will only display the comments template if the Page has a parent:

    <?php if($post->post_parent) comments_template(); ?>

    But you could simply do this in the WP-admin, by turning off comments on the pages you don’t want the comments template on. If you don’t want the «Comments closed»-message, simply use:

    <?php if(comments_open()) comments_template(); ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] conditional for pages/sub-pages’ is closed to new replies.
Skip to toolbar