Wordpress custom plugin to create shortcode for custom post type.


/ Published in: PHP
Save to your folder(s)

here i create custom plugin to display custom post type by adding short code to any page and also create custom post type for FAQ with name webr_faq.

add following short code [rep post_type='webr_faq'] to your page and replace with custom post type name.


Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3. Plugin Name: My Shortcode Plugin
  4. Description: display custom post type using short code.
  5. Version: 1.0
  6. Author: Author Name
  7. */
  8. // register custom post type to work with
  9. add_action('init', 'webr_faq_post_type');
  10. function webr_faq_post_type() {
  11. register_post_type('webr_faq', array(
  12. 'label' => 'Faq',
  13. 'description' => '',
  14. 'public' => true,
  15. 'show_ui' => true,
  16. 'show_in_menu' => true,
  17. 'capability_type' => 'post',
  18. 'map_meta_cap' => true,
  19. 'hierarchical' => false,
  20. 'rewrite' => array('slug' => 'faq', 'with_front' => true),
  21. 'query_var' => true,
  22. 'supports' => array('title','editor','excerpt','thumbnail','author','page-attributes'),
  23. 'labels' => array (
  24. 'name' => 'Faq',
  25. 'singular_name' => 'Faq',
  26. 'menu_name' => 'Faq',
  27. 'add_new' => 'Add Faq',
  28. 'add_new_item' => 'Add New Faq',
  29. 'edit' => 'Edit',
  30. 'edit_item' => 'Edit Faq',
  31. 'new_item' => 'New Faq',
  32. 'view' => 'View Faq',
  33. 'view_item' => 'View Faq',
  34. 'search_items' => 'Search Faq',
  35. 'not_found' => 'No Faq Found',
  36. 'not_found_in_trash' => 'No Faq Found in Trash',
  37. 'parent' => 'Parent Faq',
  38. )
  39. )
  40. );
  41. }
  42.  
  43. add_action('init', 'webr_faq_post_type_taxonomy');
  44. function webr_faq_post_type_taxonomy() {
  45. register_taxonomy( 'webr_faq_categories',array (
  46. 0 => 'webr_faq',
  47. ),
  48. array( 'hierarchical' => true,
  49. 'label' => 'FAQ Categories',
  50. 'show_ui' => true,
  51. 'query_var' => true,
  52. 'show_admin_column' => true,
  53. 'labels' => array (
  54. 'search_items' => 'Faq Category',
  55. 'popular_items' => '',
  56. 'all_items' => '',
  57. 'parent_item' => '',
  58. 'parent_item_colon' => '',
  59. 'edit_item' => '',
  60. 'update_item' => '',
  61. 'add_new_item' => 'Add New Faq Category',
  62. 'new_item_name' => 'New Faq Category',
  63. 'separate_items_with_commas' => '',
  64. 'add_or_remove_items' => '',
  65. 'choose_from_most_used' => '',
  66. )
  67. )
  68. );
  69. }
  70.  
  71. function cpt_content_func($atts){
  72.  
  73. extract( shortcode_atts( array(
  74. 'slug' => null,
  75. 'post_type'=>null,
  76. ), $atts ) );
  77.  
  78. $args = array(
  79. 'name' => $slug,
  80. 'post_type' => $post_type,
  81. 'posts_per_page' => 2
  82. );
  83. $posts= array();
  84. $posts = get_posts( $args );
  85. //echo "<pre>";
  86. //print_r($posts);
  87. $content='';
  88. $post_nav=array();
  89. foreach ($posts as $post) : setup_postdata($post);
  90. $post_nav[] += $post->ID;
  91. $url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID) );
  92. $content .= '<h2>'.$post->post_title.'</h1>';
  93. if(!empty($url)){
  94. $content .= '</br><img src='.$url[0].'>';
  95. }
  96. $content .= '</br></br>'.$post->post_content;
  97. $content .= '<div class="nav-previous">' . get_next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts' ) ) . '</div>';
  98.  
  99. endforeach;
  100.  
  101. $current = array_search( get_the_ID(), $post_nav );
  102. $prevID = $posts[$current-1];
  103. $nextID = $posts[$current+1];
  104.  
  105. $content .= '<div class="navigation">';
  106. if ( !empty( $prevID ) ):
  107. $content .= '<div class="alignleft">';
  108. $content .= '<a href='.get_permalink( $prevID ).' title='.get_the_title( $prevID ).'>Previous</a>';
  109. $content .= '</div>';
  110. endif;
  111. if ( !empty( $nextID ) ):
  112. $content .= '<div class="alignright">';
  113. $content .= '<a href='.get_permalink( $nextID ).' title='.get_the_title( $nextID ).'>Next</a>';
  114. $content .= '</div>';
  115. endif;
  116. $content .= '</div>'; //navigation
  117.  
  118. return $content.wp_reset_postdata();
  119.  
  120. }
  121.  
  122. add_shortcode('rep','cpt_content_func');
  123.  
  124.  
  125. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.