Advertisement
rusho81

Post Type Shortcode

Mar 25th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.25 KB | None | 0 0
  1. //This ishortcode will print different type of post [post_list type="post/page"]
  2. <?php
  3. /*
  4. Plugin Name: Stock Toolkit
  5. */
  6. //This is registration of custom post type
  7. add_action( 'init', 'rusho_custom_post' );
  8. function rusho_custom_post() {
  9.     register_post_type( 'testimonial',
  10.         array(
  11.             'labels' => array(
  12.                 'name' => __( 'Testimonials' ),
  13.                 'singular_name' => __( 'Testimonial' )
  14.             ),
  15.             'supports' => array('title', 'editor', 'thumbnail', 'page-attributes'),
  16.             'public' => false,
  17.             'show_ui' => true
  18.         )
  19.     );
  20. }
  21.  
  22. function post_list_shortcode($atts){
  23.     extract( shortcode_atts( array(
  24.         'count' => -1,
  25.         'type' => 'page',
  26.     ), $atts) );
  27.      
  28.     $q = new WP_Query(
  29.         array('posts_per_page' => $count,
  30.               'post_type' => $type
  31.               )
  32.         );      
  33.          
  34.     $list = '<ul>';
  35.     while($q->have_posts()) : $q->the_post();
  36.         $idd = get_the_ID();
  37.         $post_content = get_the_content();
  38.         $list .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';        
  39.     endwhile;
  40.     $list.= '</ul>';
  41.     wp_reset_query();
  42.     return $list;
  43. }
  44. add_shortcode('post_list', 'post_list_shortcode');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement