Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Wordpress Creating a simple short code

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 132
    Comment on it

    Shortcodes are the power of the wordpress. You can simply create shortcodes and call it via the do_shortcode function or on pasting it on editor. For adding a shortcode, you ned to use the hook add_shortcode. We are going to share the script for shortcode creation.

    add_shortcode('mytest','mycalltest');
     function mycalltest($atts,$content=''){
    extract(shortcode_atts(array(
          'posts' => 1,
       ), $atts));
    
       $return_string = '<h3>'.$content.'</h3>';
       $return_string .= '<ul>';
       query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
       if (have_posts()) :
          while (have_posts()) : the_post();
             $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
          endwhile;
       endif;
       $return_string .= '</ul>';
    
       wp_reset_query();
       return $return_string;
    
    
     }
    

    In above example we are showing posts by customizing the number of the post to show. We created shortcode named mytest and passing attribute posts to customize the number of the post to show. You can call the shortcode in post editor like this

     [mytest posts=3]
    

    and via function named do_shortcode

     <?php
    do_shortcode('[mytest posts=3]');
     ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: