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 Answer
Reset to default 3Needs 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"]
-
in a function name likecat-post
. That's illegal. You could docat_post
thought. – kingkool68 Commented Mar 12, 2016 at 3:00extract()
. It was removed from core more than two years ago for very specific reasons. The use ofextract()
is even highly discouraged in normal PHP – Pieter Goosen Commented Mar 14, 2016 at 18:15