• Resolved csleh

    (@csleh)


    I’m using a plugin to assign categories to pages. Works beautifully. On the page template I’m able to display css according to the category. There is a sidebar that is included and I’d like to display a certain image depending on what category the page or post is in.

    This is the code that works on the page template:

    ‘<?php if ( in_category(9) ) {
    echo ‘thing’;
    } elseif ( in_category(8) ) {
    echo ‘thing2’;
    } else {
    echo ‘something else’;
    }
    ?>’

    but it breaks the display when put in the sidebar.

    Have searched and tried lots of things for getting the current category outside the loop but am having the devil of a time. Help greatly appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter csleh

    (@csleh)

    I forgot I can add links in this forum. You can see what I mean here:
    https://www.puritanautomation.com/datafiles/?page_id=14

    the two left columns — the second (inner) one has a different background color depending on category. I’m trying to get the photo in the first one differnt depending on category as well. THat info is in sidebar-left.php

    Perhaps there is a way to query the post, get the category, then do an echo?

    Thread Starter csleh

    (@csleh)

    OK, I don’t need to do this — was overthinking the issue. I found some conditional tags that do the trick BUT if I try to use an image instead of just text, the site below the header comes back blank. For every page, even the ones in a different category.

    So new question — how can I use an image in a conditional comment as seen below?

    <?php
    // let's generate info appropriate to the page being displayed
    if (in_category(9)) {
             echo "<p>we are in category automation</p>";
    } elseif (in_category(8)) {
             echo "<p>we are in category test</p>";
    } else {
           echo "<p>this is anything else</p>";
    } // That's all, folks!
    ?>

    I’ve tried:
    echo "<img src="<?php bloginfo('template_directory'); ?>/images/brose1.jpg" alt="test system image" />";

    <img src="<?php bloginfo('template_directory'); ?>/images/brose1.jpg" alt="test system image" />; (no echo)

    `echo “<img src=”fullurlcopiedfrombrowser.jpg” />;

    The image does display — if you visit the site you’ll see the text is on top of that image. So link seems correct.

    Thread Starter csleh

    (@csleh)

    looking for something completely different, found this on “the undersigned” blog:

    `<?php if ( is_single(’slug’) ) { ?>
    <img src=”” alt=”” / />
    <?php } ?>’

    works like a charm!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get category outside the loop — code difficulty’ is closed to new replies.