Posted By

mommygeekology on 12/09/11


Tagged

wordpress


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Kurt
Gabrielyan


FAQ Custom Post Type - WordPress


 / Published in: PHP
 

URL: http://rentageekmom.com

Code snippet to create custom post type FAQ and custom taxonomy Topics in WordPress v. 2.9.2+

  1. <?php
  2. add_action('init', 'custom_faqs');
  3. function custom_faqs()
  4. {
  5. $labels = array(
  6. 'name' => _x('faqs', 'post type general name'),
  7. 'singular_name' => _x('faq', 'post type singular name'),
  8. 'add_new' => _x('Add New', 'FAQ'),
  9. 'add_new_item' => __('Add New FAQ'),
  10. 'edit_item' => __('Edit FAQ'),
  11. 'new_item' => __('New FAQ'),
  12. 'view_item' => __('View FAQ'),
  13. 'search_items' => __('Search FAQs'),
  14. 'not_found' => __('No FAQs found'),
  15. 'not_found_in_trash' => __('No FAQs found in Trash'),
  16. 'parent_item_colon' => '',
  17. 'menu_name' => 'FAQ'
  18.  
  19. );
  20. $args = array(
  21. 'labels' => $labels,
  22. 'public' => true,
  23. 'publicly_queryable' => true,
  24. 'show_ui' => true,
  25. 'show_in_menu' => true,
  26. 'query_var' => true,
  27. 'rewrite' => true,
  28. 'capability_type' => 'post',
  29. 'has_archive' => true,
  30. 'hierarchical' => false,
  31. 'menu_position' => 5,
  32. 'supports' => array('title','editor','thumbnail','excerpt','custom-fields')
  33. );
  34. register_post_type('faqs',$args);
  35. }
  36.  
  37. //add filter to ensure the text faq, or faq, is displayed when user updates a faq
  38. add_filter('post_updated_messages', 'faq_updated_messages');
  39. function faq_updated_messages( $messages ) {
  40. global $post, $post_ID;
  41.  
  42. $messages['faqs'] = array(
  43. 0 => '', // Unused. Messages start at index 1.
  44. 1 => sprintf( __('FAQ updated. <a href="%s">View FAQ</a>'), esc_url( get_permalink($post_ID) ) ),
  45. 2 => __('Custom field updated.'),
  46. 3 => __('Custom field deleted.'),
  47. 4 => __('FAQ updated.'),
  48. /* translators: %s: date and time of the revision */
  49. 5 => isset($_GET['revision']) ? sprintf( __('FAQ restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
  50. 6 => sprintf( __('FAQ published. <a href="%s">View FAQ</a>'), esc_url( get_permalink($post_ID) ) ),
  51. 7 => __('FAQ saved.'),
  52. 8 => sprintf( __('FAQ submitted. <a target="_blank" href="%s">Preview FAQ</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  53. 9 => sprintf( __('FAQ scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview FAQ</a>'),
  54. // translators: Publish box date format, see http://php.net/date
  55. date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
  56. 10 => sprintf( __('FAQ draft updated. <a target="_blank" href="%s">Preview FAQ</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  57. );
  58.  
  59. return $messages;
  60. }
  61.  
  62. //display contextual help for faqs
  63. add_action( 'contextual_help', 'faq_add_help_text', 10, 3 );
  64.  
  65. function faq_add_help_text($contextual_help, $screen_id, $screen) {
  66. //$contextual_help .= var_dump($screen); // use this to help determine $screen->id
  67. if ('faqs' == $screen->id ) {
  68. $contextual_help =
  69. '<p>' . __('Things to remember when adding or editing a FAQ:') . '</p>' .
  70. '<ul>' .
  71. '<li>' . __('Specify the correct topic such as Orders. Create a new topic if necessary.') . '</li>' .
  72. '<li>' . __('Specify a custom excerpt - a short overview of the FAQ') . '</li>' .
  73. '</ul>' .
  74. '<p>' . __('Choose related posts using the MicroKids Related Posts Plugin below the FAQ') . '</p>' .
  75. '<ul>' .
  76. '<li>' . __('Click the Save button to save an FAQ as a draft') . '</li>' .
  77. '<li>' . __('Click publish to publish an FAQ. ') . '</li>' .
  78. '</ul></p>';
  79. } elseif ( 'edit-faqs' == $screen->id ) {
  80. $contextual_help =
  81. '<p>' . __('This is the list of all current FAQs in published or draft form. Hover over an FAQ and click Edit to edit, or click on the title of the FAQ to edit.') . '</p>' ;
  82. }
  83. return $contextual_help;
  84. }
  85.  
  86. function my_rewrite_flush() {
  87. custom_faqs();
  88. flush_rewrite_rules();
  89. }
  90. register_activation_hook(__FILE__, 'my_rewrite_flush');
  91.  
  92. //hook into the init action and call create_FAQ_taxonomies when it fires
  93. add_action( 'init', 'create_faq_taxonomies', 0 );
  94.  
  95. //create two taxonomies, genres and writers for the post type "FAQ"
  96. function create_faq_taxonomies()
  97. {
  98. // Add new taxonomy, make it hierarchical (like categories)
  99. $labels = array(
  100. 'name' => _x( 'topics', 'taxonomy general name' ),
  101. 'singular_name' => _x( 'topic', 'taxonomy singular name' ),
  102. 'search_items' => __( 'Search Topics' ),
  103. 'all_items' => __( 'All Topics' ),
  104. 'parent_item' => __( 'Parent Topic' ),
  105. 'parent_item_colon' => __( 'Parent Topic:' ),
  106. 'edit_item' => __( 'Edit Topic' ),
  107. 'update_item' => __( 'Update Topic' ),
  108. 'add_new_item' => __( 'Add New Topic' ),
  109. 'new_item_name' => __( 'New Topic Name' ),
  110. 'menu_name' => __( 'Topics' ),
  111. );
  112.  
  113. register_taxonomy('topic',array('faqs'), array(
  114. 'hierarchical' => true,
  115. 'labels' => $labels,
  116. 'show_ui' => true,
  117. 'query_var' => true,
  118. 'rewrite' => array( 'slug' => 'topic' ),
  119. ));
  120.  
  121. }
  122. ?>

Report this snippet  

You need to login to post a comment.