高级 WordPress 单篇文章分页 - 排除类别 &仅浏览设置类别 [英] Advanced WordPress Single Post Pagination - Exclude Category & Browse Through Only Set Categories

查看:20
本文介绍了高级 WordPress 单篇文章分页 - 排除类别 &仅浏览设置类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的主要博客,其中列出了所有帖子.

I have my main blog, which lists all posts.

我还有一个分类页面,只列出了 1 个类别的帖子.

I also have a category page that lists only posts from 1 category.

主博客

  • 发布 #1
  • 发布 #2
  • 帖子 #3
  • 发布 #4
  • 发布 #5

A 类网页

  • 发布 #1
  • 帖子 #3
  • 发布 #4

B 类页面

  • 发布 #1
  • 帖子 #3
  • 发布 #5

如果用户点击查看帖子,然后使用默认的下一个/上一个链接功能,WordPress 无法知道下一个应该是哪个帖子.

If a user clicks to look at a post, and then uses the default next/prev link functions, there is no way for WordPress to know which post should be next.

例如,如果用户正在查看帖子 #3,下一个帖子应该是 #4 还是 #5?这完全取决于用户来自哪里.

For example, if the user is looking at Post #3, should the next post be #4 or #5? It all depends on where the user came from.

所以我写了下面的代码来回答这个问题,并想我会分享它.

So I wrote the following code to answer this problem and thought I'd share it.

推荐答案

注意:我知道这不是最有效的方法,尤其是当您有数千个帖子时.我很想看到获得下一篇文章的更好方法.

NOTE: I know that this is not the most efficient way to do this, especially if you have thousands of posts. I'd be interested in seeing better ways to get the next post.

将以下内容添加到您的functions.php文件中:

Add the following to your functions.php file:

/*
 * Session Tracking
 */    
    add_action('init', 'start_session', 1);
    function start_session() {
        if(!session_id()) {
            session_start();       
        }
    }
    // Update Cookie trail
    function update_cookie_trail() {
        $_SESSION['ref_category'] = get_query_var('cat');
    }


/*
 * Return next post based off of cookies
 */
    function next_post_from_session($text, $categories) {
        global $post;

        $cat_array = explode(',', $categories);
        $cat_array[] = $_SESSION['ref_category'];

        // Get all posts, exclude Hidden cat
        $args = array(
            'numberposts'     => -1,
            'category'        => implode(',', $cat_array),
            'orderby'         => 'post_date',
            'order'           => 'DESC',
            'post_type'       => 'post',
            'post_status'     => 'publish',
        );
        $allPosts = get_posts( $args );
        $index = 0;

        foreach( $allPosts as $thePost ) {
            $index++;
            if($post->ID == $thePost->ID) {
                $nextPost = $allPosts[$index++];
                $url = get_permalink($nextPost->ID);
                $a = '<a href="'.$url.'" title="'.esc_attr($nextPost->post_title).'" />'.$text.'</a>';
                return $a;
            }
        }
    }

/*
 * Return previous post based off of cookies
 */
    function previous_post_from_session($text, $categories) {
        global $post;

        $cat_array = explode(',', $categories);
        $cat_array[] = $_SESSION['ref_category'];

        // Get all posts, exclude Hidden cat
        $args = array(
            'numberposts'     => -1,
            'category'        => implode(',', $cat_array),
            'orderby'         => 'post_date',
            'order'           => 'DESC',
            'post_type'       => 'post',
            'post_status'     => 'publish',
        );
        $allPosts = get_posts( $args );
        $index = 0;

        foreach( $allPosts as $thePost ) {
            if($post->ID == $thePost->ID) {
                $prevPost = $allPosts[$index-1];
                $url = get_permalink($prevPost->ID);
                $a = '<a href="'.$url.'" title="'.esc_attr($prevPost->post_title).'" />'.$text.'</a>';
                return $a;                
            }
            $index++;
        }
    }


/*
 * Generate a "back" URL to the previous category page based off session data
 */
    function previous_category_permalink() {
        $ref_url = $_SESSION['ref_category'];
        return get_category_link($ref_url);
    }

然后在显示多篇博客文章的任何 category.php 或页面上(如相关文章"部分),运行此函数:

And then on any category.php or page where you show several blog posts (like a "related posts" section), run this function:

update_cookie_trail();

然后在您的 single.php 上您可以使用以下功能.

Then on your single.php you can use the following functions.

<?php echo next_post_from_session('Next', '-24, 10'); ?>

<a class="close" href="<?php echo previous_category_permalink(); ?>">
    Back
</a>                      

<?php echo previous_post_from_session('Previous Post', '-24, 10'); ?>

'-24, 10' 是一个参数,允许您通过逗号分隔的 ID 排除或特别包括类别.

The '-24, 10' is a parameter that allows you to exclude, or specifically include categories via comma separated ID's.

这篇关于高级 WordPress 单篇文章分页 - 排除类别 &amp;仅浏览设置类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