Posts List Shortcode

Below, you’ll find a detailed guide on how to add the Posts List Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Posts List Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Posts List Plugin and the shortcodes it provides:

Plugin Icon
Posts List

"Posts List is a WordPress plugin designed to conveniently manage and display your blog posts in a structured format, enhancing navigation and user experience."

★★★★★ (1) Active Installs: 5000+ Tested with: 3.2.1 PHP Version: false
Included Shortcodes:
  • [posts-list]

Posts List [posts-list] Shortcode

The ‘Posts-List’ shortcode is a powerful tool that fetches and displays posts. It accepts various parameters like ‘type’, ‘sort’, ‘style’, ‘date_format’, ‘year’, ‘month’, ‘category’ to customize the output. It generates a list of posts based on the given parameters and stores the result in a transient for efficient retrieval. It supports different list styles (‘ul’, ‘ol’, ‘dl’, ‘table’, ‘div’, ‘p’) and sorts (‘asc’, ‘desc’). The shortcode also handles the ‘date_format’ parameter, allowing you to format the post date. With ‘year’, ‘month’, ‘category’ parameters, you can filter the posts to display. In summary, the ‘Posts-List’ shortcode offers flexible control over the display of posts in WordPress.

Shortcode: [posts-list]

Parameters

Here is a list of all possible posts-list shortcode parameters and attributes:

  • type – Defines the type of post to be listed (e.g., ‘post’, ‘page’, ‘attachment’)
  • sort – Determines the order of posts, ‘asc’ for ascending and ‘desc’ for descending
  • style – Specifies the HTML element used for listing (e.g., ‘ul’, ‘ol’, ‘dl’, ‘table’, ‘div’, ‘p’)
  • date_format – Sets the format of the date for each post; defaults to the WordPress date format
  • year – Filters posts by a specific year
  • month – Filters posts by a specific month
  • category – Filters posts by a specific category
  • class_list – Sets the class for the list element
  • class_item – Sets the class for each item in the list
  • expiration – Sets the cache expiration time for the posts list in seconds

Examples and Usage

Basic example – A straightforward usage of the ‘posts-list’ shortcode, listing all posts in descending order with a default unordered list style.

[posts-list /]

Advanced examples

Using the shortcode to display a list of posts from a specific category, sorted in ascending order, and presented in an ordered list style.

[posts-list type="post" sort="asc" style="ol" category="3" /]

Displaying a list of pages instead of posts, sorted in descending order, and presented in a table style with a specific class for the list and each item.

[posts-list type="page" style="table" class_list="my-list" class_item="my-item" /]

Presenting a list of posts from a specific year and month, sorted in ascending order, and displayed in a div style.

[posts-list type="post" sort="asc" style="div" year="2021" month="06" /]

Using the shortcode to list posts with a custom date format, and set a specific expiration for the transient cache.

