Close
Menu

RTCXpression

Post Lists in WordPress – Shortcodes Make it Easy


May 21, 2017

Post lists in WordPress are easy to create once you know how to do it. I’m not fond of using shortcodes, but it’s better than using a plugin to embed PHP code in a post.

With shortcodes, we’re still using PHP but the processing happens outside of the post, before the post is even fetched from the database.

I’m going to give you some shortcodes I created and some template code I mangled.

Post Lists – Post List or Article List

Whatever you want to call them, a list of posts can easily be created with this shortcode:

function shortcode_post_list() {
  $posts_array = get_posts(
    array(
      'posts_per_page' => -1,
      'orderby' => 'date',
      'order' => 'DESC',
      'post_type' => 'post',
      'post_status' => 'publish'
    )
  );
  $output = '<ul>';
  foreach ($posts_array as $post) {
    $a = explode(' ', $post->post_date);
    $output .= '<li><a href="' . home_url('/') . $post->post_name . '.html">' . $post->post_title . '</a> (' . $a[0] . ')</li>';
  }
  $output .= '</ul>';
  return $output;
}
add_shortcode('post_list', 'shortcode_post_list');

All you have to do is embed “post_list” in a WordPress page, replacing the quotes with brackets.

Category List and Post Tag List

Before I get to the template code that can create post lists for categories and posts lists for post tags, I’ll give you the shortcodes to produce category and post tag lists.

The category list:

function shortcode_category_list() {
  $categories = get_terms(
    array(
      'taxonomy' => 'category',
      'orderby' => 'name',
      'hide_empty' => true
    )
  );
  $output = '<ul>';
  foreach ($categories as $cat) {
  $output .= '<li><a href="' . home_url('/') . 'category-' . $cat->slug . '.html">' . $cat->name . '</a></li>';
 }
 $output .= '</ul>';
 return $output;
}
add_shortcode('category_list', 'shortcode_category_list');

Embed “category_list”, replacing the quotes with brackets.

The post tag list:

function shortcode_tag_list() {
  $post_tags = get_terms(
    array(
      'taxonomy' =>
      'post_tag', 'orderby' =>
      'name',
      'hide_empty' => true
    )
  );
  $output = '<ul>';
  foreach ($post_tags as $tag) {
  $output .= '<li><a href="' . home_url('/') . 'tag-' . $tag->slug . '.html">' . $tag->name . '</a></li>';
 }
 $output .= '</ul>';
 return $output;
}
add_shortcode('tag_list', 'shortcode_tag_list');

Embed “tag_list”, replacing the quotes with brackets.


Post Lists within Templates

Most WordPress themes don’t come with category or post tag templates. The usual way to create them is to create “category.php” and “tag.php” based on “archive.php”. I didn’t do that. I used a “page.php” template and replaced “the_content()” with my code in each:

For “category.php”:

$slug = get_queried_object()->slug;
$posts_array = get_posts(
  array(
    'posts_per_page' => -1,
    'category_name' => $slug,
    'orderby' => 'date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish')
  );
)
$output = '<ul>';
foreach ($posts_array as $post) {
  $a = explode(' ', $post->post_date);
  $output .= '<li><a href="' . home_url('/') . $post->post_name . '.html">' . $post->post_title . '</a> (' . $a[0] . ')</li>';
}
$output .= '</ul>';
echo $output;

For “tag.php”:

$posts_array = get_posts(
  array(
    'posts_per_page' => -1,
    'orderby' => 'data',
    'order' => 'DESC',
    'post_status' => 'publish',
    'tax_query' => array(
      array(
        'taxonomy' => 'post_tag',
        'field' => 'slug',
        'terms' => $tag
      )
    )
  )
);
$output .= '<ul>';
foreach ($posts_array as $post) {
  $a = explode(' ', $post->post_date);
  $output .= '<li><a href="' . $real_site_url . $post->post_name . '.html">' . $post->post_title . '</a> (' . $a[0] . ')</li>';
}
$output .= '</ul>';
echo $output;

Editing the Post Lists

You’ll have to search the WordPress Codex (or the newer WordPress Developer Resources) to find the code to change them (like adding excerpts). Google search is better at finding the right pages than the search utility WordPress uses.

All of the post lists I created are basic, with only links to the posts involved. You may want to include more, like images or excerpts, but it’s going to make the pages longer and slower loading.

The purpose of post lists is to make it easy for your visitors to find what they’re looking for. If you’re writing your post titles correctly, you don’t need the images or the excerpts.

Please go to this alternate page if you would like read or post comments.

Share:

Categories: Technology

Tags: , , ,

Previous and Next Articles (if any):

« »

More

Please read some of my more important pages if you have the time:

Comments Policy           Privacy Policy

RTCXpression established Feb 28, 2011
Copyright © 2013-2017 RT Cunningham
Hosted at Digital Ocean