检查以前的帖子是否属于同一类别

I need something like this:

if (previous_post_is_the_same_category()){
    ...
} else {
    ...
}

How can I do this?

There is a function already in Wordpress to get the previous post https://codex.wordpress.org/Function_Reference/get_previous_post

You can pass a boolean of true as the first parameter to return the previous post which is in the same category, you can then compare the post IDs to see if the previous post is also the previous post in the same category. Something like this...

$previousPost = get_previous_post();
$previousPostInSameCat = get_previous_post(true);
if($previousPost->ID === $previousPostInSameCat->ID){
    ...
}else{
    ...
}

That is very vague. You could have a variable that changes according to the category. Guessing you are pulling all your articles (or whatever) from a while f.ex;

$category = NULL;
while($result = mysqli_fetch_array($query)) {

    if($result['category'] == $category) {
        // Do something
    } else {
        // Do something else
    }

    $category = $result['category'];
}