Advertisement
shakil97bd

How to query custom post with shortcode

Aug 11th, 2014
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. <!-----------How to query custom post with shortcode--------------->
  2.  
  3. 1.First register custom post in fuctions.php like bellow....
  4.  
  5. function portfolio_list_custom_post() {
  6. register_post_type( 'portfolio_items',
  7. array(
  8. 'labels' => array(
  9. 'name' => __( 'portfolios' ),
  10. 'singular_name' => __( 'portfolio' ),
  11. 'all_items' => __( 'All portfolio' ),
  12. 'add_new_item' => __( 'Add new portfolio' ),
  13. 'search_items' => __( 'Search portfolio' ),
  14. 'not_found' => __( 'No portfolio found,Please!!! try with another keyword' ),
  15. 'not_found_in_trash' => __( 'No portfolio found in trash' ),
  16. 'parent_item' => __( 'Parent portfolio' ),
  17. 'parent_item_colon' => __( 'Parent portfolio' ),
  18. 'edit_item' => __( 'Edit portfolio' ),
  19. 'update_item' => __( 'Update portfolio' ),
  20. 'view_item' => __( 'View portfolio' ),
  21. 'new_item_name' => __( 'New portfolio' ),
  22. 'menu_name' => __( 'portfolio' )
  23.  
  24.  
  25. ),
  26. 'public' => true,
  27. 'menu_icon' => 'dashicons-nametag',
  28. 'supports' => array('title', 'editor','thumbnail'),
  29. 'has_archive' => true,
  30. 'menu_position' => 8,
  31. 'rewrite' => array('slug' => 'portfolio_item')
  32.  
  33.  
  34. )
  35.  
  36. );
  37.  
  38.  
  39. }
  40. add_action('init', 'portfolio_list_custom_post');
  41.  
  42. 2.Than add theme support and images size in custom post....
  43.  
  44. add_theme_support( 'post-thumbnails' );
  45. add_image_size( 'portfolio-big', 298, 460, true );
  46.  
  47. 3.Than query your post with shortcode in functions.php or shortcode.php file.....
  48.  
  49. function portfolio_list_shortcode($atts){
  50. extract( shortcode_atts( array(
  51. 'title' => '',
  52. 'dsc' => '',
  53. 'more' => '',
  54. 'build' => '',
  55. ), $atts, 'portfolio' ) );
  56.  
  57. $q = new WP_Query(
  58. array('posts_per_page' => 4, 'post_type' => 'portfolio_items')
  59. );
  60.  
  61.  
  62. $list = '
  63.  
  64. <!----your content rapper--->
  65.  
  66. <div id="portfolio" class="portfolio-area top-40">
  67. <div class="container">
  68. <div class="heading-area text-center bottom-30 wow fadeInUp" data-wow-duration="0.6s" data-wow-delay="0.3s">
  69. <h4 class="heading large">
  70. '.$title.'
  71. </h4>
  72. <span class="sub-heading">
  73. '.$dsc.'
  74. </span>
  75. </div>
  76.  
  77. <div id="portfolio-area" class="top-40">
  78. <div class="row onepixel">';
  79. while($q->have_posts()) : $q->the_post();
  80. $idd = get_the_ID();
  81.  
  82. $portfolio_big_img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'portfolio-big' );
  83.  
  84. $list .= '
  85.  
  86. <!---your dynamic content--->
  87.  
  88. <div class="col-md-3 col-sm-6 bottom-1 wow fadeInRight" data-wow-duration="0.6s" data-wow-delay="0.6s">
  89. <div class="portfolio-item">
  90. <div class="portfolio-image">
  91. <a href="portfolio-detail.html" title="">
  92. <img src="'.$portfolio_big_img[0].'" alt="" class="img-responsive">
  93. </a>
  94. </div>
  95.  
  96. <div class="portfolio-info">
  97. <div class="portfolio-info-inner clearfix">
  98. <div class="portfolio-title">
  99. <h4 class="bottom-0">
  100. <a href="portfolio_detail.html">
  101. '.get_the_title().'
  102. </a>
  103. </h4>
  104. </div>
  105. <div class="portfolio-star">
  106. <a href="http://localhost/rrf/wp-content/uploads/2014/08/Desert1.jpg">
  107. <i class="fa fa-star-o">
  108. </i>
  109. </a>
  110. </div>
  111. <div class="portfolio-link">
  112. <a href="'.get_permalink().'">
  113. <i class="fa fa-link">
  114. </i>
  115. </a>
  116. </div>
  117. </div>
  118. </div>
  119.  
  120. </div>
  121.  
  122. </div>
  123.  
  124. ';
  125. endwhile;
  126. $list.= '
  127.  
  128. <!---your cotent rapper end----->
  129.  
  130. </div>
  131.  
  132. </div>
  133.  
  134. <div class="button-group text-center top-30">
  135. <a href="'.$more.'" class="button wow fadeInRight" data-wow-duration="0.6s" data-wow-delay="0.3s">
  136. <span>
  137. View more projects
  138. </span>
  139. <span class="icon">
  140. <i class="fa fa-briefcase">
  141. </i>
  142. </span>
  143. </a>
  144. <a href="'.$build.'" class="button black wow fadeInLeft" data-wow-duration="0.6s" data-wow-delay="0.6s">
  145. <span>
  146. Build your own
  147. </span>
  148. <span class="icon">
  149. <i class="fa fa-database">
  150. </i>
  151. </span>
  152. </a>
  153. </div>
  154.  
  155. </div>
  156.  
  157. </div>
  158.  
  159. ';
  160. wp_reset_query();
  161. return $list;
  162. }
  163. add_shortcode('portfolio', 'portfolio_list_shortcode');
  164.  
  165.  
  166. 4.Next call your shortcode in index.php like bellow ....
  167.  
  168. <?php echo do_shortcode('[portfolio title="Recent Projects" dsc="the latest work of our team" more="#" build="#"]'); ?>
  169.  
  170.  
  171. বি:দ্র: যদি shortcode এর অংশ টুকু আলাদা file -এ করতে হয় তাহলে inc নামে একটি folder করে shorcode.php file এর মধ্যে করতে হবে এবং shorcode.php file টিকে functions.php file -এ call করতে হবে এভাবে.......
  172.  
  173. include_once('inc/shortcode.php');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement