functions.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. add_theme_support( 'custom-aboutus' );
  3. add_theme_support( 'custom-news' );
  4. add_theme_support( 'custom-newsDetail' );
  5. add_theme_support( 'custom-join' );
  6. add_theme_support( 'custom-joinIndex' );
  7. add_theme_support( 'custom-product' );
  8. add_theme_support( 'custom-school' );
  9. add_theme_support( 'custom-schoolDetail' );
  10. add_theme_support( 'custom-contact' );
  11. add_theme_support( 'custom-jobs' );
  12. add_theme_support( 'custom-service' );
  13. // 自定义评论留言
  14. function submit_custom_comment() {
  15. if (isset($_POST['comment_data'])) {
  16. parse_str($_POST['comment_data'], $commentData); // 解析评论数据
  17. // 构建评论数据数组
  18. $comment_args = array(
  19. 'comment_post_ID' => $commentData['comment_post_ID'],
  20. 'comment_author_IP' => $commentData['comment_author_IP'],
  21. 'comment_author_url' => $commentData['comment_author_url'],
  22. 'comment_author' => $commentData['comment_author'],
  23. 'comment_author_email' => $commentData['comment_author_email'],
  24. 'comment_content' => $commentData['comment'],
  25. 'comment_type' => 'comment',
  26. 'comment_meta' => array(
  27. 'phone' => $commentData['comment_author_phone'], // 将自定义字段添加到评论元数据中
  28. 'compname' => $commentData['comment_compname'], // 将自定义字段添加到评论元数据中
  29. 'address' => $commentData['comment_provinces'].$commentData['comment_city'].$commentData['comment_area'].' '.$commentData['comment_address'], // 将自定义字段添加到评论元数据中
  30. )
  31. );
  32. // 插入评论
  33. $comment_id = wp_insert_comment($comment_args);
  34. if ($comment_id !== 0) {
  35. echo json_encode(['status' => 'success', 'comment_id' => $comment_id]);
  36. } else {
  37. echo json_encode(['status' => 'error', 'message' => 'Failed to submit comment.']);
  38. }
  39. }
  40. die(); // 必须终止脚本
  41. }
  42. add_action('wp_ajax_submit_custom_comment', 'submit_custom_comment'); // 钩子 - 处理登录用户的请求
  43. add_action('wp_ajax_nopriv_submit_custom_comment', 'submit_custom_comment'); // 钩子 - 处理非登录用户的请求
  44. // 添加后台评论处显示自定义字段代码
  45. add_filter( 'manage_edit-comments_columns', 'my_comments_columns' );
  46. add_action( 'manage_comments_custom_column', 'output_my_comments_columns', 10, 3 );
  47. function my_comments_columns( $columns ){
  48. $columns[ 'phone' ] = __( '电话' );
  49. $columns[ 'compname' ] = __( '主营项目' );
  50. $columns[ 'address' ] = __( '地址' );
  51. return $columns;
  52. }
  53. function output_my_comments_columns( $column_name, $comment_id ){
  54. switch( $column_name ) {
  55. case "phone" :
  56. echo get_comment_meta( $comment_id, 'phone', true );
  57. break;
  58. case "compname" :
  59. echo get_comment_meta( $comment_id, 'compname', true );
  60. break;
  61. case "address" :
  62. echo get_comment_meta( $comment_id, 'address', true );
  63. break;
  64. }
  65. }
  66. // 在文章被创建时设置初始浏览计数
  67. add_action('draft_to_publish', 'set_post_views_on_publish');
  68. add_action('save_post', 'set_post_views_on_publish');
  69. function set_post_views_on_publish($post_ID) {
  70. if (!isset($_POST['post_ID'])) {
  71. return;
  72. }
  73. $post_ID = $_POST['post_ID'];
  74. if (!isset($_POST['post_status']) || 'publish' !== $_POST['post_status']) {
  75. return;
  76. }
  77. $count_key = 'views';
  78. $count = (int) get_post_meta($post_ID, $count_key, true);
  79. if (!$count) {
  80. delete_post_meta($post_ID, $count_key);
  81. add_post_meta($post_ID, $count_key, '0');
  82. }
  83. }
  84. // 添加封面图片选项到文章编辑页面
  85. function add_featured_image_field_to_edit_page() {
  86. add_meta_box('featured_image_meta_box', '设置封面图片', 'render_featured_image_meta_box', 'post', 'normal', 'high');
  87. }
  88. add_action('add_meta_boxes', 'add_featured_image_field_to_edit_page');
  89. // 渲染封面图片选项界面
  90. function render_featured_image_meta_box($post) {
  91. // 非必要代码,用于在编辑页面显示提示信息
  92. echo '<p>请上传作为文章封面的图片:</p>';
  93. // 创建图片上传表单
  94. wp_nonce_field('featured_image_nonce', 'featured_image_nonce_field');
  95. ?>
  96. <input type="hidden" name="featured_image_id" value="<?php echo get_post_thumbnail_id($post->ID) ?: ''; ?>">
  97. <input type="file" name="featured_image">
  98. <?php
  99. }
  100. // 保存封面图片选项
  101. function save_featured_image_meta($post_id) {
  102. if (!isset($_POST['featured_image_nonce_field']) || !wp_verify_nonce($_POST['featured_image_nonce_field'], 'featured_image_nonce')) {
  103. return;
  104. }
  105. if (!current_user_can('edit_post', $post_id)) {
  106. return;
  107. }
  108. if (isset($_FILES['featured_image']) && !empty($_FILES['featured_image']['name'])) {
  109. $attachment_id = media_handle_upload('featured_image', $post_id);
  110. if ($attachment_id) {
  111. update_post_meta($post_id, '_thumbnail_id', $attachment_id);
  112. }
  113. } elseif (isset($_POST['featured_image_id']) && !empty($_POST['featured_image_id'])) {
  114. update_post_meta($post_id, '_thumbnail_id', $_POST['featured_image_id']);
  115. }
  116. }
  117. add_action('save_post', 'save_featured_image_meta');
  118. add_theme_support('post-thumbnails');
  119. /**
  120. * 数字分页函数
  121. * 因为wordpress默认仅仅提供简单分页
  122. * 所以要实现数字分页,需要自定义函数
  123. * @Param int $range 数字分页的宽度
  124. * @Return string|empty 输出分页的HTML代码
  125. */
  126. function lingfeng_pagenavi( $range = 4 ) {
  127. global $paged,$query;
  128. $max_page = 0;
  129. if ( !$max_page ) {
  130. $max_page = $query->max_num_pages;
  131. }
  132. if( $max_page >1 ) {
  133. echo "<div class='fenye'>";
  134. echo '<span>共'.$max_page.'页</span>';
  135. if( !$paged ){
  136. $paged = 1;
  137. }
  138. if( $paged != 1 ) {
  139. echo "<a href='".get_pagenum_link(1) ."' class='extend' title='跳转到首页'>首页</a>";
  140. }
  141. previous_posts_link('上一页');
  142. if ( $max_page >$range ) {
  143. if( $paged <$range ) {
  144. for( $i = 1; $i <= ($range +1); $i++ ) {
  145. echo "<a href='".get_pagenum_link($i) ."'";
  146. if($i==$paged) echo " class='current'";echo ">$i</a>";
  147. }
  148. }elseif($paged >= ($max_page -ceil(($range/2)))){
  149. for($i = $max_page -$range;$i <= $max_page;$i++){
  150. echo "<a href='".get_pagenum_link($i) ."'";
  151. if($i==$paged)echo " class='current'";echo ">$i</a>";
  152. }
  153. }elseif($paged >= $range &&$paged <($max_page -ceil(($range/2)))){
  154. for($i = ($paged -ceil($range/2));$i <= ($paged +ceil(($range/2)));$i++){
  155. echo "<a href='".get_pagenum_link($i) ."'";if($i==$paged) echo " class='current'";echo ">$i</a>";
  156. }
  157. }
  158. }else{
  159. for($i = 1;$i <= $max_page;$i++){
  160. echo "<a href='".get_pagenum_link($i) ."'";
  161. if($i==$paged)echo " class='current'";echo ">$i</a>";
  162. }
  163. }
  164. next_posts_link('下一页',$max_page);
  165. if($paged != $max_page){
  166. echo "<a href='".get_pagenum_link($max_page) ."' class='extend' title='跳转到最后一页'>尾页</a>";
  167. }
  168. echo "</div>\n";
  169. }
  170. }
  171. ?>