[posts-list date_format="F j, Y" expiration="600" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [posts-list] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('posts-list', array(&$this, 'shortcode_handler'));

Shortcode PHP function:

function shortcode_handler($atts, $content = '') {
		global $wpdb;
		extract( shortcode_atts( array(
			'type' => 'post',
			'sort' => 'desc',
			'style' => 'ul',
			'date_format' => get_option('date_format'),
			'year' => '',
			'month' => '',
			'category' => '',
			'class_list' => 'archive-list',
			'class_item' => 'archive-list-item',
			'expiration' => 300,
			), $atts) );

		if (strtolower($date_format) === 'false')
			$date_format = FALSE;

		$transient  = 'posts-list-' . md5($type . $sort . $style . ($date_format ? $date_format : '') . $year . $month . $category . $class_list . $class_item);
		if (false !== ($return_text = get_transient($transient))) {
			return $return_text;
		}

		$return_text = '';
		$list_before = '';
		$list_template = '%s<a title="%s" href="%s">%s</a>';
		$list_after = '';
		$expiration = (is_numeric($expiration) ? $expiration : 5 * 60);

		$type = trim(strtolower($type));
		switch ($type) {
		case 'page':
		case 'post':
		case 'attachment':
			break;
		default:
			$post_types = get_post_types(array('public' => true));
			if ( !in_array($type, $post_types) ) {
				$type = 'post';
			}
		}

		$sort = trim(strtolower($sort));
		switch ($sort) {
		case 'desc':
		case 'asc':
			$sort = ' ' . $sort;
			break;
		default:
			$sort = '';
		}

		$style = trim(strtolower($style));
		$class_list = trim(esc_attr($class_list));
		$class_item = trim(esc_attr($class_item));
		switch ($style) {
		case 'ol':
			$list_before = "\n<ol class=\"$class_list\">\n";
			$list_after = "</ol>\n\n";
			$list_template = "<li class=\"$class_item\">$list_template</li>\n";
			break;
		case 'dl':
			$list_before = "\n<dl class=\"$class_list\">\n";
			$list_after = "</dl>\n\n";
			$list_template = "<dt class=\"$class_item\">$list_template</dt>\n";
			break;
		case 'table':
			$list_before = "\n<table class=\"$class_list\"><tbody>\n";
			$list_after = "</tbody></table>\n\n";
			$list_template = '<tr class=\"$class_item\"><td>%s</td><td><a title="%s" href="%s">%s</a></td></tr>'."\n";
			break;
		case 'div':
			$list_before = "\n\n";
			$list_after = "\n";
			$list_template = "<div class=\"$class_item\">$list_template</div>\n";
			break;
		case 'p':
			$list_before = "\n\n";
			$list_after = "\n";
			$list_template = "<p class=\"$class_item\">$list_template</p>\n";
			break;
		case 'ul':
		default:
			$style = 'ul';
			$list_before = "\n<ul class=\"$class_list\">\n";
			$list_after = "</ul>\n\n";
			$list_template = "<li class=\"$class_item\">$list_template</li>\n";
			break;
		}

		$year = $this->numeric_normalize($year);
		$month = $this->numeric_normalize($month);
		$category = $this->numeric_normalize($category);

		$sql = "
			select
			 p.ID,
			 u.user_nicename,
			 p.post_author,
			 p.post_date,
			 p.post_modified,
			 p.post_name,
			 p.post_title,
			 p.post_type,
			 p.post_parent,
			 p.post_status,
			 min(c.cat_id) as cat_id
			from
			 $wpdb->posts as p
			 left join $wpdb->users as u on u.ID = p.post_author
			 left join (
			  select
			   thiscat.object_id as ID,
			   cat.term_id as cat_id
			  from
			   $wpdb->term_relationships AS thiscat
			   inner JOIN $wpdb->term_taxonomy AS cat on(thiscat.term_taxonomy_id = cat.term_taxonomy_id AND cat.taxonomy = 'category')
			  ) as c on (c.ID = p.ID )
			where
			 p.post_status IN ( 'publish',  'static' )
			 and p.post_password = ''
			 and p.post_type = '$type'" .
			(!empty($year) ? " and DATE_FORMAT(p.post_date, '%Y') in ($year)" : '') .
			(!empty($month) ? " and DATE_FORMAT(p.post_date, '%c') in ($month)" : '') .
			(!empty($category) ? " and c.cat_id in ($category)" : '') . "
			group by
			 p.ID
			order by
			 p.post_date $sort
			";
		$posts = $wpdb->get_results($sql);
		if (count($posts) > 0) {
			$return_text = $list_before;
			foreach ( $posts as $post ) {
				$post_date = $date_format ? mysql2date($date_format, $post->post_date) . ': ' : '';
				$permalink = $this->get_permalink($type, $post);
				$post_title = trim($post->post_title);
				$return_text .= sprintf(
					$list_template ,
					$post_date ,
					esc_attr($post_title),
					$permalink,
					$post_title
					);
			}
			$return_text .= $list_after;
		}
		set_transient($transient, $return_text, $expiration);

		return $return_text;
	}

Code file location:

posts-list/posts-list/posts-list.php

Conclusion

Now that you’ve learned how to embed the Posts List Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *