WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] echo conditional tag depending on conditional tag? (7 posts)

  1. dl33
    Member
    Posted 8 years ago #

    Is it possible to echo php code, depending which page you are on. I do not want to echo a whole block of text but only a conditional tag which is wrapped around my loop.
    This is the conditional tag that I want to print, but only, if I am on the home page:
    <?php if (!(in_category('6')) ) { ?>THE LOOP<?php } ?>
    How do I do this? Do I simply put my conditional tag in an if statement like so:
    if (is_home()) {
    echo "if (!(in_category('6')) ) {";
    }THE LOOP
    if (is_home()) {
    echo "}";
    }

    Or do I have to do it another way?
    Thanks dl33

  2. Kafkaesqui

    Posted 8 years ago #

    <?php if ( is_home() && !in_category('6') ) { ?>

    THE LOOP CONTENT

    <?php } ?>

  3. dl33
    Member
    Posted 8 years ago #

    Yup, I thought about that, but if the page is not home, then the loop will not be gone through. Yet I also want the loop to show if the page is not home. The loop should only be wrapped in the !in_category('6') conditional if I am on the home page, otherwise not. With the above solution the loop wouldn't display it at all on those other pages.
    dl33

  4. Karrde
    Member
    Posted 8 years ago #


    <?php if ( ((is_home()) AND (in_category('6'))) OR (!is_home()) ) { ?>

    THE LOOP CONTENT

    <?php } ?>


    ...I think. It took me a while to work those brackets out. My brain hurts.

  5. Kafkaesqui

    Posted 8 years ago #

    Slightly easier to read version of karrde's:

    <?php if ( (is_home() && in_category('6')) || !is_home() ) { ?>

    A little crazy with the parens :). And to iterate through the if statement in plain language: If we're on the home page but in category 6, *or* not on the home page at all.

  6. Karrde
    Member
    Posted 8 years ago #

    Yeah, I had to re-edit the post a couple of times cus those parens confused the bejeezus out of me.

    The order is important though, for precedence's sake.

  7. dl33
    Member
    Posted 8 years ago #

    Thanks a lot... This saves me multiple loops and else statements. In the end I actually used this though:
    <?php if ( (is_home() && !in_category('6')) || !is_home() ) { ?>
    It works perfect...(although the brackets are making me dizy as well;-)

Topic Closed

This topic has been closed to new replies.

About this Topic