WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Multiple loops in thematic child theme to display related posts (9 posts)

  1. roburdick
    Member
    Posted 4 years ago #

    Hello,

    I am trying to get two loops working on a category template in order to show some related posts (posts from the same category). I have read the codex pages and tried the suggested loops but it doesn't work for me. It usually repeats the same post rather than listing several other relevant ones.

    Here is how my category-3.php template is looking without my attempts at adding the second loop:

    '
    <?php

    // calling the header.php
    get_header();

    // action hook for placing content above #container
    thematic_abovecontainer();

    ?>

    <div id="container">
    <div id="content">

    <?php

    // displays the page title
    thematic_page_title();

    // create the navigation above the content
    thematic_navigation_above();

    // action hook for placing content above the category loop
    thematic_above_categoryloop();

    // action hook creating the category loop
    thematic_categoryloop();

    // action hook for placing content below the category loop
    thematic_below_categoryloop();

    // create the navigation below the content
    thematic_navigation_below();

    ?>

    </div><!-- #content -->
    </div><!-- #container -->

    <?php

    // action hook for placing content below #container
    thematic_belowcontainer();

    // calling the standard sidebar
    thematic_sidebar();

    // calling footer.php
    get_footer();

    ?>
    '

    Any help would be great. I have tried various plug-ins mostly not working for me.

    Thanks

  2. roburdick
    Member
    Posted 4 years ago #

    I think i should be using something like this:

    <?php $my_query = new WP_Query('category_name=featured&posts_per_page=1');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;?>
        <!-- Do stuff... -->
      <?php endwhile; ?>
        <!-- Do other stuff... -->
      <?php if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
       <!-- Do stuff... -->
      <?php endwhile; endif; ?>

    but every comnbination i try to cut and paste seems to fail. Please help!

  3. roburdick
    Member
    Posted 4 years ago #

    I have managed to get a second loop working and it's not duplicating (using post__not_in). Lost all my styling but I guess I can rebuild that. The major problem is that the second loop outputs too many posts and the first putputs the wrong one!

    <?php
    
        // calling the header.php
        get_header();
    
        // action hook for placing content above #container
        thematic_abovecontainer();
    
    ?>
    
    	<div id="container">
    		<div id="content">
    
    <?php
    // *****1ST LOOP*****
    ?>
    <?php
    query_posts('showposts=1');
    $ids = array();
    while (have_posts()) : the_post();
    $ids[] = get_the_ID();
    the_title();
    the_content();
    endwhile;
    ?>
    
    <?php
                // create the navigation above the content
    			thematic_navigation_above();
    
                // calling the widget area 'single-top'
                get_sidebar('single-top');
    
                // action hook creating the single post
    
                // calling the widget area 'single-insert'
                get_sidebar('single-insert');
    
                // create the navigation below the content
    			thematic_navigation_below();
    
                // calling the comments template
                thematic_comments_template();
    
                // calling the widget area 'single-bottom'
                get_sidebar('single-bottom');
    
                ?>
    
    		</div><!-- #content -->
    
    <?php
    // *****2ND LOOP*****
    ?>
    
    <?php
    query_posts(array('post__not_in' => $ids));
    while (have_posts()) : the_post();
    the_title();
    the_content();
    endwhile;
    ?>
    	</div><!-- #container -->
    
    <?php 
    
        // action hook for placing content below #container
        thematic_belowcontainer();
    
        // calling the standard sidebar
        thematic_sidebar();
    
        // calling footer.php
        get_footer();
    
    ?>
  4. roburdick
    Member
    Posted 4 years ago #

    <?php
    // *****1ST LOOP*****
    ?>
    <?php
    while (have_posts()) : the_post();
    	    $do_not_duplicate[] = $post->ID; // remember ID's in loop
    		?>
    			<div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
        			<?php thematic_postheader(); ?>
    				<div class="entry-content">
    <?php thematic_content(); ?>
    
    				</div>
    				<?php thematic_postfooter(); ?>
    			</div><!-- .post -->
    
    		<?php endwhile;?>
    <?php
    // *****2ND LOOP*****
    ?>
     <?php
    
    query_posts( array(
        'cat' => 3,
        'post__not_in' => $do_not_duplicate
        )
    );
    ?>
    <div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
        			<?php thematic_postheader(); ?>
    				<div class="entry-content">
    <?php thematic_content(); ?>
    
    				</div>
    				<?php thematic_postfooter(); ?>
    			</div><!-- .post -->

    The above attempts failed also.

  5. roburdick
    Member
    Posted 4 years ago #

  6. roburdick
    Member
    Posted 4 years ago #

    I tried changing the loop in the functions.php. No good either:

    <?php
      // Category-3 LOOP
    function remove_single_loop() {
      remove_action('thematic_singleloop', 'thematic_single_loop');
    }
    add_action('init', 'remove_single_loop');
    
    function snippet_single_loop() {
      global $post;
            /* Count the number of posts so we can insert a widgetized area */ $count = 1;
            while ( have_posts() ) : the_post() ?>
            <?php $do_not_duplicate[] = $post->ID; // remember ID's in loop ?>	
    
                <?php $counter++; ?>
                <div class="column <?php if ($counter == 1) { echo 'one'; } else { echo 'two'; $counter = 0; } ?>">
                    <div class="clear-fix">
                        <div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?>">
                            <?php thematic_postheader(); ?>
                            <div class="entry-content">
    <?php the_content(); ?>
    <?php wp_link_pages('before=<div class="page-link">' .__('Pages:', 'thematic') . '&after=</div>') ?>
                            </div>
                                <?php thematic_postfooter(); ?>
                        </div><!-- .post -->
                    </div><!-- .clear-fix -->
                </div><!-- .column -->
     <?php
    // *****2ND LOOP*****
    ?>
     <?php
     query_posts('showpost=1');
    query_posts( array(
        'cat' => 3,
        'post__not_in' => $do_not_duplicate
        )
    );
    ?>
    <div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
        			<?php thematic_postheader(); ?>
    				<div class="entry-content">
    <?php thematic_content(); ?>
    
    				</div>
    				<?php thematic_postfooter(); ?>
    			</div><!-- .post -->
    
                        <?php comments_template();
                        if ($count==$thm_insert_position) { get_sidebar('index-insert');}
    
            $count = $count + 1;
            endwhile;
    }
    add_action('thematic_singleloop', 'thematic_single_loop');
    ?>
  7. roburdick
    Member
    Posted 4 years ago #

    OK I think I managed to do it if anyone is interested here is the code for my custom category loop in the file category-3.php

    I replaced the this part:

    the_post();

    and this part for my second loop:

    thematic_sidebar();
    Here is my full code:

    <link rel="stylesheet" href="http://www.robertburdick.com/mysite/wp-content/themes/robertburdick.com/portfolio-single.css" type="text/css" media="screen" />
    <?php
    
        // calling the header.php
        get_header();
    
        // action hook for placing content above #container
        thematic_abovecontainer();
    
    ?>
    
    	<div id="container">
    		<div id="content">
                <?php // Get the last 2 posts in the featured category.?>
                  <?php query_posts('category_name=portfolio&showposts=1'); ?>
    <?php $my_query = new WP_Query('category_name=portfolio&showposts=1'); ?>
    
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
                <div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
        			<?php thematic_postheader(); ?>
    				<div class="entry-content">
    
    <?php thematic_content(); ?>
    
    <?php $ids[]= $post->ID; endwhile;?>
    
    					<?php wp_link_pages('before=<div class="page-link">' .__('Pages:', 'thematic') . '&after=</div>') ?>
    				</div>
    				<?php thematic_postfooter(); ?>
    			</div><!-- .post -->
    <?php
    
                // create the navigation above the content
    			thematic_navigation_above();
    
                // calling the widget area 'single-top'
                get_sidebar('single-top');
    
                // action hook creating the single post
    
                // calling the widget area 'single-insert'
                get_sidebar('single-insert');
    
                // create the navigation below the content
    			thematic_navigation_below();
    
                // calling the comments template
                thematic_comments_template();
    
                // calling the widget area 'single-bottom'
                get_sidebar('single-bottom');
    
                ?>
    
    		</div><!-- #content -->
    
    			</div><!-- .post -->
    
    <?php 
    
        // action hook for placing content below #container
        thematic_belowcontainer();
    
        // calling the standard sidebar
    ?>
    			 <?php rewind_posts(); ?>
                  <?php query_posts('category_name=portfolio&showposts=-1'); ?>
    
      <?php while (have_posts()) : the_post(); ?>
    <?php if (!in_array($post->ID, $ids)) { ?>
    
                <div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
        			<?php thematic_postheader(); ?>
    				<div class="entry-content">
    
    <?php thematic_content(); ?>
    
    <?php } endwhile;?>
    
    	<?php
        // calling footer.php
        get_footer();
    
    ?>

    and I put this in my functions.php to make the posts show in full not excerpt:

    // Category loop
    function childtheme_content($content) {
    	if (is_category()) {
    		$content= 'full';}
    	return $content;
    }
    add_filter('thematic_content', 'childtheme_content');
  8. roburdick
    Member
    Posted 4 years ago #

  9. roburdick
    Member
    Posted 4 years ago #

    Sorry actually this is my final code after I realised the first loop did not need any query-posts for my purposes:

    <link rel="stylesheet" href="http://www.robertburdick.com/mysite/wp-content/themes/robertburdick.com/portfolio-single.css" type="text/css" media="screen" />
    <?php
    
        // calling the header.php
        get_header();
    
        // action hook for placing content above #container
        thematic_abovecontainer();
    
    ?>
    
    	<div id="container">
    		<div id="content">
    
      <?php while (have_posts()) : the_post(); ?>
    
                <div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
        			<?php thematic_postheader(); ?>
    				<div class="entry-content">
    
    <?php thematic_content(); ?>
    
    <?php $ids[]= $post->ID; endwhile;?>
    
    					<?php wp_link_pages('before=<div class="page-link">' .__('Pages:', 'thematic') . '&after=</div>') ?>
    				</div>
    				<?php thematic_postfooter(); ?>
    			</div><!-- .post -->
    <?php
    
                // create the navigation above the content
    			thematic_navigation_above();
    
                // calling the widget area 'single-top'
                get_sidebar('single-top');
    
                // action hook creating the single post
    
                // calling the widget area 'single-insert'
                get_sidebar('single-insert');
    
                // create the navigation below the content
    			thematic_navigation_below();
    
                // calling the comments template
                thematic_comments_template();
    
                // calling the widget area 'single-bottom'
                get_sidebar('single-bottom');
    
                ?>
    
    		</div><!-- #content -->
    
    			</div><!-- .post -->
    
    <?php 
    
        // action hook for placing content below #container
        thematic_belowcontainer();
    
        // calling the standard sidebar
    ?>
    <div class="folio-menu">
    			 <?php rewind_posts(); ?>
                  <?php query_posts('category_name=portfolio&showposts=-1'); ?>
    
      <?php while (have_posts()) : the_post(); ?>
    <?php if (!in_array($post->ID, $ids)) { ?>
    
                <div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
        			<?php thematic_postheader(); ?>
    
            <?php the_post_thumbnail();  // we just called for the thumbnail ?>
    <?php wp_link_pages('before=<div class="page-link">' .__('Pages:', 'thematic') . '&after=</div>') ?>
    
    <?php } endwhile;?>
    </div>
    	<?php
        // calling footer.php
        get_footer();
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic