Problems WordPress category

0

I am taking care of a news site in WordPress and I noticed that when more than one category is selected by subject of the following conflict:

BytheURLI'montheLifestylecategorypageonlyitreturnstheHealth/Beautymatter.Thesesubjectsaremarkedascategory:Lifestyle,Health/Beauty.Thisisthecodeforthecategoriespage

get_header();?><divid="primary" class="content-area">
        <main id="main" class="site-main">
        <div class="container">
            <div class="row">
<?php
        if ( have_posts() ) : ?>

            <header class="page-header">
<?php
$args = array(
    'category_name' => $slug
);
$terms = get_the_terms( get_the_ID(), 'category');
if( !empty($terms) ) :
    $term = array_pop($terms);
    $cor = get_field('cor', $term );
    $slug = $term->slug;
endif;
?>

<h1 style="color:<?php echo $cor; ?>" class="page-title tac"><?php echo $term->name; ?></h1>



            </header><!-- .page-header -->

<!-- Notícia em destaque -->            
<!-- Notícia sozinha na DIV -->
            <?php

                                    $terms = get_the_terms( get_the_ID(), 'category');
                                    if( !empty($terms) ) :
                                        $term = array_pop($terms);
                                        $slug = $term->slug;
                                    endif;

                            // The Query
                            $args = array(
                                'post_type' => 'post',
                                'posts_per_page' => 2,
                                'category_name' => $slug
                            );
                            $query = new WP_Query( $args );
                            if ( $query->have_posts() ) :
                                $cols = 0;
                                $ids = array();
                                while ( $query->have_posts() ) : $query->the_post();
                                $ids[] = get_the_ID();
                                $terms = get_the_terms( get_the_ID(), 'category');
                                    if( !empty($terms) ) :
                                        $term = array_pop($terms);
                                        $cor = get_field('cor', $term );
                                    endif;

                                    ?>
<div class="col-md-6">
    <a href="<?php echo get_permalink(); ?>">
        <h1 class="title-main" style="color:<?php echo $cor; ?>">
            <?php echo get_field('titulo_chamada'); ?>
        </h1>
    </a>

    <a href="<?php echo get_permalink(); ?>">
        <div class="img-destaque img-responsive" style="background-image: url(<?php the_post_thumbnail_url(); ?>); ">
            <div class="tag-category branco" style="background-color: <?php echo $cor; ?>">
                <?php echo $term->name; ?>
            </div>  
        </div>
    </a>
     <?php the_excerpt(); ?>

</div>
                                    <?php

                                endwhile;
                                /* Restore original Post Data */
                                wp_reset_postdata();
                endif;
            ?>
                        <!-- PUBLICIDADE -->
                        <div class="col-md-6 col-xs-12 img-responsive mw100pc" style="float: right;">
                            <?php echo do_shortcode("[pro_ad_display_adzone id=1043]"); ?>
                        </div>

<!-- Quatro primeiras notícias -->
<div class="col-md-12">
     <div class="row">
        <?php

            $terms = get_the_terms( get_the_ID(), 'category');
                            if( !empty($terms) ) :
                                $term = array_pop($terms);
                                $slug = $term->slug;
                            endif;
                            // The Query
                            $args = array(
                                'post_type' => 'post',
                                'post__not_in' => $ids,
                                'posts_per_page' => 4,
                                'category_name' => $slug,
                                ‘showposts’ => 1, 
                                ‘paged’=>$paged
                            );
                            $query = new WP_Query( $args );
                            if ( $query->have_posts() ) :
                                $cols = 0;
                                while ( $query->have_posts() ) : $query->the_post();
                                $ids[] = get_the_ID();
                                $terms = get_the_terms( get_the_ID(), 'category');
                                    if( !empty($terms) ) :
                                        $term = array_pop($terms);
                                        $cor = get_field('cor', $term );
                                    endif;

                                    ?>
            <div class="col-md-3" style="height: 620px;">
                <a href="<?php echo get_permalink(); ?>">
                    <h1 class="title-main" style="color:<?php echo $cor; ?>">
                        <?php echo get_field('titulo_chamada'); ?>
                    </h1>
                </a>

                <a href="<?php echo get_permalink(); ?>">
                    <div class="img-destaque" style="background-image: url(<?php the_post_thumbnail_url(); ?>); ">
                        <div class="tag-category branco" style="background-color: <?php echo $cor; ?>">
                            <?php echo $term->name; ?>
                        </div>  
                    </div>
                </a>
                <?php the_excerpt(); ?>             

            </div>
            <?php                   
                endwhile;       
                    /* Restore original Post Data */
                    wp_reset_postdata();
                endif;
            ?>
     </div>
</div>

<!-- Resto das noticias da categoria -->
        <div class="col-md-12">
            <div class="slide-noticias">
            <?php

            $terms = get_the_terms( get_the_ID(), 'category');
                                    if( !empty($terms) ) :
                                        $term = array_pop($terms);
                                        $slug = $term->slug;
                                    endif;
                            // The Query
                            $args = array(
                                'post_type' => 'post',
                                'post__not_in' => $ids,
                                'posts_per_page' => 4,
                                'category_name' => $slug,
                                ‘showposts’ => 1, 
                                ‘paged’=>$paged
                            );
                            $query = new WP_Query( $args );
                            if ( $query->have_posts() ) :
                                $cols = 0;
                                while ( $query->have_posts() ) : $query->the_post();
                                $ids[] = get_the_ID();
                                $terms = get_the_terms( get_the_ID(), 'category');
                                    if( !empty($terms) ) :
                                        $term = array_pop($terms);
                                        $cor = get_field('cor', $term );
                                    endif;

                                    ?>
                                    <div class="col-md-3" style="height: 620px;"><a href="<?php echo get_permalink(); ?>">
                                    <h1 class="title-main" style="color:<?php echo $cor; ?>"><?php echo get_field('titulo_chamada'); ?></h1></a>
                                        <a href="<?php echo get_permalink(); ?>">
                                        <div class="img-destaque" style="background-image: url(<?php the_post_thumbnail_url(); ?>); ">



                                        <div class="tag-category branco" style="background-color: <?php echo $cor; ?>"><?php echo $term->name; ?></div> 
                                        </div>
                                        </a>
                                        <?php the_excerpt(); ?>             

                                    </div>
                                    <?php

                                endwhile;


                                /* Restore original Post Data */
                                wp_reset_postdata();
                endif;
            ?> 
            </div>
        </div>

            <!-- Paginação -->

            <div class="clear"></div>

            <br />
            <br />          

            <!--BANNER-->
                 <div class="col-xs-12 img-responsive" id="closer">
                    <?php echo do_shortcode("[pro_ad_display_adzone id='1049' class='banner-categoria']"); ?>
             <a hrfe="#" class="modal-close hidden-xs" onclick="document.getElementById('closer').style.display='none'"></a>
                </div>  <!-- /.publicidade -->

                        <div class="clear"></div>


<!-- PROVISÓRIO -->

<hr>

                        <?php
                                // The Query
                                $args = array(
                                    'post_type' => 'post',
                                    'category_name' => $term->slug,
                                    'post__not_in' => $ids,
                                    'posts_per_page' => 10
                                );
                                $query = new WP_Query( $args );
                                if ( $query->have_posts() ) :

                                    $colun = 1;
                                        while ( $query->have_posts() ) : $query->the_post();
                                        $terms = get_the_terms( get_the_ID(), 'category');
                                        if( !empty($terms) ) :
                                            $term = array_pop($terms);
                                            $cor = get_field('cor', $term );
                                        endif;
                                        ?>
                                            <?php if($colun == 1 or $colun == 6) : ?><div class="col-md-6"><?php endif; ?>
                                            <a href="<?php echo get_permalink(); ?>" style="color:<?php echo $cor; ?>"><h1 class="title-resume"><?php echo get_field('titulo_chamada'); ?></h1></a>
                                            <?php echo get_the_date(); ?>
                                            <hr>
                                            <?php if($colun == 5 or $colun == 10) : ?></div><?php endif; ?>
                                        <?php

                                        $colun++;

                                    endwhile;
                                    /* Restore original Post Data */
                                    wp_reset_postdata();
                                endif;
                            ?>
<?php endif; ?> <!-- DEVE PERMANECER NA PÁGINA -->

        </main><!-- #main -->
    </div><!-- #primary -->

    <br />
    <br />
    <br />

<?php
get_footer();
?>
<script type="text/javascript" async src="https://d335luupugsy2.cloudfront.net/js/loader-scripts/7270b716-ffb6-4f76-8de0-9f09b12a4159-loader.js"></script><script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*newDate();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');ga('create','UA-68151385-5','auto');ga('send','pageview');</script><script>$('.slide-noticias').slick({infinite:true,slidesToShow:4,slidesToScroll:4,dots:true,infinite:false,speed:300,slidesToShow:4,slidesToScroll:4,responsive:[{breakpoint:1024,settings:{slidesToShow:3,slidesToScroll:3,infinite:true,dots:true}},{breakpoint:600,settings:{slidesToShow:2,slidesToScroll:2}},{breakpoint:480,settings:{slidesToShow:1,slidesToScroll:1}}//Youcanunslickatagivenbreakpointnowbyadding://settings:"unslick"
    // instead of a settings object
  ]
});
</script>
    
asked by anonymous 10.07.2017 / 17:47

2 answers

0

The problem is here:

$terms = get_the_terms( get_the_ID(), 'category');
    if( !empty($terms) ) :
        $term = array_pop($terms);
        $slug = $term->slug;
    endif;

get_the_terms() brings a list of the categories associated with the post without any special order (I think it comes in the order they were saved in the database), in $term = array_pop($terms) you dispense with all other terms and only the last one. Then throw that term in category_name => $slug and redo the query, you can not bring more than one category like this.

Strictly speaking, as far as layout is concerned, all of these calls to $query = new WP_Query are unnecessary. Just to call the page with the right URL you already have the correct posts to view. The changes made in this piece:

// The Query
$args = array(
    'post_type' => 'post',
    'post__not_in' => $ids,
    'posts_per_page' => 4,
    'category_name' => $slug,
    'showposts' => 1, 
    'paged'=>$paged
);
$query = new WP_Query( $args );

Could be done once, by hook pre_get_posts , and the page would be clean by running just the original query.

ps: a terrible, horrible and not recommended solution to have the posts appear correctly would be to change:

$term = array_pop($terms);
$slug = $term->slug;

by

$term_ids = wp_list_pluck( $terms, 'term_id' );

e 'category_name' => $slug, by 'category__in' => $term_ids

This will make the posts appear, but you continue to do 3 or 4 times more queries to the bank ($$$) than necessary.

    
11.07.2017 / 04:00
-1

I do not understand what the conflict is.

In WordPress, when you select several categories in a post, it puts in the friendly URL of the post the first category that you have selected.

And the post always appears in the selected categories.

The code has nothing wrong, is getting the posts of the selected category, as this post is in both categories, so it will display in that category as well.

    
10.07.2017 / 19:26