How To Creating Shortcode for a post in WordPress

How To Creating Shortcode for post in Wordpress

This code use for you is to create a Shortcode in any function.php file WordPress.

Shortcodes are easy to use dynamic content into your WordPress pages, posts, custom posts, and sidebars.

WordPress Many plugins and themes use shortcodes to add specialized content like contact forms, image galleries, sliders, and more.

Step-1
<?php
function  getShortcode(){
 $args=array(
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 0,
  );
  
  $my_query = null;
  $my_query = new WP_Query($args);
  $city_arr = array();
  if( $my_query->have_posts()) {
   while ($my_query->have_posts()) : $my_query->the_post();
   $meta=get_post_meta($my_query->post->ID);
    the_title();
    the_content();
   endwhile;
  }
  
}
add_shortcode('Shortcode', 'getShortcode');
?>

Step-2
//Add this code your wordpress page.
[Shortcode]
// Use shortcodes in form like Landing Page Template.
echo do_shortcode( '[Shortcode]' );
// Store the short code in a variable.
$var = do_shortcode( '' );

echo $var;
Learn Tutorials Point
We Well organized and easy-to-understand Interactive tutorials With lots of examples of how to use Tutorials WordPress, PHP, Bootstrap, Bootstrap 4, HTML, CSS, AJAX, jQuery, Woocommerce, Post, Database, Javascript, Theme, Responsive, Templates.

Leave a Reply

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

Contact us

Fill in the form below or give us a call and we'll contact you. We endeavour to answer all enquiries within 24 hours on business days.