2016-05-29 33 views
0

我想在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); 
+0

如果($后> post_type ==“服务”) – user5200704

+0

尝试,但结果是一样的,不工作。 – SURTLER77

回答

0

有时候在WordPress的,根据不同的情况下,全球$后可能会产生意外结果。因此,诸如$ post-> ID之类的内容可能并不指向您要查找的适当ID。您也可以尝试检查$ post对象,看看它是否符合您的预期;像这样:

 <?php 

      function add_comment_meta_values($idcommento) {   
       global $post; 
       // TRY DUMPING THE $post VARIABLE TO SEE WHAT YOU GET 
       var_dump($post->ID); 
       var_dump($post->post_type); 
       var_dump($post); 
       exit; 

       $idcommento = get_comment_ID(); 
       $tipodipost = get_post_type($post->ID); 

       // ALTHOUGH THIS IS ESSENTIALLY THE SAME AS WHAT YOU DID 
       if($post->post_type == '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); 

检查结束后,你肯定知道的,哪些是没有地方在你的代码行...

+0

我如何看到var_dump函数的具体位置? – SURTLER77

+0

好的我已经提交了评论和结果var_dump是这个“NULL NULL NULL”,所以为每个变量接收一个null,为什么? – SURTLER77