Wordpress Shortcodes
How to create a Shortcode in wordpress with easiest Example without plugin!
Wordpress Shortcode works in same way as shortcut, to call large functionality in a smallest way.
In the other words, it helps you to show large functionality in smallest calling as with as write code one time instead of writing code multiple times to show specific functionality on different pages. So means it saves time.
For example :
If you want to show posts listing with different Post Types on different locations, you will create a shortcode with a parameter where you will pass a Post Type Name.
Lets see how we will create a shortcode with one time coding and multiple times calling with different post time name and parameter.
Syntax :
add_shortcode('kamra_shortcode-name', 'kamra_shortcode_function');
function kamra_shortcode_function(){}
Demo :
add_shortcode('kamra_wordpress-shortcodes', 'kamraWordpressShortcodes');
function kamraWordpressShortcodes($attr){
$post_type = $posts_per_page = $order = $return_shortcode = '';
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$attributes = shortcode_atts(
array(
'post_type' => 'post',
'posts_per_page'=> -1,
'order' => 'DESC'
),
$attr
);
if(@$attributes['post_type']) {
$post_type = $attributes['post_type'];
}
if(@$attributes['posts_per_page']) {
$posts_per_page = $attributes['posts_per_page'];
}
if(@$attributes['order']) {
$order = $attributes['order'];
}
if($posts_per_page != -1) {
$post_args = array(
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'post_status' => 'publish',
'order' => $order,
'paged' => $paged
);
}
else {
$post_args = array(
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'post_status' => 'publish',
'order' => $order
);
}
$post_query = new WP_Query($post_args);
if($post_query->have_posts()):
while($post_query->have_posts()):$post_query->the_post();
$return_shortcode .= "<div class='post-listing'>";
$return_shortcode .= get_the_title() . "<br/>";
$return_shortcode .= get_the_content() . "<br/>";
$return_shortcode .= get_the_post_thumbnail( get_the_ID(), 'large', array( 'class' => 'float-none' ) ) . "<br/>";
$return_shortcode .= "</div>";
endwhile;
wp_reset_query();
$pager = 999999999;
$return_shortcode .= paginate_links(
array(
'base' => str_replace($pager, '%#%', esc_url(get_pagenum_link($pager))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $post_query->max_num_pages
)
);
endif;
return $return_shortcode;
}
Calling of above shortcode in posts and pages :
[kamra_wordpress-shortcodes] || [kamra_wordpress-shortcodes post_type="post" posts_per_page=2 order="ASC"]
Calling of above shortcode in files :
<?php echo do_shortcode('[kamra_wordpress-shortcodes]'); ?>
<?php echo do_shortcode('[kamra_wordpress-shortcodes post_type="post" posts_per_page=2 order="ASC"]'); ?>
Conclusion :
In the above example, we are providing 3 parameters :
- post type
- posts per page
- order
we can complete the above example with one parameter also but we clarify the shortcode with order and pagination also.
If you will not pass parameters in shortcode while calling it will automatically show you the default posts. Our all the examples are working well so don't be hesitate just copy and paste the above example in your function file and run the code.
If you like this post please share it. Thank You!
Comments
Post a Comment