Home>

We are developing an original theme for WordPress.
I'm displaying a list of categories on the article details page.
I would like to add a current class to the corresponding category according to the category of the article i am viewing.
I tried various things and couldn't do it well, so I will consult with you.

The cause is that the category cannot be read because it is displayed outside the loop?
Sorry for lack of knowledge.
Please let us know if you have any missing information.
Below is the code.

single.php<? php
/ **
 * The template for displaying post pages
 * Template Name: post_page
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package hamaen
 * /
get_header ();
?><section>                   ">HOME            <?php wp_title ('');?>               salt       <? php wp_title ('');?></section><main><section><p>This page is an information providing page for manufacturers, retailers, specialists, government offices, and other business partners.<br>Please note that we do not sell products directly to general customers.</p><ul>    <li>">What's New</li>    <? php
            $categories = get_categories ('hide_empty = 1&title_li =');
            if (is_array ($categories)) {
                foreach ($categories as $category):
                    $cat_id = $category->cat_ID;$cat_title = $category->cat_name;
                    $cat_url = get_category_link ($cat_id);
                    echo "<li";
                    if (is_category ($cat_id)) {
                        echo'';
                    }
                    echo'>'. $Cat_title.'</li>';
                endforeach;
            }
        ?></ul>            <? php
            // Call 10 articles from the same category
            $categories = get_the_category ($post->ID);
            $category_ID = array ();
            foreach ($categories as $category):
            array_push ($category_ID, $category->cat_ID);
            endforeach;
            $args = array (
            #'post__not_in' =>array ($post->ID), // Excluding the article i am reading
            'posts_per_page'=>-1,
            'category__in' =>$category_ID,
            #'orderby' =>'rand', // Randomly select articles
            );
            $query = new WP_Query ($args);
            if ($query->have_posts ()): while ($query->have_posts ()): $query->the_post ();
            ?>                              ">                 <time datatime = "<? php echo get_the_date ('Y year n month j day');?>" itemprop = "datepublished"><? php echo get_the_date ('Y year n month j day');?></time>                <span>                    <? php $cat = get_the_category ();
                            $cat = $cat [0];
                            {echo $cat->cat_name;}?>                </span>                <p><? php the_title ();?></p>        <? php endwhile;endif;?>        <? php wp_reset_postdata ();?></section><section>     <? php the_title ();?>     <time datatime = "2<? php echo get_the_date ('Y year n month j day');?>" itemprop = "datepublished"><span>Information disclosure date:</span><? php echo get_the_date ('Y Year n month j day');?></Time>                <? php the_content ();?>    </section></main><!-Footer-><? php get_footer ('common');?>

It's hard to understand, so the following is the code that lists this category.
This is an excerpt from the code above.

<ul>    <li>">What's New</li>    <? php
            $categories = get_categories ('hide_empty = 1&title_li =');
            if (is_array ($categories)) {
                foreach ($categories as $category):
                    $cat_id = $category->cat_ID;
                    $cat_title = $category->cat_name;
                    $cat_url = get_category_link ($cat_id);
                    echo "<li";
                    if (is_category ($cat_id)) {
                        echo'';
                    }
                    echo'>'. $Cat_title.'</li>';
                endforeach;
            }
        ?></ul>

That's it.
Please confirm.

  • Answer # 1

    if (is_category ($cat_id)

    Not

    if (in_category ($cat_id)

    Why don't you try it?

Related articles