ThePixelMe

wp support - expandable/collapsible posts - tpl-test.php

Mar 19th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Template Name: Test Page
  4.  */
  5. get_header(); ?>
  6.  
  7. <div class="expandable-collapsible-posts">
  8.     <ul class="categories-list">
  9.  
  10.     <?php
  11.  
  12.     /**
  13.      * First we need to get_categories
  14.      * see https://developer.wordpress.org/reference/functions/get_categories/ for reference
  15.      */
  16.  
  17.     $cat_args = array(
  18.         'orderby' => 'name',
  19.         'order' => 'ASC'
  20.     );
  21.  
  22.     $categories = get_categories($cat_args); ?>
  23.  
  24.         <?php
  25.         foreach($categories as $category) {
  26.             $args = array(
  27.                 'showposts' => 2, // limit to maximum of 2 posts per category
  28.                 'category__in' => array($category->term_id),
  29.                 'caller_get_posts' => 1 // turn sticky post off
  30.             );
  31.  
  32.             $posts = get_posts($args); ?>
  33.  
  34.             <!-- each list item will contain 1 category + 2 posts from that category -->
  35.             <li class="categories-list-item">
  36.                 <?php
  37.                 // see https://developer.wordpress.org/reference/functions/get_posts/ for reference
  38.                 if ($posts) {
  39.                     echo '<h2 class="category-name">Category: ' . $category->name . ' </h2>';
  40.  
  41.                     foreach($posts as $post) {
  42.                         setup_postdata($post); ?>
  43.  
  44.                         <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  45.                             <h3><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
  46.                             <p><?php the_excerpt() ?></p>
  47.                         </article>
  48.  
  49.                         <?php
  50.                     } // foreach ($posts
  51.                 } // if ($posts
  52.                 ?>
  53.             </li> <!-- .categories-list-item -->
  54.  
  55.             <?php
  56.         } // foreach($categories
  57.         ?>
  58.     </ul> <!-- .categories-list -->
  59.  
  60. </div><!-- #expandable-collapsible-posts-->
  61.  
  62. <?php
  63. get_footer();
Add Comment
Please, Sign In to add comment