I'm trying to create a shortcode for my theme without using plugins. I tried this http://wpgeneratetool.com/short-code/ but i can not figure out how to create.

I want to create something like the following where I can set category name and post per page.

Example like [categorypost cat="pant" post-per-page="5" ]

HTML output will be title, post thumbnail and permalink.

Example bellow

<div><h1>post title</h1><a herf="permalink url"> post thumbnail</a></div>

I've tried with http://wpgeneratetool.com/short-code/

function cat-post($atts){
    extract( shortcode_atts(

        '$category-name' => '$atts['id']',
        '$post-title' => 'get_the_title($post_id)',
        '$post-link' => 'get_the_permalink($post_id)',
        '$post-image' => 'get_the_post_thumbnail($post_id,  'thumbnail')',
        $post-data ='<div><a href="'.$link.'">'.$image.'<h5>'.$title.'</h5></a></div>';
    return $data;

        ), $atts ) )

add_shortcode( 'categorypost', 'cat-post' );

but I know this code is not correct.

  • 1
    You can't use - in a function name like cat-post. That's illegal. You could do cat_post thought.
    – kingkool68
    Mar 12, 2016 at 3:00
  • thanks for your suggestion. but rest of think ok ?
    – pagol
    Mar 12, 2016 at 3:50
  • No! it isn't correct, there are lot syntax error. Read these examples codex.wordpress.org/Shortcode_API
    – Sumit
    Mar 12, 2016 at 10:19
  • Never ever use extract(). It was removed from core more than two years ago for very specific reasons. The use of extract() is even highly discouraged in normal PHP Mar 14, 2016 at 18:15

1 Answer 1


Needs a complete rewrite...

add_shortcode( 'categorypost', 'cat_post' );

function cat_post( $atts ) {

    // attributes for shortcode
   if ( isset( $atts['cat'] ) ) {$cats = $atts['cat'];} else {return;}
   if ( isset( $atts['posts_per_page'] ) ) {$posts_per_page = $atts['posts_per_page'];} else {$posts_per_page = -1;}

   // get the category posts
   $category = get_category_by_slug( $cats );
   if ( !is_object( $category ) ) {return;}
   $args = array(
        'cat' => $category->term_id,
        'posts_per_page' => $posts_per_page
   $posts = get_posts( $args );

   // create the list output
   if ( count( $posts ) > 0 ) {
       foreach ( $posts as $post ) {
           $link = get_permalink( $post->ID );
           $title = $post->post_title;
           $image = get_post_thumbnail( $post->ID,'thumbnail' );
           $output .= '<div id="postrow-'.$post->ID.'" class="postrow">';
           $output .= '<a class="postlink" href="'.$link.'">' . $image;
           $output .= '<h5 class="posttitle">' . $title . '</h5></a></div>';
   return $output;

Example shortcode usage: [categorypost cat="pant" posts_per_page="5"]

  • Thank you so much for kind help. i think it will work if we can tweak a bit. i tried but it show first time error. then add } after ` return $output; }` then in dreamweaver error not showing but one page show error line number 216 which is ` $image = get_post_thumbnail($post->ID);. after then then add }` after ` $atts ) );` error not showing but data not coming
    – pagol
    Mar 14, 2016 at 16:37
  • finally i did. $image = get_the_post_thumbnail($post->ID, 'thumbnail'); when i put it then it is work. thanks man. thank a lot. one more thing is possible also shorting option. like assdining designing
    – pagol
    Mar 14, 2016 at 16:48
  • Take this as a downvote, never ever use extract(). It was removed from core more than two years ago for very specific reasons. The use of extract() is even highly discouraged in normal PHP. Usage of extract() was also removed from the codex, so you are definitely doing it wrong Mar 14, 2016 at 18:15
  • @PieterGoosen this is working but i don't about extract issue. would you please tell us which we can code then without using extract(). i am not expert on WordPress yet
    – pagol
    Mar 14, 2016 at 18:26
  • 1
    @NarenVerma yes you are correct, it was missing close } for the count condition check, looking at the top I see the questioner had the same problem but I wasn't clear on what was meant. fixed.
    – majick
    Dec 17, 2019 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.