CPT(自定义Post类型)提要短代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CPT(自定义Post类型)提要短代码相关的知识,希望对你有一定的参考价值。

  1. /**
  2. * CPT (Custom Post Type) Feed Shortcode
  3. * http://codex.wordpress.org/Shortcode_API
  4. */
  5. function fn_cpt( $atts = array(), $content = NULL ) {
  6. global $post, $wp_query;
  7.  
  8. $cpt = 'project'; // Custom Post Type slug
  9.  
  10. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  11.  
  12. // Save and overwrite wp_query so the navigation will work
  13. $saved_query = $wp_query; $wp_query = null;
  14. $saved_post = $post; $post = null;
  15.  
  16. // http://codex.wordpress.org/Function_Reference/WP_Query
  17. // http://codex.wordpress.org/Template_Tags/get_posts
  18. $wp_query = new WP_Query();
  19. $defaults = array(
  20. 'orderby' => 'menu_order', // or 'date'
  21. 'order' => 'ASC', // or 'DSC'
  22. 'post_type' => $cpt,
  23. // $term->taxonomy => $term->slug,
  24. 'post_status' => 'publish',
  25. // 'posts_per_page' => get_option('posts_per_page', 10),
  26. // 'paged' => $paged,
  27. 'posts_per_page' => -1
  28. );
  29. $wp_query->query($defaults); // posts_per_page
  30.  
  31. $output = '';
  32.  
  33. if (have_posts()) {
  34. $count = 0;
  35. while (have_posts()) {
  36. the_post();
  37. get_template_part('templates/'.$cpt, 'excerpt'); // templates/project-excerpt.php
  38. if ($count % 4 == 3) {
  39. echo('<div class="end"></div>');
  40. }
  41. $count++;
  42. }
  43. }
  44.  
  45. // Pagination
  46. get_template_part('templates/nav', 'page'); // templates/nav-page.php
  47.  
  48. $output = ob_get_clean();
  49.  
  50. // Restore previous wp_query
  51. $wp_query = null; $wp_query = $saved_query;
  52. $post = null; $post = $saved_post;
  53.  
  54. return $output;
  55. }
  56. add_shortcode('cptfeed', 'fn_cpt');

以上是关于CPT(自定义Post类型)提要短代码的主要内容,如果未能解决你的问题,请参考以下文章

用户无需访问常规帖子即可访问CPT(自定义帖子类型)

查询自定义帖子类型并按自定义帖子类型排序

自定义帖子类型的无限帖子

php 注册多个自定义帖子类型CPT

php 注册多个自定义帖子类型CPT

php 添加自定义帖子类型#cpt #cust