Support » Fixing WordPress » Creating a popular and recent loop

  • cmccarra

    (@connor-mccarra)


    Hi,
    I want to create a loop on the index showing 10 of the most popular posts, then 10 of the most recently added posts. Here’s what I’ve got so far:

    <?php /* If there are no posts to display, such as an empty archive page  */ ?>
    <?php if (have_posts()) : ?>
    
    <?php /* Display navigation to next/previous pages when applicable  */ ?>
    <?php if (  $wp_query->max_num_pages > 1 ) : ?>
    	<div id="nav-above" class="navigation">
            <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older') ); ?></div>
            <div class="nav-next"><?php previous_posts_link( __( 'Newer <span class="meta-nav">&rarr;</span>') ); ?></div>
        </div><!-- #nav-below -->
    <?php endif; ?>
    
    	<div id="sort"><h2>Popular</h2>
        <?php $postno = 1; ?>
    	  <?php $my_query = new WP_Query('akpc_most_popular');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;?>
    	<div class="box">
    
        	<?php
    		if ( has_post_thumbnail() ){ ?>
    			<?php $thumbID = get_post_thumbnail_id($post->ID); ?>
                <a href="<?php echo wp_get_attachment_url($thumbID); ?>" rel="gallery" title="<?php the_title(); ?>">
                    <?php the_post_thumbnail(); ?>
                    <span class="view-large"></span>
                </a>
            <?php } ?>
    
            <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
            <?php the_content('<p>Continue Reading &rarr;</p>'); ?>
            <?php edit_post_link('Edit this post'); ?>
        </div>
        <?php if ($postno == 1) { ?><div class="box"><div align="center">
    <script type="text/javascript"><!--
    google_ad_client = "ca-pub-0263400590288735";
    /* Image Ads - Medium rectangle */
    google_ad_slot = "5580641477";
    google_ad_width = 300;
    google_ad_height = 250;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    </div></div><div class="box"><?php } ?>
    <?php if ($postno == 1) { ?><div align="center">
    <script type="text/javascript"><!--
    google_ad_client = "ca-pub-0263400590288735";
    /* Image Ads - Medium rectangle */
    google_ad_slot = "5580641477";
    google_ad_width = 300;
    google_ad_height = 250;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    </div></div><?php } ?>
    <?php $postno++; ?>
        <?php endwhile; ?></div>
    <div id="sort">
    <h2>Recent</h2>
    <?php if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate ) continue; ?>
      <div class="box">
    
        	<?php
    		if ( has_post_thumbnail() ){ ?>
    			<?php $thumbID = get_post_thumbnail_id($post->ID); ?>
                <a href="<?php echo wp_get_attachment_url($thumbID); ?>" rel="gallery" title="<?php the_title(); ?>">
                    <?php the_post_thumbnail(); ?>
                    <span class="view-large"></span>
                </a>
            <?php } ?>
    
            <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
            <?php the_content('<p>Continue Reading &rarr;</p>'); ?>
            <?php edit_post_link('Edit this post'); ?>
        </div>
    
     <?php endwhile; endif; ?>
        </div><!-- #sort -->

    Though I’m no php coder so I could see there being a lot of mistakes in that ;). Any help appreciated.
    Here’s the link

  • The topic ‘Creating a popular and recent loop’ is closed to new replies.