• I understood the explanation about using two query loops in the same page, but it’s far from my php skills to make multiple ones!

    I would like to show the first post for each category in a page’s page. i.e. not in a blog page, but in a page, which already used the query for displaying the requested page.

    The website is seosumo.com

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • You want to put this into a Page template

    <?php /*
    Template Name: FirstPostOfEachCategory
    */ ?>
    
    <?php get_header(); ?>
    
    <div id="content">
    <div id="main">
    
    	<?php // Here we output the actual content of the Page. If you need introductory text or something
    		if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<h1><?php the_title(); ?></h1>
    		<?php the_content(); ?>
    	<?php endwhile; else: endif; ?>
    
    	<?php // this is where the categories get cycled through and the latest X posts of each are displayed
    	$categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description, term_order from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE taxonomy = 'category' AND parent = 0 ORDER BY term_order ASC");
    
    	foreach ($categories as $category) { ?>
    		<h1>Category: <a href="<?php echo get_category_link($category->id); ?>"><?php echo $category->name; ?></a></h1>
    		<?php query_posts('category_name='.$category->name.'&showposts=1'); } ?>
    		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    			<?php the_title(); ?>
    			<!-- Style this any way you want -->
    		<?php endwhile; else: ?><p>Nix!</p><?php endif; ?>
    	</div>
    	<?php }	?>
    
    </div>
    </div>
    
    <?php get_footer(); ?>

    Thread Starter Zambrano Sergio

    (@sergiozambrano)

    Thank you for your reply!!

    Looks good!, but it doesn’t work.
    I tested it, and I even added a rewind_posts() function and nothing.

    (There was an extra <?php }?> in this script)

    Both queries show the same content: the page.

    See it at http://seosumo.com/test (that’s the only page that loads your script)
    The code goes as follow:

    <?php
    /*
    Template Name: Pages2
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="mainContent" class="showtab1">
      <div id="maincontent1">
          <h2 class="maintabs">
            <big><b>Sumo</b></big>
            <small><a href="javascript:void(0);" onclick="switch_main(2)">Latest Articles</a></small>
            <small><a href="javascript:void(0);" onclick="switch_main(3)">SEO Tools</a></small>
            </h2>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="entry" id="post-<?php the_ID(); ?>">
    
    <h1 class="typeface-js"><?php the_title(); ?></h1>
    	<?php the_content(__('(more...)')); ?>
    
    <?php endwhile; else: ?>
    	<div class="entry">
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    <!-- end #entry --></div>
    
          <div class="footer"><hr /></div>
      </div>
    
      <div id="maincontent2">
          <h2 class="maintabs">
            <small><a href="javascript:void(0);" onclick="switch_main(1)">Sumo</a></small>
            <big><b>Latest Articles</b></big>
            <small><a href="javascript:void(0);" onclick="switch_main(3)">SEO Tools</a></small>
            </h2>
    
    <?php // this is where the categories get cycled through and the latest X posts of each are displayed
    	$categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description, term_order from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE taxonomy = 'category' AND parent = 0 ORDER BY term_order ASC");
    
    	foreach ($categories as $category) { ?>
    		<?php rewind_posts(); query_posts('category_name='.$category->name.'&showposts=1'); } ?>
    		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<div class="entry multipost" id="post-<?php the_ID(); ?>">
    
    <?php the_date('','<span class="postinfo">','</span>'); ?>
    
    <div class="postheader">
    	<span class="postinfo"><?php the_date('','<h2>','</h2>'); ?></span>
    	<h1 class="typeface-js"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    	<span class="postinfo"><?php _e("Filed under:"); ?> <?php the_category(',') ?> — Posted by <?php the_author() ?> @ <?php the_time() ?> <?php edit_post_link(__('Edit This')); ?></span>
    </div>
    	<?php the_content(__('(more...)')); ?>
    
    <?php link_pages('<p class="pagenavigator"> ', '</p>', 'next', ' next page', 'previous page '); ?>
    
     	<div class="postfooter postinfo"><?php the_tags(__('Tags: '), ', ', ' — '); ?></div>
    
    <?php if (is_single()) : ?>
    <div class="footer"><hr /></div>
    <?php comments_template(); // Get wp-comments.php template ?>
    <?php endif; ?>
        <!-- end Entry --></div>
    
          <?php endwhile; else: ?>
    	<div class="entry">
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
        <!-- end Entry --></div>
    
    <?php endif; ?>
          <div class="footer"><hr /></div>
    
    <?php posts_nav_link(' — ', __('&laquo; Newer Posts'), __('Older Posts &raquo;')); ?>
          <!-- maincontent2 --></div>
    
            <div id="maincontent3">
          <h2 class="maintabs">
            <small><a href="javascript:void(0);" onclick="switch_main(1)">Sumo</a></small>
            <small><a href="javascript:void(0);" onclick="switch_main(2)">Articles</a></small>
            <big><b>SEO Tools</b></big>
            </h2>
    
      <div class="entry">
          <h1>Content 3 title</h1>
          <p>This page is under construction and it will be up soon.</p>
        <!-- end Content 3 --></div>
          <div class="footer"><hr /></div>
     <!-- end Content3 --></div>
    
     <!-- end mainContent --> </div>
    
    <?php include(TEMPLATEPATH.'/sidebars.php'); ?>
    
    <br class="clearfloat" />
    <!-- end #maincontainer --></div>
    
    <?php get_footer(); ?>

    Thread Starter Zambrano Sergio

    (@sergiozambrano)

    The juice of that code is this. Same posts is shown in both loops.

    <?php // this is where the categories get cycled through and the latest X posts of each are displayed
    	$categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description, term_order from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE taxonomy = 'category' AND parent = 0 ORDER BY term_order ASC");
    
    	foreach ($categories as $category) { ?>
    		<?php rewind_posts(); query_posts('category_name='.$category->name.'&showposts=1'); } ?>
    		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<div class="entry multipost" id="post-<?php the_ID(); ?>">
    
    <?php the_date('','<span class="postinfo">','</span>'); ?>
    
    <div class="postheader">
    	<span class="postinfo"><?php the_date('','<h2>','</h2>'); ?></span>
    	<h1 class="typeface-js"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    	<span class="postinfo"><?php _e("Filed under:"); ?> <?php the_category(',') ?> — Posted by <?php the_author() ?> @ <?php the_time() ?> <?php edit_post_link(__('Edit This')); ?></span>
    </div>
    	<?php the_content(__('(more...)')); ?>
    
    <?php link_pages('<p class="pagenavigator"> ', '</p>', 'next', ' next page', 'previous page '); ?>
    
     	<div class="postfooter postinfo"><?php the_tags(__('Tags: '), ', ', ' — '); ?></div>
    
    <?php if (is_single()) : ?>
    <div class="footer"><hr /></div>
    <?php comments_template(); // Get wp-comments.php template ?>
    <?php endif; ?>
        <!-- end Entry --></div>
    
          <?php endwhile; else: ?>
    	<div class="entry">
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
        <!-- end Entry --></div>
    
    <?php endif; ?>
          <div class="footer"><hr /></div>
    Thread Starter Zambrano Sergio

    (@sergiozambrano)

    First post from each category… did anybody get it working?

    Okay, there were some major bugs in my code (I copied it from a working theme file and tried to remove anything unnecessary. I guess I got rid of too much 😉 ) and you garbled up the rest by adding ifs and end ifs and comment crap that does not belong inside the post div.
    So, here is the important bit:

    <?php // this is where the categories get cycled through and the latest X posts of each are displayed
    	$categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description, term_order from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE taxonomy = 'category' AND parent = 0 ORDER BY term_order ASC");
    
    	foreach ($categories as $category) : ?>
    		<?php query_posts('category_name='.$category->name.'&showposts=1'); ?>
    		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    		<div class="entry multipost" id="post-<?php the_ID(); ?>">
    			<?php the_date('','<span class="postinfo">','</span>'); ?>
    			<div class="postheader">
    				<span class="postinfo"><?php the_date('','<h2>','</h2>'); ?></span>
    				<h1 class="typeface-js"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    				<span class="postinfo"><?php _e("Filed under:"); ?> <?php the_category(',') ?> — Posted by <?php the_author() ?> @ <?php the_time() ?> <?php edit_post_link(__('Edit This')); ?></span>
    			</div>
    			<?php the_content(__('(more...)')); ?>
    			<div class="postfooter postinfo"><?php the_tags(__('Tags: '), ', ', ' — '); ?></div>
        	</div><!-- end Entry -->
        	<?php endwhile; else: ?>
    			<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    		<?php endif; ?>
    	<?php endforeach; ?>

    No need for rewinding. Add this wherever you want the posts to appear.
    I suggest you use a “single.php” or similar page as a basis for your template, not the index.php since this usually includes a bunch of if then elses for various scenarios.

    Thread Starter Zambrano Sergio

    (@sergiozambrano)

    Thank you for your help!

    Is that code php4 dependent?
    I’m using php5 (as far as I know) and I’ve read there are some differences.

    I’m using that code in http://seosumo.com/test
    Remember, I can’t use “single.php” since this is supposed to be a second loop for a page which already loads “the page”.

    Nothing renders. Not even pasted in a generic page with no previous loop.
    1st loop works fine though: “page” loads fine in “sumo” tab, but your second loop doesn’t render a thing.

    🙁

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘First post of each category?’ is closed to new replies.