categories - Create category post shortcode

I'm trying to create a shortcode for my theme without using plugins. I tried thisbut i can not figure out how to

I'm trying to create a shortcode for my theme without using plugins. I tried this / 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 /

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

        '$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.

I'm trying to create a shortcode for my theme without using plugins. I tried this http://wpgeneratetool/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/short-code/

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

        '$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.

Share Improve this question edited Mar 15, 2016 at 8:31 Pieter Goosen 55.4k23 gold badges116 silver badges210 bronze badges asked Mar 12, 2016 at 2:30 pagolpagol 2032 gold badges4 silver badges18 bronze badges 4
  • 1 You can't use - in a function name like cat-post. That's illegal. You could do cat_post thought. – kingkool68 Commented Mar 12, 2016 at 3:00
  • thanks for your suggestion. but rest of think ok ? – pagol Commented Mar 12, 2016 at 3:50
  • No! it isn't correct, there are lot syntax error. Read these examples codex.wordpress/Shortcode_API – Sumit Commented 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 – Pieter Goosen Commented Mar 14, 2016 at 18:15
Add a comment  | 

1 Answer 1

Reset to default 3

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"]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744897846a4599802.html

相关推荐

  • categories - Create category post shortcode

    I'm trying to create a shortcode for my theme without using plugins. I tried thisbut i can not figure out how to

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信