0
votes

I have a loop do display the different categories to go on the archive page.

How can y only display the categories one time ?

Currently, if two posts had the same category, there is two time the category

This is my code below

        <div class="row ptb-20">

            <?php

            $args = array(
                'category_name' => 'actualites',
            );

            // Custom query.
            $query = new WP_Query( $args );

            // Check that we have query results.
            if ( $query->have_posts() ) {

                // Start looping over the query results.
                while ( $query->have_posts() ) {

                    $query->the_post();?>

                    <div class="category-filter">
                        <div class="single-filter">

                            <?php

                            $categories = get_the_category();
                            $separator = ", ";
                            $output = ' ';

                            if ($categories) {

                                foreach ($categories as $category) {

                                    $output .= '<li><a href="' . get_category_link($category->term_id) . '">' . $category->cat_name . '</a></li>';

                                }

                                echo trim($output, $separator);

                            }

                            ?>

                        </div>   
                    </div>

                    <?php

                } // End while 
            } // End if

            else { echo '<p>Aucune actualité trouvée</p>'; } ?>

            <?php wp_reset_postdata(); ?>

        </div>
2

2 Answers

0
votes

Method 1: Exclude a Category from WordPress Using Plugin First thing you need to do is to install and activate the Ultimate Category Excluder plugin. For more details, you should follow our guide on how to install a WordPress plugin.

Upon activation, you’ll need to go to Settings » Category Excluder page. It will display all the categories that are available on your WordPress blog.

Method 2: Exclude a Category from WordPress Homepage Using Code This method requires you to add code to your WordPress files. If you haven’t done this before, then see our guide on how to copy and paste code snippets in WordPress.

You will need to add following code to your theme’s functions.php file or a site-specific plugin.

   function exclude_category_home( $query ) {
    if ( $query->is_home ) {
    $query->set( 'cat', '-5' );
    }
    return $query;
    }


add_filter( 'pre_get_posts', 'exclude_category_home' );

Don’t forget to replace ID (-5) with your category ID. It will hide all blog posts from homepage belonging to the category that matches this ID.

Note: Make sure to add a minus (-) sign with the category ID.

Please refer: https://www.wpbeginner.com/wp-tutorials/how-to-exclude-a-category-from-your-wordpress-homepage/

0
votes

If I understand your issue correctly, you can introduce a variable in which you would remember what categories has already been used, so you don't include them more than once.

    <div class="row ptb-20">

        <?php

        $args = array(
            'category_name' => 'actualites',
        );

        // Custom query.
        $query = new WP_Query( $args );

        // Check that we have query results.
        if ( $query->have_posts() ) {

            // Start looping over the query results.
            while ( $query->have_posts() ) {

                $query->the_post();?>

                <div class="category-filter">
                    <div class="single-filter">

                        <?php

                        $categories = get_the_category();
                        $categories_displayed = [];
                        $separator = ", ";
                        $output = ' ';

                        if ($categories) {

                            foreach ($categories as $category) {
                                if (!in_array($category->cat_name, $categories_displayed)) { 
                                    $output .= '<li><a href="' . get_category_link($category->term_id) . '">' . $category->cat_name . '</a></li>';

                                    $categories_displayed[] = $category->cat_name;
                                }
                            }

                            echo trim($output, $separator);

                        }

                        ?>

                    </div>   
                </div>

                <?php

            } // End while 
        } // End if

        else { echo '<p>Aucune actualité trouvée</p>'; } ?>

        <?php wp_reset_postdata(); ?>

    </div>