shortcode to display post by category entered by user without plugin

i am created a custom post called knw_shr_docs. i am trying to create a short code whcih should display post tile according to category i.e : [knowledge_sharing posts_per_page=’5′ doc_cat=’docs’]
category should be add by me.
Code i tried :-

function func_knowledge_sharing_docs(){
$a = shortcode_atts( array(
‘doc_cat’ => ”
), $atts );

$output = ”;
$post_cat = esc_attr($a[‘doc_cat’]);

$args = array(
‘category’ => $post_cat,
‘post_type’ => ‘knowledgeSharingDocs’,
‘order’ => ‘DESC’
);

$query = new WP_Query( $args );

$pages = $query->posts;
$output .= ‘<ul>’;
foreach($pages as $page) {
$page_title = $page->post_title;
$page_url = get_page_link( $page->ID );

$output .= ‘<li><a href=”‘.$page_url.'”>’ .$page_title. ‘</a></li>’;

}
$output .= ‘</ul>’;
wp_reset_postdata();
return $output;
}

add_shortcode( ‘knowledge_sharing’, ‘func_knowledge_sharing_docs’ );

please anyone tell where is my mistake.
Thank you in advance.

Read more here:: shortcode to display post by category entered by user without plugin

Leave a Reply

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