我想在wordpress的评论中添加一个新的字段“标题”,插入新的输入字段后,我在默认的wordpress中添加了这个我的function.php
用于保存新的标题提交评论。WordPress上的简单条件function.php不工作
这是我使用的代码保存标题:
function add_comment_meta_values($idcommento) {
global $post;
$idcommento= get_comment_ID();
$tipodipost= get_post_type($post->ID);
if(get_post_type($post->ID) == 'service') {
if(isset($_POST['title_svz'])) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta($idcommento , 'title_svz', $title, false);
}}
}
add_action ('comment_post', 'add_comment_meta_values', 1);
此代码的工作,只有当删除的条件:
if(get_post_type($post->ID) == 'service') {}
,我不明白为什么,我已经尝试过这条件comment.php或与这样的
function test_function() {
if(get_post_type($post->ID) == 'service') { echo 'done'; }
}
add_action('wp_footer', 'test_function');
,它的工作变得简单功能的页脚,所以我不不明白为什么不能在我的主代码中工作,有什么想法?
解决MYSELF
这是新代码:
function add_comment_meta_values($idcomment) {
$comment_full = get_comment($idcomment);
$idpost = $comment_full->comment_post_ID;
$typepost= get_post_type($idpost);
if($typepost == 'service') {
if(isset($_POST['title_svz'])) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta($idcomment , 'title_svz', $title, false);
} }
}
add_action ('comment_post', 'add_comment_meta_values', 10, 1);
如果($后> post_type ==“服务”) – user5200704
尝试,但结果是一样的,不工作。 – SURTLER77