WordPress.org

Ready to get started?Download WordPress

Forums

Categories link does not work when posts are displayed by Wp_Query (2 posts)

  1. a_perfect_oblong
    Member
    Posted 5 years ago #

    Hi all,

    i've once again hit a road block in wordpress... right now i'm displaying my posts using WP_Query so that i can have control of displaying the posts by categories. On the sidebar is where i have the list of the categories i have ( <?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=0'); ?> ) .

    The problem: Everytime i click on the categories on my sidebar, it doesnt seem to load any of the articles from that category. it just displays the default posts when you load the website. i tried taking out the wp_query code and used the regular "if /while have posts code" and the category links worked pretty well.

    i'll attach the code that i have:

    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('cat=3&showposts=10'.'&paged='.$paged);
    ?>

    <?php while ($wp_query->have_posts()) : $wp_query->the_post();
    $img = get_post_meta($post->ID, 'img', $single = true);
    $img_class = get_post_meta($post->ID, 'img class', $single = true);
    ?>

    <div id="headline" class="clearfix">
    <div class="headline_box">
    <div id="headline_title" class="clearfix">
    <div class="headline_title_left"><h2>"><?php the_title(); ?></h2></div>
    <div class="headline_title_right"><h3><?php the_date(); ?> <?php the_time(); ?></h3></div>
    </div>
    <div class="headline_content_box">
    <div class="img_box">
    <?php if($img !== '') { ?><img src="<?php echo $img; ?>"class="<?php if($img_class !== '') { echo $img_class; } else { echo "left"; } ?>" border="1" /><?php } else { echo '<div class="no_image"><p>No Image Available.</p></div>'; } ?>
    </div>
    <div class="text_box"><p><?php wswwpx_content_extract ( 'Read more', 55); ?></p>
    </div>
    </div>
    </div>
    </div>
    <div class="spacer"></div>
    <?php endwhile; ?>
    </div>
    <div id="sidebar_wrap">

    <div class="sidebar_content_box">
    <h1>Boxing Headline Archives</h1>

    <ul class="archive-list">

  2. <?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=0'); ?>
  3. </div>

    So anyone with any suggestions or help will be deeply appreciated. Thanks guys!

  • stvwlf
    Member
    Posted 5 years ago #

    Hi - In your existing code, you are using the variable $paged, but it is not defined anywhere, therefore its value is nothing, so no paging is occurring.
    $wp_query->query('cat=3&showposts=10'.'&paged='.$paged);

    The problem: Everytime i click on the categories on my sidebar, it doesnt seem to load any of the articles from that category. it just displays the default posts when you load the website.

    That is because that is exactly what this code

    $wp_query = new WP_Query();
    $wp_query->query('cat=3&showposts=10'.'&paged='.$paged);

    is telling WordPress to do. Every time that loop executes it displays the same posts in category 3.

    You don't need WP_query to do what you are trying to do. I don't totally get what page this is occurring on so I don't have a complete answer for you. I assume you are referring posts displaying on your front page.

    When you click a category link in the sidebar, you categories should be using the template file category.php if your theme has that file. If not you can make one by copying index.php as category.php. Take out the WP_Query code and revert back to the default WP loop. That should display your categories just fine.

    There are a few other issues but I will wait to hear the specifics of your situation before saying more.

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags