functions.php 7.2 KB

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