Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Template Name: Test Page
- */
- get_header(); ?>
- <div class="expandable-collapsible-posts">
- <ul class="categories-list">
- <?php
- /**
- * First we need to get_categories
- * see https://developer.wordpress.org/reference/functions/get_categories/ for reference
- */
- $cat_args = array(
- 'orderby' => 'name',
- 'order' => 'ASC'
- );
- $categories = get_categories($cat_args); ?>
- <?php
- foreach($categories as $category) {
- $args = array(
- 'showposts' => 2, // limit to maximum of 2 posts per category
- 'category__in' => array($category->term_id),
- 'caller_get_posts' => 1 // turn sticky post off
- );
- $posts = get_posts($args); ?>
- <!-- each list item will contain 1 category + 2 posts from that category -->
- <li class="categories-list-item">
- <?php
- // see https://developer.wordpress.org/reference/functions/get_posts/ for reference
- if ($posts) {
- echo '<h2 class="category-name">Category: ' . $category->name . ' </h2>';
- foreach($posts as $post) {
- setup_postdata($post); ?>
- <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
- <h3><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
- <p><?php the_excerpt() ?></p>
- </article>
- <?php
- } // foreach ($posts
- } // if ($posts
- ?>
- </li> <!-- .categories-list-item -->
- <?php
- } // foreach($categories
- ?>
- </ul> <!-- .categories-list -->
- </div><!-- #expandable-collapsible-posts-->
- <?php
- get_footer();
Add Comment
Please, Sign In to add comment