Advertisement
Guest User

related-posts.php

a guest
Aug 27th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.59 KB | None | 0 0
  1. <div class="column-b">
  2.             <span class="posts-title">Related Articles</span>
  3.  
  4.              <?php global $post;
  5.      
  6.                             $cat = get_the_category( $post->ID );
  7.                            
  8.                             if ($cat){    
  9.                                 $cat_id = $cat[0]->cat_ID;
  10.                             }
  11.                             else{
  12.                                 $cat_id = 1; // Тут задается дефолтная категория
  13.                             }
  14.  
  15.                             $args = array( 'numberposts' => '2', 'category' => $cat_id );
  16.                             $myposts = get_posts( $args );
  17.  
  18.                             foreach( $myposts as $post ) :    setup_postdata($post); ?>
  19.                            
  20.  
  21.             <div class="item-post">
  22.                 <?php
  23.                     $thumb = get_post_thumbnail_id();
  24.                     $img_url = wp_get_attachment_url( $thumb,'full' ); //get full URL to image (use "large" or "medium" if the images too big)
  25.                     $image = $img_url; //resize & crop the image
  26.        
  27.                 ?>
  28.        
  29.        
  30.                 <?php if($image) : ?>
  31.                     <div class="blog-item-img"><a href="<?php the_permalink(); ?>"><img alt="<?php the_title(); ?>"  src="<?php echo $image ?>"/></a></div>
  32.                 <?php endif; ?>
  33.  
  34.                 <h2>
  35.                     <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
  36.                 </h2>
  37.  
  38.                 <p><?php the_category(', ') ?></p>
  39.  
  40.                 <p class="post-desc"><?php echo excerpt(15); ?></p>
  41.  
  42.             </div>
  43.  
  44.              <?php endforeach; ?>
  45.  
  46. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement