doudi8298 2015-10-06 20:29
浏览 5

从用户输入中查找类似的wordpress帖子

im trying to show the closest matching wordpress post depending on user input. I have tried using levenstheins distance as you can see below but it doesn't work. If anyone also know how to show the content of the posts that is selected it would be great.

The code I have this far is below. Thanks!

 $gallery_args = array(
                    'posts_per_page'   => -1,
                    'orderby'=> 'date',
                    'order'=> 'DESC',
                    'post_type'=> 'gallery',
                    'post_status'=> 'publish',
                    'suppress_filters' => true 
        );
        $posts_gallery = get_posts( $gallery_args ); 
        foreach($posts_gallery as $rows){
            $post_titles = $rows->post_title;
        }

       $user_input = //Users input string
       $shortest = -1;

    foreach ($post_titles as $post_title) {

    $lev = levenshtein($user_input, $post_title);

    // check for an exact match
    if ($lev == 0) {

        $closest = $post_title;
        $shortest = 0;

        break;
    }

    if ($lev <= $shortest || $shortest < 0) {
        $closest  = $post_title;
        $shortest = $lev;
    }
}

if ($shortest == 0) {
    echo "Exact match found: $closest
" . "<br />";
} else {
    echo  $closest;
}
}
} 
  • 写回答

1条回答 默认 最新

  • du6029076877 2015-10-08 12:49
    关注

    Your code works, but the $user_input-variable is missing. With that variable set, and a manually set $post_titles-array it will give you the closest matching post_title.

    I've included a function (at top) to display the closest post.

    My testcode:

        <?php
    
       function display_closest_post($thePostID) {
         $post = get_post($thePostID);
         setup_postdata($post);
    
         // display the post here
         the_title();
         the_excerpt();
         the_post_thumbnail();
    
         wp_reset_postdata();
       }
    
        $post_titles = [
            "1" => "car",
            "2" => "bar",
        ];
    
              $user_input = 'cab';
               $shortest = -1;
    
            foreach ($post_titles as $post_title) {
    
            $lev = levenshtein($user_input, $post_title);
    
            // check for an exact match
            if ($lev == 0) {
    
                $closest = $post_title;
                $closestID = get_the_ID(); //Fetch the ID of the post
                $shortest = 0;
    
                break;
            }
    
            if ($lev <= $shortest || $shortest < 0) {
                $closest  = $post_title;
                $closestID = get_the_ID(); //Fetch the ID of the post
                $shortest = $lev;
            }
        }
    
        if ($shortest == 0) {
            echo "Exact match found: $closest
    " . "<br />";
            //Shows the whole post
            display_closest_post($closestID);
        } else {
            echo  $closest;
            //Shows the whole post
            display_closest_post($closestID);
        }
    
        ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计