解决WordPress 页面无法评论的问题

最近在使用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()的信息为当前页面的信息
?>
 
参考:
 
 
 
 
 
 





原文地址:https://www.cnblogs.com/huangtailang/p/4146560.html