douyalin2258 2016-05-29 15:39
浏览 61

关于wordpress function.php的简单条件不起作用

i want add a new field "title" on comment of wordpress, after insert the new input field in a default form of wordpress i added this in my function.php for save the title when new comment are submitted.

this is the code i use for save title:

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);

this code work only when remove the condition :

      if( get_post_type($post->ID) == 'service') {}

and i don't understand why, i have already tried this condition in comment.php or in a footer with simple function like this

function test_function() {

            if( get_post_type($post->ID) == 'service') { echo 'done'; }


}
add_action( 'wp_footer', 'test_function' );

and it's work, so i don't understand why don't work in my primary code, any idea ?

SOLVED MYSELF

THIS IS THE NEW CODE:

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); 
  • 写回答

1条回答 默认 最新

  • duanlumei5941 2016-05-29 15:58
    关注

    Sometimes in Wordpress, depending on the Context, the global $post may give you unexpected result. Thus something like $post->ID may not point to the appropriate ID you are looking for. You may as well try inspecting the $post object to see if it is what you had expected; like so:

            <?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);
    

    After the Inspection, you'd sure know where and what was not OK in your Code...

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题