WordPress.org

Forums

Embedding code within "if" statements (6 posts)

  1. Paperninja
    Member
    Posted 8 years ago #

    I've been tweaking a few things within WordPress, slowly figuring out how everything works, and wanted to know something about if statements: is it possible to embed further php code within such a statement?

    The situation I'm trying to create involves only displaying the Comments option under one specific post category. My guess(which does not work) as to how the code would be structured is something to this effect:

    <?php if (in_category('5'))
    {
    ?php comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)'));
    } else {
    echo '';
    }?>

    Is it possible to place code within an if statement? Every tutorial that I've managed to find regarding "if" statements only uses commands like "echo," so I haven't been able to tell whether anything more complicated can be added. Any help would be appreciated.

  2. jabecker
    Member
    Posted 8 years ago #

    You can do conditional statements. Here's a link to info about conditional tags in the codex: http://codex.wordpress.org/Conditional_Tags

    I think is_category only works on the category archive page so that might not be your best bet.

  3. Paperninja
    Member
    Posted 8 years ago #

    Thank you for pointing me in the right direction. It looks as though is_category is only useful for the page itself(and thus probably only useful in the Archives), but the in_category looks as though it gives more freedom. I still can't seem to figure out where the php code is giving me a problem. Here's my current code:

    <?php if ( in_category(5) ) {
    echo '<?php wp_link_pages(); ?>
    <?php comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)')); ?>';
    } else {
    echo '';
    }
    ?>

    The third line down is line 26 of my code. The error message that I've been receiving is:
    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' (filepath to my index.php) on line 26

  4. jabecker
    Member
    Posted 8 years ago #

    try something like this (big disclaimer -- I haven't tried this so don't know if it will work)

    <?php
    if ( in_category(5) ) {
    wp_link_pages();
    comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)'));
    }
    ?>

    I don't think you really need the "else" because you're not doing anything in that case.

  5. Paperninja
    Member
    Posted 8 years ago #

    Hooray! Yes, that worked. Thank you very much for the assistance.

  6. imagiscapeca
    Member
    Posted 7 years ago #

    That works for WordPress's template tags like those used above

    AND for plugins' php calls, such as

    <?php if(is_single()) { the_ratings(); } ?>

    AND

    <?php if(is_single()) { punmail_form(InputLength, StackBelow); } ?>

    which is, without a condition:

    <?php punmail_form(InputLength, StackBelow) ?>

    (Post/Page Update Notification)

    Note the semi-colon in the_ratings(); . I couldn't get this to work because I was missing the semi-colon.

Topic Closed

This topic has been closed to new replies.

About this Topic