WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] problem with in_category (4 posts)

  1. csleh
    Member
    Posted 6 years ago #

    In a sidebar, the goal is to have this:

    if in category a or in category b,
    show a list of category a with "category a" on top, and the show a list of category b with "category b" on top.

    the wording for "category a" and "category b" will be styled differently.

    I've tried many variations, this one almost worked but keeps showing the same post content for every different post.

    <?php if (in_category('5') || in_category ('6')) : ?>
            <ul class="lineabove title notopmargin">
           <li>Markit News</li></ul>
    <?php $my_query = new WP_Query('cat=5&showposts=15'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <ul><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li></ul>
    <?php endwhile; ?>
    <?php endif; ?>
     <?php if (in_category('6')) : ?>
            <ul class="lineabove title notopmargin">
            <li>Client News</li></ul>
    <?php $my_query = new WP_Query('cat=6&showposts=15'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <ul><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li></ul>
    <?php endwhile; ?>
    <?php endif; ?>

    Even having just one of these loops messes it up.

  2. Justin Tadlock

    Posted 6 years ago #

    in_category() checks if a post is within a specific category.

    I think maybe you're wanting to use is_category(), which checks if you're on a particular category's page.

  3. csleh
    Member
    Posted 6 years ago #

    Not exactly (these are single posts), but I might be thinking about this backwards.

    Both category a and b are news items. Perhaps I need to make them children category of a master "news" category. Then in this sidebar I can display a list of all child categories and posts within those categories of the master "news" category.

    At that point perhaps the code can be the within-the-loop kind. Will investigate this now.

    Any tips or alternate ways of approaching the problem appreciated though.

  4. csleh
    Member
    Posted 6 years ago #

    Okay, I had to drop the "if in_category" part, and put the code right into the single post code (no link to sidebar)

    Used this:

    <?php $my_query = new WP_Query('cat=6&showposts=15'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <ul><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li></ul>
    <?php endwhile; ?>
    <ul class="lineabove title notopmargin"><li>Client News</li></ul>
    <?php $my_query = new WP_Query('cat=5&showposts=15'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <ul><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li></ul>
    <?php endwhile; ?>

    BUT had to make sure the beginning of page loop was AFTER the above. Works a treat!

Topic Closed

This topic has been closed to new replies.

About this Topic