top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to display all categories name in Wordpress page and the selected category will be in bold style?

0 votes
569 views

I want to display all the existing categories in my Wordpress page in the bottom of the content and the selected category will be in bold style, as follows:

For example, for an existing post if I selected category2 of 3 existing, then it show like this

category1 category2 category3

<div class="entry-meta">
<span class="term-links">
<?php foreach ( get_the_terms( $post->ID, 'category') as $term ) : 
?>
<a href="<?php echo esc_url( get_term_link( $term->term_id ) ) 
?>"><span class="<?php echo $term->slug ?>"><?php echo $term->name ?>
</span></a>
<?php endforeach; ?>
</span>

<style>
.term-links .category2 {
display: inline-block;
font-weight:bold;
</style>

In the above code only display the selected category, not showing the all category. How can I do this?

posted Jul 28, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Add below code in your template it shows list of all category

<style type="text/css">
        .single-product div.product .product_meta .product_cat ul li{ list-style-type:circle;}
        .single-product div.product .product_meta .product_cat ul li.current-cat{list-style-type:circle;}
        .single-product div.product .product_meta .product_cat ul li.current-cat a{display: inline-block;font-weight:bold;}
        </style>
        <?php
        global $post;
        $terms = get_the_terms( $post->ID, 'product_cat' );
        $product_cat_id_array = array();
        foreach ($terms  as $term  ) {
            $product_cat_id_array[]  = $term->term_id;
        }
        $product_cat_id_string = implode(",",$product_cat_id_array);

        $args = array(
            'child_of'            => 0,
            'current_category'    => $product_cat_id_string,
            'depth'               => 0,
            'echo'                => 1,
            'exclude'             => '',
            'exclude_tree'        => '',
            'feed'                => '',
            'feed_image'          => '',
            'feed_type'           => '',
            'hide_empty'          => 0,
            'hide_title_if_empty' => false,
            'hierarchical'        => true,
            'order'               => 'ASC',
            'orderby'             => 'name',
            'separator'           => '',
            'show_count'          => 0,
            'show_option_all'     => '',
            'show_option_none'    => __( 'No categories' ),
            'style'               => 'list',
            'taxonomy'            => 'product_cat',
            'title_li'            => __( 'Categories' ),
            'use_desc_for_title'  => 0,
        );
        wp_list_categories($args);
     ?>
answer Aug 21, 2017 by Deepa
Similar Questions
0 votes

I try to get wordpress list categories with count at the end of each category's name by input the parent category as output.

Ex: I have a parent category name "alpha", and its child categories name are, Category A, Category B, Category C, Category D

I want the output display:

-Category A (5)

-Category B (2)

-Category C (6)

-Category D (7)

<?php
    $variable = wp_list_categories( array(
    'show_count' => true,
    'orderby'    => 'name',
    'style'      => 'none'
    ) );
    echo $variable; 
?>

The result of the above code display categories with the count of posts at the end, but it display all categories. Can anybody help me how I fix this problem?

0 votes

I have 4 categories. For each category I will show 5 recent post. I will have category such as breakfast, dessert, lunch and savory food. There will be a "See All" link for each category, so when the user click on "see All" link, can see all post of the particular category. Currently my code looks like this, but I am stuck with the "See All" link. I don't know how to link it to main category.

<?php 
get_header();
?> 
<!-- recipe -->
<section class="recipe-wrap">
    <?php
    /*
     * Loop through Categories and Display Posts within
     */
    $post_type = 'recipe';
    $category_link = get_category_link($cat->cat_ID);

    // Get all the taxonomies for this post type
    $taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );

    foreach( $taxonomies as $taxonomy ) :

        // Gets every "category" (term) in this taxonomy to get the respective posts
        $terms = get_terms( $taxonomy );

        foreach( $terms as $term ) : ?>

    <div class="recipe-category owl-carousel-slide">
        <div class="row">

            <h2><?php echo $term->name; ?><a href="#">see all</a></h2>

            <div class="recipe-category-carousel owl-carousel owl-theme">

                <?php
                $args = array(
                        'post_type' => $post_type,
                        'posts_per_page' => 10,  //show all posts
                        'tax_query' => array(
                            array(
                                'taxonomy' => $taxonomy,
                                'field' => 'slug',
                                'terms' => $term->slug,
                            )
                        )
                    );
                $posts = new WP_Query($args);

                if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>

                <div class="item recipe-box">
                    <a href="<?php the_permalink(); ?>">
                        <img src="<?php echo(types_render_field('artwork', array('raw' => true) )); ?>">
                        <p><?php the_title(); ?></p>
                    </a>
                </div> 

                <?php endwhile; endif; ?>
                    </div>
                    </section>
                <?php endforeach;
            endforeach; ?>
            </div>
        </div>
    </div>
</section>
<!-- /recipe -->
<?php 
get_footer();
?>
0 votes

I want to create a shortcode for category title in wordpress to display the category name, and I found this code:

function categories_list_func( $atts ){
 $categories = get_the_category();

     if($categories) {
        foreach($categories as $category) {
            $output .= '<li class="cat-' . $category->cat_ID . '"><a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "Read more posts from : %s" ), $category->name ) ) . '">'.$category->cat_name.'</a></li>';
        }
        $second_output = trim($output);
      }
      $return_string = '<h4>'.__( "Categories :", "my_site").'</h4><div class="overflow"><ul class="post-categories">' . $second_output . '</ul></div>';

 return $return_string;

} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );

Can anybody help me how I create this shortcode?

0 votes

I am currently working on my wordpress site and have started to use a timeline. There are next/previous buttons in the post, but I want them to display the post title instead of Next/Previous. I have looked over the wordpress code for this, but the dev is using non-standard code to achieve it.

Could somebody take a look at the code and tell me what I need to change.

<div class="clearfix"></div>
            </div>
            <div class="timeline-info">
                <div class="timeline-content">
                    <?php 
                    $content =  preg_replace ('#<embed(.*?)>(.*)#is', ' ', get_the_content(),1);
                    $content =  preg_replace ('@<iframe[^>]*?>.*?</iframe>@siu', ' ', $content,1);
                    $content =  preg_replace ('/<source\s+(.+?)>/i', ' ', $content,1);
                    $content =  preg_replace ('/\<object(.*)\<\/object\>/is', ' ', $content,1);
                    $content =  preg_replace ('#\[video\s*.*?\]#s', ' ', $content,1);
                    $content =  preg_replace ('#\[audio\s*.*?\]#s', ' ', $content,1);
                    $content =  preg_replace ('#\[/audio]#s', ' ', $content,1);
                    preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $content, $match);
                    foreach ($match[0] as $amatch) {
                        if(strpos($amatch,'soundcloud.com') !== false){
                            $content = str_replace($amatch, '', $content);
                        }elseif(strpos($amatch,'youtube.com') !== false){
                            $content = str_replace($amatch, '', $content);
                        }
                    }
                    $content = preg_replace('%<object.+?</object>%is', '', $content,1);
                    echo apply_filters('the_content',$content);?>
                </div>
            </div>
            <?php 
            $we_sevent_navi = get_option('wpex_navi');
            if($we_sevent_navi!='no'){
                $wpex_navi_order = get_option('wpex_navi_order');
                $preevtrsl = get_option('wpex_text_prev')!='' ? get_option('wpex_text_prev') : esc_html__('Previous article','wp-timeline');
                $nextevtrsl = get_option('wpex_text_next')!='' ? get_option('wpex_text_next') : esc_html__('Next article','wp-timeline');
                if($wpex_navi_order!='ct_order'){ ?>
                    <div class="timeline-navigation defa">
                        <div class="next-timeline">
                            <?php next_post_link('%link', $nextevtrsl) ?>
                        </div>
                        <div class="previous-timeline">
                            <?php previous_post_link('%link', $preevtrsl) ?>
                        </div>
                    </div>
                    <?php 
                }else{
                    wpex_next_previous_timeline($preevtrsl,$nextevtrsl);
                }
            }?>
            <div class="clearfix"></div>
        </div>
  </div>
...