Advertisement
Guest User

Broken loop

a guest
May 31st, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1. <?php
  2.             // First, let's eliminate some DRY,
  3.             // by making an array of our categories
  4.             $random_posts_cat_array = array( 'artists', 'projects', 'people', 'development', 'offsite', 'contact' );
  5.            
  6.             // Globalize $post,
  7.             // since we're outside the primary loop
  8.             global $post;
  9.             $post_cat = get_the_category( $post->ID );
  10.             // Current post category ID
  11.             $post_cat_id = $post_cat->cat_ID;
  12.             // Current post category slug
  13.             $post_cat_slug = $post_cat->slug;
  14.            
  15.             // Now, let's find out if we're displaying
  16.             // the category index for one of our categories
  17.             if ( in_array( $random_posts_cat_array, $post_cat_slug ) ) {
  18.            
  19.                 // Set up custom loop args
  20.                 $random_posts_query_args = array(
  21.                     'post_per_page' => 3,
  22.                     'orderby'       => 'rand'
  23.                 );
  24.                 // Add Cat ID to custom loop args
  25.                 foreach ( $random_posts_cat_array as $random_post_cat ) {
  26.                 if ( $post_cat_slug == $random_post_cat ) {
  27.                     // Add Cat ID
  28.                     $random_posts_query_args['cat'] = $post_cat_id;
  29.                 }
  30.            
  31.                 // Run random posts query
  32.                 $random_posts_query = new WP_Query( $random_posts_query_args );
  33.            
  34.                 // Setup random posts query loop
  35.                 if ( $random_posts_query->have_posts() ) : while ( $random_posts_query->have_posts() ) : $random_posts_query->the_post();>?>
  36.                
  37.                     <h2>Plop sticks</h2>
  38.                    
  39.                     <?php foreach( get_the_category() as $cat ) echo '<div class="module ' . $cat->slug . '" data-category="' . $cat->slug . '" >'; ?>
  40.                                    
  41.                           <a href="<?php the_permalink()?>" title="<?php the_title(); ?>">
  42.                           <div class="active">
  43.                                 <div class="hover"></div>
  44.                                 <?php the_post_thumbnail(); ?>
  45.                           </div>
  46.                          
  47.                          <?php
  48.                               $sub_title=get_post_meta($post->ID,'subtitle',true);
  49.                               if($sub_title != '') {
  50.                               echo '<h1>'. get_the_title() .'<span> / '. $sub_title .'</span></h1>';
  51.                               } else {
  52.                               echo '<h1>'. get_the_title() .'</h1>';
  53.                               }
  54.                               ?>
  55.                              
  56.                               <?php
  57.                               // Call in the contents of a custom field called Excerpt and if custom field in admin panel is empty don't display <p> tags otherwise wrap contents in <p> tags
  58.                               $excerpt=get_post_meta($post->ID,'Excerpt',true);
  59.                               if($excerpt != '') {
  60.                               echo '<p>'. $excerpt .'</p>';
  61.                               } else {
  62.                               echo ' ';
  63.                               }
  64.                               ?>
  65.                               <p class="date"><?php the_time('YdmHi') ?></p>
  66.                          </a>
  67.                     </div>
  68.                    
  69.                 <?php endwhile; endif;
  70.            
  71.                 // Be kind; rewind
  72.                 wp_reset_postdata();
  73.            
  74.             } else { ?>
  75.            
  76.                 <p>There is nothing to display</p>
  77.                
  78.             <?php } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement