WordPress: display blogposts using shortcode

Photo of author
Written By geekerhub

Lorem ipsum dolor sit amet consectetur pulvinar ligula augue quis venenatis. 

To display the blogs of any post type you must know how to add a shortcode in WordPress. You need to follow the below steps to display blogposts using shortcode:

  • Register a shortocode in functions.php
  • Use attributes in shortcode
  • Pass query argument to the shortcode

One need to use this filter “add_shortcode()” to register the shortcode.
This is how our shortcode will be used “[loop_shortcode the_query=”showposts=100&post_type=page&post_parent=453″]

Register a shortocode in functions.php

The Shortcode API is a simple set of functions for creating WordPress shortcodes for use in posts and pages.
Below code snippets will help to register the shortcode in functions.php

	function custom_query_shortcode($atts) {
		// Defaults
	   extract(shortcode_atts(array(
	      "the_query" => ''
	   ), $atts));
	}
	add_shortcode("loop_shortcode", "custom_query_shortcode");

Use attributes in shortcode

One can create attributes for the shortcode as per requirements. We have created one attribute to pass query parameters and format the query before executing

	function custom_query_shortcode($atts) {
		// Defaults
	   	extract(shortcode_atts(array(
	      "the_query" => ''
	   	), $atts));

	    //do format wp_query argument
   		$the_query = preg_replace('~*([0-9a-f]+);~ei', 'chr(hexdec("\1"))', $the_query);
   		$the_query = preg_replace('~*([0-9]+);~e', 'chr(\1)', $the_query);
	}
	add_shortcode("loop_shortcode", "custom_query_shortcode");

Pass query argument to the shortcode

One need to pass the query to the attributes with the query parameters following WordPress standard. Below is the shortcode functions that will go in functions.php file and nee to use the shortcode as mentioned above to fetch the blogs as per the query you have passed in the form of attribute.

	function custom_query_shortcode($atts) {

		// Defaults
	   	extract(shortcode_atts(array(
	      "the_query" => ''
	   	), $atts));

	    //do format wp_query argument
   		$the_query = preg_replace('~*([0-9a-f]+);~ei', 'chr(hexdec("\1"))', $the_query);
   		$the_query = preg_replace('~*([0-9]+);~e', 'chr(\1)', $the_query);

   		// query is made               
   		query_posts($the_query);

   		// the loop
   		if (have_posts()) : while (have_posts()) : the_post();
	      	$temp_id 			= $post->ID;
	      	$temp_title 		= get_the_title($post->ID);
	      	$temp_link 			= get_permalink($post->ID);
	      	$temp_content 		= get_the_content($post->ID);
	      	if ( has_post_thumbnail() ) {
        		$temp_thumb = get_the_post_thumbnail($post->ID);
        	} else {
        		$temp_thumb = "" ;
        	}

      		// output all findings -
     		$output .= "HTML WITH ABOVE DYNAMIC DATA";
    	endwhile; else:
      		$output .= "not found.";
   		endif;
   		
   		wp_reset_query();
   	return $output;
}
add_shortcode("loop_shortcode", "custom_query_shortcode");

Leave a Comment