• Resolved lifeflame

    (@lifeflame)


    Hi, I’ve been trying for a few weeks to tweak my sidebar. Here’s what I really want:

      I want the main + single pages to list 5 recent posts + 5 others from the category of the page(that have not already been listed in recent).
      I want the category pages to list all the posts from the relevant category and then 5 other recent posts (that were not already part of the category list).

    I’ve been trying a combination of widgets and codes, but don’t seem to be able to get what I want. I’ve been figuring out PHP as I go along, but obviously don’t have enough experience with it. It’s the combination of eliminating overlap + list of same category that is throwing me.

    Can anyone help me with this sidebar issue?


    Experiments that I have already tried — and oh, I have tried all week:

    I’ve tried the code suggested which lists posts from the same category as the main post, but for some reason the sidebar will stay stuck in the category of the most recent post. (There’s probably a reset line that I need)

    The closest I’ve got is a “Category Posts” plugin plus a Widget context which would probably get me close enough, if the widget context was working properly but I can’t seem to get it to recognize my category posts. In any case, none of these allow me to eliminate the overlap between the recent and the category posts.

    many thanks…

Viewing 3 replies - 1 through 3 (of 3 total)
  • I want the category pages to list all the posts from the relevant category and then 5 other recent posts (that were not already part of the category list).

    <?php
    $used_ids = array();
    if ( is_category() ) {
      $current_cat = get_query_var('cat');
      $args=array(
        'cat' => $current_cat,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo 'All Posts in category ' . $current_cat;
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        $used_ids[] = $my_query->post->ID;
        endwhile;
      }
      $args=array(
        'post__not_in' => $used_ids,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 5,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo 'List of 5 recent Posts (none All Posts in category)';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        endwhile;
      }
    wp_reset_query();  // Restore global post data stomped by the_post().
    }  // if (is_category())
    ?>
    Thread Starter lifeflame

    (@lifeflame)

    MichealH-

    You rock the world. Thank you.
    I think I can figure out the main + single pages from this. Add the formatting, and I’m set.

    I knew there were people out there where writing this would be a buzz.
    =)

    Thread Starter lifeflame

    (@lifeflame)

    Hi Michael,

    I’m having some trouble styling the code you sent me.
    There are basically two main errors that I can’t seem to fix.

    1. One is the fact that I can’t get a space before the “Other Recent Posts”.
    I’ve tried adding <p>, changing the <h2> style before to get soem padding before, inserting “\n” into the echo but with no avail. When I inspect the element in google chrome it seems that it’s considered one block with the list above.

    2. Is that I’ve been trying to insert an ‘if..else’ in the code so that I can have an alternative sidebar for not non-category archive pages. However, I’m having syntax errors. I think it may have to do with the ?> right after it but I can’t figure out exactly what.

    Perhaps you can help shed light on this?
    thanks…

    <div id="contentright">
    
    <div class="title">
    &nbsp</p>
    <h1><a href="<?php echo get_option('home'); ?>/">Hofan is Home</a></li></h1>
    <div id="sidebar">
    </p>_____________________</p>
    
    <?php
    $used_ids = array();
    if ( is_category() ) {
      $current_cat_title = single_cat_title("", false);
      $current_cat = get_query_var('cat');
      $args=array(
        'cat' => $current_cat,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo "<h2>$current_cat_title</h2>";
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div class="recentpostswrap">										<div class="recentpoststitle">
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Go to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    </div><li type=square></div>
          <?php $used_ids[] = $my_query->post->ID;
        endwhile;
      }
    
      $args=array(
        'post__not_in' => $used_ids,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 99,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo'<h2>Other recent posts</h2>';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div class="recentpostswrap">										<div class="recentpoststitle">
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    </div><li type=square></div>
    
          <?php
        endwhile;
      }
    wp_reset_query();  // Restore global post data stomped by the_post().
    }  // if (is_category()) else {
    ?></ul>
    
    			<div id="recentposts">
    				<h2>Recent Posts</h2>
    
    <ul>
    				<?php query_posts('showposts=12'); ?>
    					<?php while (have_posts()) : the_post(); ?>
    						<div class="recentpostswrap">
    
    							<div class="recentpoststitle">
    								<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent link to'); ?> <?php the_title(); ?>"><?php the_title(); ?></a>
    </div><li type=square>
    
    						</div>
    				<?php endwhile;?></ul>
    </div>}
    
    </p>
    <a href="https://www.hofan.burntmango.org/journal/current/about/"><img src="https://www.hofan.burntmango.org/images/16.gif" align="right" height="150" border-style="none" title="About Hofan"/></a>
    
    <div id="widgets">
    		<?php 	/* Widgetized sidebar, if you have the plugin installed. */
    				if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
    		<?php endif; ?>
    </p>
    </div>
    		<div id="searchformsidebar">
    			<?php include (TEMPLATEPATH . '/searchform.php'); ?>
    		</div>
    </div>
    </div>

    here is an example of how it looks now, without the else:
    https://www.hofan.burntmango.org/journal/current/category/writing/
    (it won’t run with the else in there)

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘recent posts posts from same category with no overlap’ is closed to new replies.