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 accord

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.

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.

Share Improve this question edited Sep 10, 2019 at 18:36 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Sep 10, 2019 at 7:02 Poonam KatparaPoonam Katpara 135 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try the below code. I think it could help you.

add_shortcode( 'knowledge_sharing', '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($cat);
   if (!is_object($category)) {return;}
   $args = array(
        'cat' => $category->term_id,
        'posts_per_page' => $posts_per_page,
        'post_type' => 'knowledgeSharingDocs',
        'order'  => 'DESC'
   );
   $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_the_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;
}

shortcode: [knowledge_sharing cat="pant" posts_per_page="5"]

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

相关推荐

  • 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 accord

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信