【问题标题】:Wordpress Relevanssi boost pages with certain word in titleWordpress Relevanssi 提升标题中包含特定单词的页面
【发布时间】:2018-01-25 12:42:49
【问题描述】:

我是 php 新手,我正在尝试提升标题中包含某些单词的页面,使其首先出现在搜索结果页面中。

我尝试修改此代码

/*Relevanssi sort boost exact matches in search results*/
add_filter('relevanssi_results', 'rlv_exact_boost');
function rlv_exact_boost($results) {
    $query = strtolower(get_search_query());
    foreach ($results as $post_id => $weight) {
        $post = relevanssi_get_post($post_id);

                // Boost exact title matches
        if (stristr($post->post_title, $query) != false) $results[$post_id] = $weight * 100;

                // Boost exact matches in post content
        if (stristr($post->post_content, $query) != false) $results[$post_id] = $weight * 100;
    }
    return $results;
}

到这里:

add_filter('relevanssi_results', 'course_title_boost');
function course_title_boost($results) {
    $query = strtolower(get_search_query());
    foreach ($results as $post_id => $weight) {
        $post = relevanssi_get_the_title($post_id);

                // Boost pages with course in the certain titles
        if (stristr($post->post_content, $query) != false) $results[$post_title->strpos(strrpos($post_title, 'courses'))] = $weight * 200;
    }
    return $results;
}

但它不起作用。

我们将不胜感激。

哦,是的,我在 wordpress 4.9.2 上使用 relevanssi 4.0.3

【问题讨论】:

    标签: php wordpress relevanssi


    【解决方案1】:

    终于找到了似乎可以工作的那个。

    我刚刚包括:

            if (strpos($post->post_title, "Courses")) $results[$post_id] = $weight * 100;
    

    所以现在完整的代码如下所示:

    add_filter('relevanssi_results', 'rlv_exact_boost');
    function rlv_exact_boost($results) {
        $query = strtolower(get_search_query());
        foreach ($results as $post_id => $weight) {
            $post = relevanssi_get_post($post_id);
    
                    // Boost pages with "courses" in the title
            if (strpos($post->post_title, "Courses")) $results[$post_id] = $weight * 100;
    
                    // Boost exact matches in post content
            if (stristr($post->post_content, $query) != false) $results[$post_id] = $weight * 85;
    
                            // Boost exact title matches
            if (stristr($post->post_title, $query) != false) $results[$post_id] = $weight * 65;
    
        }
        return $results;
    }
    

    我只会增加重量,直到它适合我的需要。

    希望这可以帮助其他正在寻找的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多