Advertisement
alchymyth

loops without duplicate over two templates

Jun 22nd, 2011
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. <?php get_header(); ?>
  2.  
  3.  
  4. <?php global $do_not_duplicate; //NEW DECLARATION in the header part of the code ?>
  5. <?php $do_not_duplicate = array();
  6. $my_query = new WP_Query(array('posts_per_page' => 3, 'post__in' => get_option( 'sticky_posts' ), 'caller_get_posts' => 1 ) );
  7. while ($my_query->have_posts()) : $my_query->the_post();
  8. $do_not_duplicate[] = $post->ID; ?>
  9.  
  10. <?php
  11. $category = get_the_category();
  12. if ($category) {
  13. echo '<a href="' . get_category_link( $category[0]->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category[0]->name ) . '" ' . '>' . $category[0]->name.'</a>';
  14. }
  15. ?>
  16.  
  17. <br>
  18.  
  19. <a href="<?php the_permalink() ?>" rel="bookmark" title="Read: <?php the_title(); ?>"><?php the_title(); ?></a>
  20.  
  21. <br>
  22.  
  23. <?php endwhile;?>
  24. <?php wp_reset_query(); ?>
  25.  
  26. <br>
  27. <br>
  28.  
  29. <?php global $do_not_duplicate; //NEW DECLARATION in te index.php part of the code ?>
  30.  
  31. <?php $my_query = new WP_Query(array('posts_per_page' => 4, 'post__not_in' => array_merge($do_not_duplicate, get_option( 'sticky_posts' )) ) );
  32. while ($my_query->have_posts()) : $my_query->the_post();
  33. $do_not_duplicate[] = $post->ID; ?>
  34.  
  35. <a href="<?php the_permalink() ?>" title="Read: <?php the_title(); ?>"><?php the_title(); ?></a><br>
  36. <br>
  37.  
  38. <?php endwhile;?>
  39. <?php wp_reset_query(); ?>
  40.  
  41. <br>
  42. <br>
  43.  
  44. <?php
  45. //for each category, show 1 post
  46. $cat_args=array(
  47. 'orderby' => 'name',
  48. 'order' => 'ASC'
  49. );
  50.  
  51. $categories=get_categories($cat_args);
  52. foreach($categories as $category) {
  53. $args=array(
  54. 'numberposts' => 1,
  55. 'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
  56. 'category__in' => array($category->term_id)
  57. );
  58.  
  59. $posts=get_posts($args);
  60. if ($posts) {
  61. $ncat = '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
  62. foreach($posts as $post) {
  63. setup_postdata($post);
  64. $do_not_duplicate[] = $post->ID; ?>
  65.  
  66. <a href="<?php the_permalink() ?>" title="Read: <?php the_title(); ?>"><?php the_title(); ?></a><br>
  67. <br>
  68. <?php the_excerpt(); ?><br>
  69. <br>
  70. By <?php the_author_link(); ?>, <?php the_time('F j, Y'); ?> | In: <?php echo $ncat ?><?php comments_popup_link('<b>No Comments</b>', '<b>1 Comment</b>', '<b>% Comments</b>', '', '<b>Comments off</b>'); ?><br>
  71. <br>
  72.  
  73. <?php
  74. } // foreach($posts
  75. } // if ($posts
  76. } // foreach($categories
  77. ?>
  78.  
  79. <?php wp_reset_query(); ?>
  80.  
  81. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement