使get_adjacent_post()在自定义帖子类型中起作用

问题描述 投票:8回答:2

使用get_adjacent_postprevious_post_linknext_post_link似乎只能识别具有相同职位类型的项目。由于我有2种自定义帖子类型,有没有一种方法可以在所有上一个和下一个帖子类型之间建立链接?

wordpress custom-post-type
2个回答
25
投票

似乎这个问题已经在整个互联网上被提出,没有确定的答案。因此,我从原始的get_adjacent_post创建了自己的函数,并为需要它的其他人定制了它。

[功能

将其放在您的functions.php中

/*
 * Replacement for get_adjacent_post()
 *
 * This supports only the custom post types you identify and does not
 * look at categories anymore. This allows you to go from one custom post type
 * to another which was not possible with the default get_adjacent_post().
 * Orig: wp-includes/link-template.php 
 * 
 * @param string $direction: Can be either 'prev' or 'next'
 * @param multi $post_types: Can be a string or an array of strings
 */
function mod_get_adjacent_post($direction = 'prev', $post_types = 'post') {
    global $post, $wpdb;

    if(empty($post)) return NULL;
    if(!$post_types) return NULL;

    if(is_array($post_types)){
        $txt = '';
        for($i = 0; $i <= count($post_types) - 1; $i++){
            $txt .= "'".$post_types[$i]."'";
            if($i != count($post_types) - 1) $txt .= ', ';
        }
        $post_types = $txt;
    }

    $current_post_date = $post->post_date;

    $join = '';
    $in_same_cat = FALSE;
    $excluded_categories = '';
    $adjacent = $direction == 'prev' ? 'previous' : 'next';
    $op = $direction == 'prev' ? '<' : '>';
    $order = $direction == 'prev' ? 'DESC' : 'ASC';

    $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
    $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type IN({$post_types}) AND p.post_status = 'publish'", $current_post_date), $in_same_cat, $excluded_categories );
    $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;
}

用法

基本用途

// Custom post types can be array() or string
$post1 = mod_get_adjacent_post('prev', array('post', 'custom1', 'custom2'));
$post2 = mod_get_adjacent_post('next', 'custom2');

用于创建上一个/下一个链接

<?php
$prev = mod_get_adjacent_post('prev', array('post', 'custom1', 'custom2'));
$next = mod_get_adjacent_post('next', array('post', 'custom1', 'custom2'));
?>

<?php if($prev) : ?>
    <a href="<?php echo get_permalink($prev->ID)?>">&laquo; Go back in time</a>
<?php endif; ?>

<?php if($next) : ?>
    <a href="<?php echo get_permalink($next->ID)?>">Next: <?php echo $next->post_title; ?> &raquo;</a>
<?php endif; ?>

[如果您仍想包含变量$in_same_cat$excluded_categories,您仍然可以修改代码,但是如果这样做,我建议您改用get_adjacent_post,因为这就是它的用途。


0
投票

此答案不再起作用。

我想出了一个新的,很简单:

  • 将此粘贴到您的functions.php中
function custom_posttype_get_adjacent_ID($direction = 'next', $type = 'post', $current) {

    // Get all posts with this custom post type
    $posts = get_posts('posts_per_page=-1&order=DESC&post_type='.$type);

    $postsLength = sizeof($posts)-1;
    $currentIndex = 0;
    $index = 0;
    $result = 0;

    // Iterate all posts in order to find the current one
    foreach($posts as $p){
        if($p->ID == $current) $currentIndex = $index;
        $index++;
    }
    if($direction == 'prev') {
        // If it's 'prev' return the previous one unless it's the first one, in this case return the last. 
        $result = !$currentIndex ? $posts[$postsLength]->ID : $posts[$currentIndex - 1]->ID;
    } else {
        // If it's 'next' return the next one unless it's the last one, in this case return the first. 
        $result = $currentIndex == $postsLength ? $posts[0]->ID : $posts[$currentIndex + 1]->ID;
    }
    return $result;
}

现在您需要下一个上一个帖子ID的地方,只需使用如下功能:

custom_posttype_get_adjacent_ID('prev', 'project', get_the_ID());

评论:-如果您愿意,可以随时替换get_the_ID()为您当前的帖子ID它。-第一个参数预期为“下一个”或“上一个”,它回退为“下一个”。-第二个参数必须是自定义帖子类型名称。您可以在register_post_type()功能中找到它。它回落到“发布”。-如果最后一个参数为空,它将不起作用。

如果您想要下一个或上一个帖子永久链接,可以这样使用:

<?php echo get_permalink(custom_posttype_get_adjacent_ID('prev', 'project', get_the_ID())); ?>

因此带有标签,它看起来像这样:

<a href="<?php echo get_permalink(custom_posttype_get_adjacent_ID('prev', 'project', get_the_ID())); ?>">Previous Project</a>

无法对其进行大量测试,因此如果在某些情况下无法正常工作,请告知我,我将尝试对其进行修复/改进。

© www.soinside.com 2019 - 2024. All rights reserved.