最近在使用WordPress制作一個企業網站,因為是企業網站所以文章和頁面都不需要評論功能,因此在主題里禁用掉了評論功能
//禁用頁面和文章的評論功能
//add_filter('the_posts','htl_disable_page_comments');
//添加時禁用頁面和文章的評論功能
add_filter('add_posts','htl_disable_page_comments');
function htl_disable_page_comments( $posts ){ //if( is_page()){ $posts[0]->comment_status ='disabled'; $posts[0]->ping_status ='disabled'; //} return $posts; } //禁用WordPress的Pingback和Trackback功能 add_filter('xmlrpc_methods','remove_xmlrpc_pingback_ping'); function remove_xmlrpc_pingback_ping( $methods ){ unset( $methods['pingback.ping']); return $methods; };
但網站需要有一個聯系我們頁面即留言功能,於是我就在該頁面上通過改造WP的評論功能來實現留言
//評論自定義字段 function add_comment_meta_values($comment_id){ //地址 if(isset($_POST['address'])){ $address = wp_filter_nohtml_kses($_POST['address']); add_comment_meta($comment_id,'address', $address,false); } // phone if(isset($_POST['Phone'])){ $Phone = wp_filter_nohtml_kses($_POST['Phone']); add_comment_meta($comment_id,'Phone', $Phone,false); } // comolay if(isset($_POST['comolay'])){ $comolay = wp_filter_nohtml_kses($_POST['comolay']); add_comment_meta($comment_id,'comolay', $comolay,false); } }//end評論自定義字段 add_action ('comment_post','add_comment_meta_values',1); //添加評論自定義字段標題 function add_comment_meta_title( $columns ) { return array_merge( $columns, array( 'address'=>'地址', 'Phone'=>'聯系電話', 'comolay'=>'公司', )); }//end添加評論自定義字段標題 add_filter('manage_edit-comments_columns','add_comment_meta_title'); //輸出自定義字段值 function echo_comment_column_value( $column, $comment_ID ) { echo get_comment_meta( $comment_ID, $column ,true); } add_filter('manage_comments_custom_column','echo_comment_column_value',10,2);
然后在留言模板文件中進行制作后並測試
<?php /*TemplateName:購物車模板 *@author htl *@date2014-12-03 */ get_header(); //echo the_id(); ?> <?php get_sidebar()?> <div id="neirong"> <?php $query = new WP_Query( array('post_type'=>'post','order'=>'DESC','orderby'=>"ID",'p'=>'3')); if(isset($query)&& $query->have_posts()): ?> <?php while($query->have_posts()): $query->the_post(); ?> <!-- html代碼--> <?php endwhile; else: get_template_part('error'); endif; ?> </div> <?php if( comments_open()):?> <!--評論html代碼--> <?php endif;?> </div> <?php get_footer();?>
但結果卻返回“抱歉,該項目的評論已關閉”,一開始以為是頁面中沒有開啟評論,后來查看該頁面已經開啟,然后又將function中的"
禁用頁面和文章的評論功能"給刪除掉結果還是不行
一直不知道什么原因,於是在頁面將當面頁面的信息打印出來,而"post_status"卻為“disabled”,但數據庫中明明為“open”,再一看打印出來的信息,跟我url中的信息完全不一樣
<?php $post=get_post(the_id()); print_r($post);//post_status="disabled" if( comments_open()):?>
但當我把當前頁面打印的信息移到$query前面時信息又對了,評論也是開啟的。
最后在”
露兜博客“上找到一篇文章,原來如果在當前主循環中調用query_posts,WP_Query等方法,那么當前主循環將會被改變,而在調用the_xx時,此時信息就變成了重新查詢后的信息
知道原因就好解決了,在WordPress文檔中有介紹,通過
wp_reset_query()和
wp_reset_postdata()
方法都可以將被更改過的主循環重新恢復回來
修改后的代碼,購物車模板
<?php endwhile; else: get_template_part('error'); endif; //echo the_id();//此時ID為3,the_posts()的信息為$query的信息 //重置query,否則當前的the_post()為 $query->the_post();,跟當前頁面信息不一致 //在該代碼后面再調用the_xxx將會出現問題,因為當前的the_post信息已經被$query修改掉了 wp_reset_query(); //wp_reset_postdata();使用 new WP_Query方法重新查詢用以恢復當前主循環 //echo the_id();//id為url中的ID,the_posts()的信息為當前頁面的信息 ?>
參考:
