Support » Fixing WordPress » Embedding code within “if” statements

  • 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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You can do conditional statements. Here’s a link to info about conditional tags in the codex:

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

    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

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

    if ( in_category(5) ) {
    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.

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

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

    AND for plugins’ php calls, such as

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


    <?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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Embedding code within “if” statements’ is closed to new replies.