Advertisement
alchymyth

list category posts page template v1.1

Aug 17th, 2011
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.45 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Template Name: List Category Posts Page
  4.  *
  5.  * a page template to incorporate the 'List Category Posts' functionality
  6.  * v1.1
  7.  * @package WordPress
  8.  * @subpackage Twenty Ten / Weaver2010
  9.  * @since 3.0.0
  10.  * adapted by Michael Stolze / aka alchymyth 08/2011
  11.  *
  12.  * instruction for the 'shortcode':
  13.  * syntax: [catlist id=3,7 numberposts=-1 excerpt=yes]
  14.  * id: a comma separated list of category ids;
  15.  * numberposts: the number of posts per page;
  16.  * excerpt: defaults to 'yes', any different values are ignored.
  17.  *
  18.  * Regardless of the 'shortcode' position in the content of the page,
  19.  * it will be shown at the bottom of the page; all other page content wil be shown at the top.
  20.  */
  21. ?>
  22. <?php if(function_exists('catlist_func')) remove_shortcode( 'catlist', array('ListCategoryPosts', 'catlist_func') ); ?>
  23.    
  24. <?php get_header(); ?>
  25.     <div id="container">
  26.     <?php ttw_put_ttw_widgetarea('top-widget-area','ttw-top-widget','ttw_hide_widg_pages'); ?>
  27.     <div id="content">
  28.  
  29.         <?php the_post(); ?>
  30.         <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  31.            
  32.             <h1 class="entry-title"><?php the_title(); ?></h1>
  33.            
  34.             <div class="entry-content">
  35.                 <?php // remove the 'catlist' shortcode section from the page content //
  36.                 $the_content = preg_replace('~\[catlist([^\[\]]+)\]~is', '', get_the_content());
  37.                 echo apply_filters('the_content', $the_content); ?>
  38.                 <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', TTW_TRANS ), 'after' => '</div>' ) ); ?>
  39.                 <?php edit_post_link( __( 'Edit', TTW_TRANS ), '<span class="edit-link">', '</span>' ); ?>
  40.             </div><!-- .entry-content -->
  41.        
  42. <?php //start of 'List Category Posts' section; // ?>
  43.  
  44.             <?php //get the shortcode values from the page content, by using a regex //
  45.             preg_match_all('~\[catlist([^\[\]]+)\]~is', get_the_content(), $short); $short = $short[1];
  46.             $shortcode = $short[0];
  47.             preg_match_all('~id=([0-9,]+)\b~is', $shortcode, $id);
  48.             $cat_ids = $id[1][0]; $cat_id = explode(',', $cat_ids);
  49.             preg_match_all('~numberposts=(-([0-9]+)|([0-9]+))\b~is', $shortcode, $numberposts);
  50.             $cat_numbers = $numberposts[1][0];
  51.             if(!$cat_numbers) $cat_numbers = get_option('posts_per_page');
  52.             preg_match_all('~excerpt=(yes|no)\b~is', $shortcode, $excerpt);
  53.             $cat_excerpt = $excerpt[1][0];
  54.            
  55.             //start the 'list category posts' query //         
  56.             $temp_post = $post; $temp = $wp_query; //save original query
  57.             $wp_query = new WP_query(array('category__in' => $cat_id, 'posts_per_page' => $cat_numbers, 'paged' => get_query_var('paged') ));
  58.             if( $wp_query->have_posts() ) : ?>
  59.             <div class="entry-content">
  60.             <ul class="lcp_catlist">
  61.             <?php while( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
  62.             <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  63.             <?php if($post->post_excerpt){ //rebuild the 'list catgory posts' excerpt
  64.                     $lcp_excerpt = $post->post_excerpt;
  65.             } else {
  66.             $lcp_excerpt = strip_tags($post->post_content); }
  67.             if ( post_password_required($post) ) {
  68.                     $lcp_excerpt = __('There is no excerpt because this is a protected post.');
  69.             }
  70.             if (strlen($lcp_excerpt) > 255) {
  71.                     $lcp_excerpt = substr($lcp_excerpt, 0, 252) . '...';
  72.             } ?>
  73.             <div class="lcp_excerpt"><?php echo $lcp_excerpt; ?></div>
  74.             </li><br /><br />
  75.             <?php endwhile; ?>
  76.             </ul>
  77.             </div><!-- .entry-content of category posts -->
  78.            
  79.         <?php if (  $wp_query->max_num_pages > 1 ) : ?>
  80.         <div id="nav-below" class="navigation">
  81.         <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', TTW_TRANS ) ); ?></div>
  82.         <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', TTW_TRANS ) ); ?></div>
  83.         </div><!-- #nav-below -->
  84.         <?php endif; // end of pagination ?>
  85.             <?php $wp_query = $temp; $post = $temp_post; //restore original query ?>
  86.             <?php endif; ?>
  87.            
  88. <?php //end of 'List Category Posts' section ?>
  89. <?php remove_shortcode( 'catlist', 'ListCategoryPostsArgs' );
  90. if(function_exists('catlist_func')) add_shortcode( 'catlist', array('ListCategoryPosts', 'catlist_func') ); ?>
  91.  
  92.             </div><!-- #post-<?php the_ID(); ?> -->
  93.        
  94.     </div><!-- #content -->
  95.     <?php ttw_put_ttw_widgetarea('bottom-widget-area','ttw-bot-widget','ttw_hide_widg_pages'); ?>
  96.     </div><!-- #container -->
  97. <?php get_sidebar(); ?>
  98. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement