[resolved] conditional for pages/sub-pages (4 posts)

  1. RanchNachos
    Posted 5 years ago #

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

    if (is_page('7') ) {}
    else {

    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?

  2. xdesi
    Posted 5 years ago #

    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:

    //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"; }
  3. RanchNachos
    Posted 5 years ago #

    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(); ?>

  4. Stian Andreassen
    Posted 5 years ago #

    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(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic