如何在搜索结果中对单个帖子使用NEXT_POST_LINK和PREVICE_POST_LINK

时间:2011-09-13 作者:mike23

我正在使用next_post_linkprevious_post_link 在我的单篇文章模板上,可以从一篇文章导航到另一篇文章,到目前为止还不错。

但如果我进行搜索并单击结果帖子,那么next_post_link 不会将我带到下一个结果帖子,而是按默认帖子顺序带到下一个帖子。

有没有办法next_post_linkprevious_post_link 根据上下文以不同的方式行事?在搜索上下文中,仅在搜索结果等中浏览。

EDIT : 我正在寻找一个解决方案,将与自定义的职位类型。

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

在Bainternet上面的答案的基础上,我编写了这个快速插件,使其更加通用。您可能可以修改底部的链接构建函数,以更准确地执行您想要的操作。

<?php 
/* 
Plugin Name: Search Context
Description: Use search context on single post pages when they\'re reached from a search results page to adjust the prev/next post links.
Author: Otto
*/

add_action(\'init\',\'search_context_setup\');
function search_context_setup() {
    global $wp;
    $wp->add_query_var(\'sq\');

    add_filter(\'previous_post_link\',\'search_context_previous_post_link\');
    add_filter(\'next_post_link\',\'search_context_next_post_link\');

    if (is_search()) {
        add_filter(\'post_link\',\'search_context_add_search_context\');
    }   
}

function search_context_add_search_context($link) {
    $sq = get_search_query();
    if ( !empty( $sq ) )
        $link = add_query_arg( array( \'sq\' => $sq ), $link );
    return $link;
}

function search_context_previous_post_link($link) {
    $sq = get_query_var(\'sq\');
    if (empty($sq)) return $link;

    return get_search_context_adjacent_link($link, $sq, true);
}

function search_context_next_post_link($link) {
    $sq = get_query_var(\'sq\');
    if (empty($sq)) return $link;
    return get_search_context_adjacent_link($link, $sq, false);
}

function get_search_context_adjacent_link($link, $sq, $previous) {
    global $post, $search_context_query;
    if ( !$post ) return $link;

    if (empty($search_context_query)) {
        $search_context_query = get_posts(array(
            \'posts_per_page\' => -1,
            \'post_type\' => \'post\',
            \'post_status\' => \'publish\',
            \'fields\' => \'ids\',
            \'s\' => $sq,
            )
        );
    }

    $key = array_search($post->ID, $search_context_query);

    if ($previous) $key--;
    else $key++;

    if (!isset($search_context_query[$key])) return \'\';

    $adjpost = get_post($search_context_query[$key]);

    $title = $previous ? \'Previous Post\' : \'Next Post\';
    $rel = $previous ? \'prev\' : \'next\';
    $permalink = add_query_arg( array( \'sq\' => $sq ), get_permalink($adjpost) );

    $string = \'<a href="\'.$permalink.\'" rel="\'.$rel.\'">\';
    $output = $string . $title . \'</a>\';

    return $output;
}
对于自定义帖子类型,您可能必须将“post\\u link”过滤器更改为“post\\u type\\u link”过滤器。您还需要调整函数以检查自定义帖子类型。像这样:

...
if (is_search()) {
   add_filter(\'post_type_link\',\'search_context_add_search_context\',10,2);
}
...
以及

function search_context_add_search_context($link, $post) {
    if ($post->post_type != \'YOUR-CUSTOM-TYPE\') return $link;
    $sq = get_search_query();
    if ( !empty( $sq ) )
        $link = add_query_arg( array( \'sq\' => $sq ), $link );
    return $link;
}
在get\\u search\\u context\\u neighboration\\u link函数中,您还需要更改查询中的post\\u type值。

SO网友:Bainternet

这不是WordPress的工作方式,这意味着一旦你从搜索结果中输入一篇文章,你就会失去搜索上下文,WordPress无法判断你是来搜索结果、存档、分类页还是其他什么。

我能想到的唯一方法是创建自定义搜索结果页面,在该页面中,您应该向单个贴子链接添加参数,该链接将保存搜索查询字符串和结果编号,例如:

$post_counter = 1;
while (have_posts()){
    the_post();
    ?>
    <a href="<?php echo add_query_arg( array(\'sq\' => get_search_query(),\'offset_number\' =>$post_counter), get_permalink() ); ?>"><?php the_title(); ?></a>
    <?php
    $post_counter = $post_counter + 1;
}
然后在你的单曲里。使用的php文件next_post_linkprevious_post_link 在打印前检查您是否来自表单搜索结果,然后创建查询以获取正确的链接,例如:

if (get_query_var(\'sq\')){
    global $post;
    $temp = $post;
    //create a short query to get the right links based on search.
    $offset = (get_query_var(\'sq\')) ? get_query_var(\'sq\'): 0;
    $sq = get_posts(array(
        \'posts_per_page\' => 3, //we only need 3  PREVIOUS < this we know > NEXT
        \'post_type\' => \'post\',
        \'post_status\' => \'published\',
        \'s\' => get_query_var(\'sq\'), //this is to create a search query
        \'offset\', => $offset
    ));
    $counter = 0;

    foreach ($sq as $s){
        if ($offset == 0){ //this is the first result so no privous link should be printed only next
            if ($s->ID == $temp->ID){// look for current post as pivot
                $counter = $counter +1;
                if (isset ($sq[$counter])){
                    //next result exsits so we print it out
                    echo \'<a href="\'.add_query_arg( array(\'sq\' => get_search_query(),\'offset_number\' =>($offset+1)), get_permalink($sq[$counter ]->ID) ).\'">Next Post</a>\';
                }
            }
        }else{
            if ($s->ID == $temp->ID){ // look for current post as pivot
                $counter = $counter - 1;
                if (isset ($sq[$counter])){
                    //Previous result exsits so we print it out
                    echo \'<a href="\'.add_query_arg( array(\'sq\' => get_search_query(),\'offset_number\' =>($offset-1)), get_permalink($sq[$counter ]->ID) ).\'">Previous Post</a>\';
                }
                $counter = $counter + 2;
                if (isset ($sq[$counter])){
                    //next result exsits so we print it out
                    echo \'<a href="\'.add_query_arg( array(\'sq\' => get_search_query(),\'offset_number\' =>($offset+1)), get_permalink($sq[$counter ]->ID) ).\'">Next Post</a>\';
                }
            }
        }
        $counter = $counter +1;
    }   

}else{
    //pring your regular links using next_post_link and previous_post_link
}
这很混乱,可能不是最有效的方法,但它确实有效。

结束

相关推荐

为什么‘EXCLUDE_FROM_SEARCH’从WP_QUERY中排除自定义帖子类型?

在WP 3.1中,似乎设置\'exclude_from_search\' = TRUE 对于自定义帖子类型,不仅从前端的搜索中排除帖子类型,而且从使用\'post_type\' = \'all\'.我可以想象很多情况下,插件开发人员希望访问所有帖子类型,甚至是那些被排除在前端搜索之外的帖子类型。这是一个bug,还是只是文档记录不足?背景故事:我有一个自定义的帖子类型,使用自定义的“帖子到期日期”字段。为了自动取消发布过期的自定义帖子,我创建了一个内容过期插件,该插件使用wp\\u schedule\\u