Category Wordpress Problems

Asked

Viewed 177 times

0

I am taking care of a news site in Wordpress and I realized that when it is selected more than one category by matter of the following conflict:inserir a descrição da imagem aqui

By the URL I am on the page of the category Lifestyle only that it returns the matter of Health/ Beauty. These matters are marked as category :Lifestyle, Health/Beauty. This is the code of the categories page

get_header(); ?>
    <div id="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*new Date();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
      }
    }
    // You can unslick at a given breakpoint now by adding:
    // settings: "unslick"
    // instead of a settings object
  ]
});
</script>

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 that were recorded in the bank), there at $term = array_pop($terms) you waive all other terms and get only the last one. Here plays this term on category_name => $slug and redoes the query, there is no way to bring more than one category like this.

Strictly speaking, and from what you can see from the layout, all these calls to $query = new WP_Query are unnecessary. Just calling the page with the right URL you already have the right posts to display. 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 the hook pre_get_posts, and the page would be clean, running only the original query.

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

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

for

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

and 'category_name' => $slug, for 'category__in' => $term_ids

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

  • His solution worked in parts, he began to return the matters but he brought all the matters related to Lifestyle. For example: in Fashion he is bringing Health/Beauty, Lifestyle, in Health/Beauty he brings Fashion, Lifestyle and I want him to bring only the chosen category. And on the gastronomy page persists the same before. :/

  • In Fashion he is bringing posts that nay has the fashion category?

  • I just saw that I had overwritten the $Terms variable in my example. I switched to $term_ids there, see if anything changes.

  • I switched the variable and it didn’t change anything. Regarding the first question, is returning all posts (lifestyle, fashion, health/beauty) and it was to return only fashion or just health/beauty, depending on the page that the guy selected. See: http://piradigital.com.br/categoria/moda

  • The first post (Column Ergolife:) that this marked as health and beauty, tb is as lifestyle (by URL). It does not have the fashion category?

  • Not the only trendy posts are: "Four nineties trends that have come back with everything!" and "Dress Code Fashion Italy brings fashion and behavior tips"

  • Do you have any idea what it might be?

Show 2 more comments

-1

I don’t understand what conflict.

In Wordpress, when you select several categories in a post, it puts in the friendly post URL the first category q vc selected.

And the post always appears in the selected categories.

The code has nothing wrong, You’re picking the posts of the selected category, as this post is in the two categories, so, will display in this category also.

  • Explaining is a bit confusing...enter the site piradigital.com.br, goes in the culture category or in lifestyle. Lifestyle will return only health/beauty and culture will return only entertainment

  • I still don’t understand. What was to return? There are stories in the Lifestyle category that are not appearing, only the Health/Beauty materials?

  • This is just appearing from health/beauty.... but like if I enter the Lifestyle page will appear health/beauty, that q is the problem. On the page Lifestyle was to appear only lifestyle and n health and beauty

  • To be a little clearer, if you go back to the site it was for the other categories to be working the same as the categories 'city' and 'region'.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.