最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - get_the_category return empty inside loop

programmeradmin0浏览0评论

I'm creating a custom template for my website and I'm currently using this code in order to loop to my multisite worpress and get the latest posts:

global $post;
global $wp_query;

$subsites = get_sites();
foreach( $subsites as $subsite ) {
    $subsite_id = get_object_vars($subsite)["blog_id"];
    $subsite_name = get_blog_details($subsite_id)->blogname;
    switch_to_blog($subsite_id);
    $blog_posts = get_posts(); 
    restore_current_blog();
    foreach( $blog_posts as $post ) {
        setup_postdata( $post );
        get_template_part( 'theme-partials/post-templates/loop-content/masonry' );
    }
}

Inside the template_part the theme is trying to get the category for the post but return an empty array.

if ( pixelgrade_option( 'blog_show_categories' ) ) {
        $categories = get_the_category();
        if ( ! is_wp_error( $categories ) && ! empty( $categories ) ) { ?>

How could I get the category information inside the loop? What's wrong in my code?

I'm creating a custom template for my website and I'm currently using this code in order to loop to my multisite worpress and get the latest posts:

global $post;
global $wp_query;

$subsites = get_sites();
foreach( $subsites as $subsite ) {
    $subsite_id = get_object_vars($subsite)["blog_id"];
    $subsite_name = get_blog_details($subsite_id)->blogname;
    switch_to_blog($subsite_id);
    $blog_posts = get_posts(); 
    restore_current_blog();
    foreach( $blog_posts as $post ) {
        setup_postdata( $post );
        get_template_part( 'theme-partials/post-templates/loop-content/masonry' );
    }
}

Inside the template_part the theme is trying to get the category for the post but return an empty array.

if ( pixelgrade_option( 'blog_show_categories' ) ) {
        $categories = get_the_category();
        if ( ! is_wp_error( $categories ) && ! empty( $categories ) ) { ?>

How could I get the category information inside the loop? What's wrong in my code?

Share Improve this question asked Mar 27, 2020 at 16:59 CrazYoshiCrazYoshi 33 bronze badges 3
  • Note that by calling get_posts without specifying 'suppress_filters' => false WP can't use any caching or object caches, slowing things down considerably. It would be easier to just use a normal WP_Query loop instead. You should also check that switch_to_blog actually worked, but this code doesn't check if it returned true or false – Tom J Nowell Commented Mar 27, 2020 at 17:26
  • I read in the doc that get_posts function internally call WP_Query. In addition the function switch_to_blog always return true, is it correct? How can I change the get_posts into a WP_Query? Could you give me an example? – CrazYoshi Commented Mar 28, 2020 at 8:36
  • It does, but that doesn't mean it has identical behaviour. As for how to change them, that's a good question to ask, but a quick look at how to use WP_Query should make it obvious from the official docs. But comments aren't for answering questions or asking new ones – Tom J Nowell Commented Mar 28, 2020 at 16:41
Add a comment  | 

1 Answer 1

Reset to default 0

The problem is here:

    switch_to_blog($subsite_id);
    $blog_posts = get_posts(); 
    restore_current_blog();
    foreach( $blog_posts as $post ) {
        setup_postdata( $post );
        get_template_part( 'theme-partials/post-templates/loop-content/masonry' );
    }

Your code fetches a number of posts, but then you switch back to the original blog! So now post number 5 means something completely different than it did before.

You need to stay in that other blogs context for as long as you're working with that blogs data, or all the references will be incorrect. Once you restore the original blog you can't use that data.

Additionally, switching to another blog just changes the data that comes back, it won't load in plugins and code. This means only the filters, taxonomies, post types of the original blog are loaded.

发布评论

评论列表(0)

  1. 暂无评论