如何在特定标签中获取下一个或上一个帖子?

时间:2011-04-07 作者:The How-To Geek

我试图在一个特定的标签内(在帖子页面本身)创建指向下一篇和上一篇帖子的链接,但我似乎找不到这样做的插件或源代码。

我希望能够像这样出现在帖子下面。

get_previous_link("tagname");
get_next_link("tagname");
有人知道实现这一目标的方法吗?否则我就得自己写些东西,这很好,但我想除非我必须这样做,否则我不会重新发明轮子。

3 个回复
SO网友:Jan Fabry

get_adjacent_post(), 它由所有返回(链接到)下一篇或上一篇文章的函数使用,只有$in_same_cat 参数,它查看帖子所在的类别,而不是标记。

你可以钓到get_[next|previous]_post_join 修改呼叫的加入查询,但这样可能更容易copy the function, 删除特定于类别的代码,并将其替换为特定于标记的代码。或者使其更通用,并将其作为补丁提交给WordPress:-)

SO网友:Abhishek Bhardwaj

这对那个问题有效吗?http://digwp.com/2010/04/post-navigation-outside-loop/

该帖子中的代码适用于“存档视图页面”&;“单视图页面”:P

SO网友:Jish

下面是@Jan Fabry在上文中提到的复制/粘贴编辑版本(肯定不是最优雅的解决方案,但应该可以):

/**
 * Retrieve adjacent post.
 *
 * Can either be next or previous post.
 *
 *
 * @param bool $in_same_tag Optional. Whether post should be in same category.
 * @param string $excluded_tags Optional. Excluded tags IDs.
 * @param bool $previous Optional. Whether to retrieve previous post.
 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
 */
function get_adjacent_post_by_tag($in_same_tag = false, $excluded_tags = \'\', $previous = true) {
    global $post, $wpdb;

    if ( empty( $post ) )
        return null;

    $current_post_date = $post->post_date;

    $join = \'\';
    $posts_in_ex_tags_sql = \'\';
    if ( $in_same_tag || !empty($excluded_tags) ) {
        $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";

        if ( $in_same_tag ) {
            $tag_array = wp_get_object_terms($post->ID, \'post_tag\', array(\'fields\' => \'ids\'));
            $join .= " AND tt.taxonomy = \'post_tag\' AND tt.term_id IN (" . implode(\',\', $tag_array) . ")";
        }

        $posts_in_ex_tags_sql = "AND tt.taxonomy = \'post_tag\'";
        if ( !empty($excluded_tags) ) {
            $excluded_tags = array_map(\'intval\', explode(\' and \', $excluded_tags));
            if ( !empty($tag_array) ) {
                $excluded_tags = array_diff($excluded_tags, $tag_array);
                $posts_in_ex_tags_sql = \'\';
            }

            if ( !empty($excluded_tags) ) {
                $posts_in_ex_tags_sql = " AND tt.taxonomy = \'post_tag\' AND tt.term_id NOT IN (" . implode($excluded_tags, \',\') . \')\';
            }
        }
    }

    $adjacent = $previous ? \'previous\' : \'next\';
    $op = $previous ? \'<\' : \'>\';
    $order = $previous ? \'DESC\' : \'ASC\';

    $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_tag, $excluded_tags );
    $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = \'publish\' $posts_in_ex_tags_sql", $current_post_date, $post->post_type), $in_same_tag, $excluded_tags );
    $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );

    $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
    $query_key = \'adjacent_post_\' . md5($query);
    $result = wp_cache_get($query_key, \'counts\');
    if ( false !== $result )
        return $result;

    $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
    if ( null === $result )
        $result = \'\';

    wp_cache_set($query_key, $result, \'counts\');
    return $result;
}

结束

相关推荐

Editor removes <p> tags

如何阻止编辑器剥离<p> 标记和“空( ;)”页面上的div?既然@scribu要求这里提供一个示例代码,那么它就是:输入:<p>text</p> <div>&nbsp;</div> 输出:text