ThemeShaper Forums » Thematic

[closed]

multiple loops on home page using get_posts()

(5 posts)
  • Started 4 years ago by tripper54
  • Latest reply from mrsteveman1
  • This topic is resolved
  1. Hi there,

    I'm trying to set up 2 boxes on my home page (index.php). The first box should show one post (title and contents) from the category "newsgrabs" , the second box should show titles only of the 5 most recent posts NOT in the category "newsgrabs". I'm trying to do this by resetting thematic_indexloop, then replacing with my custom function in my child theme's functions.php.

    The custom function calls get_posts() twice, once for each box.

    The problem I'm having is that the output seems to get mixed up between the 2 boxes. The first box shows the post contents from the correct post, but the title and permalink are wrong. The second box shows the same title and permalink 5 times.

    Here is my code:

    // Remove default  index loop
    function remove_thematic_indexloop() {
    	remove_action('thematic_indexloop', 'thematic_index_loop');
    	}
    add_action('init','remove_thematic_indexloop');
    
    function agribusiness_newsgrab() {
    
    	echo "<div id='newsgrab'>";
    		$posts_news = get_posts( "category=23&numberposts=1" );
    		if( $posts_news ) {
            	foreach( $posts_news as $post_n ) {
    				setup_postdata( $post_n );
    				echo '<h2><a href="';
    				 the_permalink();
    				 echo '" title="Permanent Link to ';
    				 the_title();
    				  echo '">' ;
    				 the_title();
    				  echo '</a></h2>';
    					the_content("Continue reading '" . the_title('', '', false) . "'");
    				}
    			}
    	echo "</div>";
    
    	echo "<div id='recentlist'>";
    		$posts_r = get_posts( "category=-23&numberposts=5" );
    		if( $posts_r ) {
            	foreach( $posts_r as $post_r ) {
    				setup_postdata( $post_r );
    				echo '<h2><a href="';
    				 the_permalink();
    				 echo '" title="Permanent Link to ';
    				 the_title();
    				  echo '">' ;
    				 the_title();
    				  echo '</a></h2>';
    					//the_content("Continue reading '" . the_title('', '', false) . "'");
    				}
    			}
    	echo "</div>";
    
    	echo "39";
    
    	}
    add_action ('thematic_indexloop','agribusiness_newsgrab');

    You can see the output at http://agribusiness-australia.com.au/wp2009/

    I read the info at http://codex.wordpress.org/The_Loop#Multiple_Loops , and tried using the methods listed there to overcome the problem, but no dice.

    Also tried the technique described at http://perishablepress.com/press/2008/09/01/multiple-loops-and-multiple-columns-with-wordpress/ . It also seemed scramble the post output.

    I tried disabling all plugins. No dice.

    I'm sorry if this is not directly a Thematic issue. It could be. I just dont know...

    Does anyone have any clues what is happening here? I'm going crazy over this...

    Thanks in advance,

    Phil

    Posted 4 years ago #
  2. Hello,

    A fresh pair of eyes gave me what I needed to solve this - YAY, thanks James!

    Here's the working code:

    function agribusiness_newsgrab() {
    
    	echo "<div id='newsgrab'>";
    		$posts_news = get_posts( "category=23&numberposts=1" );
    		if( $posts_news ) {
            	foreach( $posts_news as $post_n ) {
    				setup_postdata( $post_n );
    				echo '<h2>';
    				 echo (get_the_title($post_n->ID));
    				  echo '</h2>';
    					the_content("Continue reading '" . the_title('', '', false) . "'");
    				}
    			}
    	echo "</div>";
    
    	echo "<div id='recentlist'>";
    		$posts_r = get_posts( "category=-23&numberposts=5" );
    		if( $posts_r ) {
            	foreach( $posts_r as $post_r ) {
    				setup_postdata( $post_r );
    				echo '<h2><a href="';
    				 echo (get_permalink($post_r->ID));
    				 echo '" title="Permanent Link to ';
    				 echo (get_the_title($post_r->ID));
    				  echo '">' ;
    				 echo (get_the_title($post_r->ID));
    				  echo '</a></h2>';
    					//the_content("Continue reading '" . the_title('', '', false) . "'");
    				}
    			}
    	echo "</div>";
    
    	}
    add_action ('thematic_indexloop','agribusiness_newsgrab');
    Posted 4 years ago #
  3. lqunta
    Member

    Hey Tripper54,

    Does the above example affect all pages? or just the home loop?

    Cheers

    Posted 4 years ago #
  4. lqunta
    Member

    Hello,
    There's a problem with the above code. I've tried it out, and, it duplicates everything on home.

    [i.e. the 'normal' loop, and, the titles from the above code are displayed]

    Cheers

    Posted 4 years ago #
  5. mrsteveman1
    Member

    I suspect you need to remove your existing index loop before replacing it with the above code, no?

    Posted 4 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.