New WP List Posts Shortcodes

I have searched for a plugin that can utilize WP get_posts function as described at WP codex site. The results showed that several plugin have been developed but not meet my requirement.

This is my requirement:

  • List post based on tags, categories
  • Show permalink and image attachment (if any)
  • Display next page link if result more than defined using offset
Actually I want to use this shortcode to display my tagged post in attractive way.  Here are the normal PHP code as other plugin:
function sc_get_post($atts, $content=null) {
	global $post;

	// Move to next data - testing
	$atts['offset'] = !isset($_GET['offset']) ? 0 : $_GET['offset'];

	// working with WP get_posts function
	$myposts = get_posts( $atts );
	$output='';

	foreach( $myposts as $post ) {

	setup_postdata($post);
	$output .= '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';

	}

	return '<ul>'. $output .'</ul>';
}

add_shortcode('list-pages', 'sc_get_post');

Here are some example of usage:

Displaying all posts

Code:

[list-pages]

Results:

[list-pages]

Displaying posts tagged with WordPress order by older posting date

Code:

[list-pages tag="Wordpress" orderby="date" order="ASC"]

Results:

[list-pages tag=”Wordpress” orderby=”date” order=”ASC”]

Displaying posts with defined meta_key and meta_value

Code:

[list-pages meta_key="some-key" meta_value="value-here"]

Results:

[list-pages meta_key=”_mcf_cost” meta_value=”$30-$100″]

All above shortcodes worked well in any other plugin too but the missing part here was something with offset. I have included $_GET data in plugin. Try click these link and look at above results:

Then, by knowing the total number of posts from $numberposts attributes, we can easily utilize the $offset to show list of pages partially.

function sc_get_post($atts, $content=null) {
	global $post;

	/* Get total numbers of posts */
	// Duplicate the $atts
	$array = $atts;

	// Remove $numberposts limit
	$array['numberposts'] = -1;
	$query = get_posts( $array );

	$total_post_num = 0;

	foreach( $query as $post ) {
		$total_post_num++;
	}
	// Default value of $numberposts
	$atts['numberposts'] = !isset($atts['numberposts']) ? 5 : $atts['numberposts'];

	// Get offset value
	$atts['offset'] = !isset($_GET['offset']) ? 0 : $_GET['offset'];

	// Check next paging
	if( $atts['offset'] + $atts['numberposts'] < $total_post_num) {
		$next = true;
	}else {
		$next = false;
	}

	// working with WP get_posts function
	$myposts = get_posts( $atts );
	$output='';

	foreach( $myposts as $post ) {

	setup_postdata($post);
	$output .= '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';

	}

	$output = '<ul>'. $output .'</ul>';

	if( $next ) {
		$output .= '<p><a href="?offset=' . ($atts['offset'] + $atts['numberposts']) .'">Next</a></p>
	}
	return $output;
}

add_shortcode('list-pages', 'sc_get_post');

Code:

[list-pages paging]

New result:

[list-pages paging=true]

This plugin will be updated soon to include simple thumbnail image and after that will be released for download. Comments are welcome!

Related Posts:

Leave a Reply

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

Copyright © 2011 - myPHPmaster. All rights reserved.
top

This is sample for Menu caller


Related Posts:

false

Related Posts: