Advertisement
srikat

Untitled

Jan 29th, 2015
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.12 KB | None | 0 0
  1. add_action( 'genesis_after_sidebar_widget_area', 'sk_show_cpt_same_cat' );
  2. function sk_show_cpt_same_cat() {
  3.     if ( ! is_singular( 'post' ) ) {
  4.         return;
  5.     }
  6.  
  7.     $categories = get_the_category();
  8.  
  9.     $cat_slugs = '';
  10.    
  11.     foreach( $categories as $category ) { // concatenate
  12.         $cat_slugs .= ',' . $category->category_nicename;
  13.     }
  14.  
  15.     echo '<section class="widget"><div class="widget-wrap">';
  16.  
  17.     // WP_Query arguments
  18.     $args = array (
  19.         'post_type'              => 'movies',
  20.         'category_name'          => $cat_slugs,
  21.         'posts_per_page'         => '2',
  22.     );
  23.  
  24.     // The Query
  25.     $query = new WP_Query( $args );
  26.  
  27.     // The Loop
  28.     if ( $query->have_posts() ) {
  29.         echo '<ul>';
  30.         while ( $query->have_posts() ) {
  31.             $query->the_post();
  32.             // do something
  33.             ?>
  34.             <!-- Display the Title as a link to the Post's permalink. -->
  35.             <li>
  36.                 <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
  37.             </li>
  38.     <?php
  39.         }
  40.         echo '</ul>';
  41.     } else {
  42.         // no posts found
  43.     }
  44.  
  45.     // Restore original Post Data
  46.     wp_reset_postdata();
  47.  
  48.     echo '</div></section>';
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement