admin管理员组

文章数量:1122832

I want to display the logo of a metadata from post-type for all post matching same name/category.

Posts-type: are Activities (sport). Posts: are News from these activities

Inside my first loop I get all post (default wp post) matching "carousel" category. Each post contain several categories but only one is use to compare, for example: "name-sport" . So for the first two posts I Have : Post1 cat -> carousel, website-name Post2 cat -> carousel, basketball

In the nested loop I get all post-type. All post-type name correspond to the name of sport. I get two value of these post type in a new array : post-name and metadata "logo".

I compare post category name with the name of post type.If these two values match I get the logo url

Also, I must add separately the logo of website for global news.

The in_array() function stop to work comparing the array of name and logo from post type.

For these two post, only the "website-name" logo is displayed

I think about a conflict between nested loop, but I can't find my mistake.

<?php
if($querynews->have_posts()) :
    while ($querynews->have_posts()) :
        $querynews->the_post();
        $listcategories = get_the_category();
        $cattocompare = array_column($listcategories, 'slug');
                            
        global $post; 
        $backup=$post; // Send data after inner loop
                                
        global $array_official_logo;
        $array_official_logo = [];
        
        $the_logo = ""; 


            $query_sections = new WP_Query(array(
                'post_type'      => 'section_post_type',
                'post_status'    => 'publish',
                'posts_per_page' => -1
            ));
            while ($query_sections->have_posts()) :
                $query_sections->the_post();
                $post_id = get_the_ID();

                array_push($array_official_logo, [
                    'name'    => $post->post_name,
                    'logourl' => get_the_post_thumbnail_url()
                ]);
                                        
            endwhile; //wp_reset_postdata();
                            
                            
        $post=$backup;// Get back data from first loop
    
        array_push($array_official_logo, [
            'name'    => 'website-name',
            'logourl' => $uploadDir.'/official-logo-2024.png'
        ]); 
        
        foreach($array_official_logo as $keyS):
            $the_logo = in_array($keyS['name'], $cattocompare) ? $keyS['logourl'] : "";
        endforeach;
        var_dump($the_logo);

?>

本文标签: