在使用搜索查询时,如何为_Content()中找到的每个结果进行循环?

时间:2020-04-22 作者:Keusta

我正在做一个自定义搜索结果页,其中包含从搜索查询前后的\\u content()中提取一些单词的函数。(例如?s=冷结果=[…昨天是cool 和par…]

它正在工作,但我只获取内容中最后一项的摘录。

how to find position for each item found ? 我知道逻辑,但不知道如何正确编码,不想使用任何像revelanssi这样的插件。

最好的方法是什么?

我的代码functions.php

function show_search_blog(){
    $title = get_the_title();
    // change case for better results
    $mystring = strtolower($title);
    $findme   = get_search_query();
    $pos = strpos($mystring, $findme);
    // vars
    $newContent = $exctractBeforeQuery = $exctractAfterQuery = \'\';
    //
    if ($pos === false) {
        $link = get_the_permalink();
        $newContent .= "<a href=\'".$link."\'>".$title."</a> ";
        // search without tags
        $content = wp_strip_all_tags(get_the_content());

        $pos = strpos($content, $findme);
        // $pos return only value for the last found
        // place for the loop for each keyword found
        if($pos==\'\'){
        $newContent .= "(Debug : keyword found in tags (like in href link attr)<br>";
        //ignore search query in tags
            }
        else{
            $newContent .= "(Debug : Position".$pos.") - ";
            $exctractAfterQuery = substr($content,($pos+strlen($findme) ),40);
            $exctractBeforeQuery = substr($content,$pos-40,40);
            $newContent .= "[...".$exctractBeforeQuery.\'<strong class="search-highlight">\'.$findme."</strong>".$exctractAfterQuery."...]<br>";
            }
        } 
    else{
    $keys = implode(\'|\', explode(\' \', get_search_query()));
    $title = preg_replace(\'/(\' . $keys .\')/iu\', \'<strong class="search-highlight">\\0</strong>\', $title);
        $link = get_the_permalink();
        $newContent .= "<a href=\'".$link."\'>".$title."</a><br>";
    }
  return $newContent;   
}  

index.php

if(is_search()){
    // check for keyword
    if($_GET[\'s\'] == \'\'){echo \'no keyword - add form again\';}
    else{ 
    // add style for the highlight  
    echo "<style>.search-highlight{background:yellow;}</style>";
    // sanitize
    $s = filter_input(INPUT_GET, \'s\', FILTER_SANITIZE_STRING);
    echo "Search for: ".$s."<br>";
    // reset vars
    $html = $pluriel = $nbrResult = ""; 
    // args 
    $args = array(
        \'posts_per_page\' => -1,
        \'orderby\' => \'date\',
        \'post_type\' => \'post\',
        \'s\' => $s
    );
    // query and loop
  $q = new WP_Query($args);

  if($q->have_posts()){
        while($q->have_posts()){
            $q->the_post();
            $html .= show_search_blog(); // call my function
            }
        $nbrResult = $q->found_posts;
        if($nbrResult>1){$pluriel="s";}
        echo $nbrResult." Result".$pluriel." in the Blog : <br>".$html."<br>";
        wp_reset_postdata();
        }
    else{
        echo "Sorry... nothing found";
        }                                                                                                                                                                                                                                                                                       
    }
}

1 个回复
最合适的回答,由SO网友:Keusta 整理而成

找到了解决方案https://stackoverflow.com/questions/15737408/php-find-all-occurrences-of-a-substring-in-a-string

$lastPos = 0;
$positions = array();

if($pos==\'\'){}
else{
    while (($lastPos = strpos($content, $findme, $lastPos))!== false){
        $positions[] = $lastPos;
        $lastPos = $lastPos + strlen($findme);
        }

    // Displays 
    foreach ($positions as $eachPos){
        $pos = $eachPos;
        $exctractAfterQuery = substr($content,($pos+strlen($findme) ),40);
        $exctractBeforeQuery = substr($content,$pos-40,40);
        $newContent .= "[...".$exctractBeforeQuery.\'<strong class="search-highlight">\'.$findme."</strong>".$exctractAfterQuery."...]<br>";
        }
}
它运行良好,不需要插件。希望这能帮助别人