banner



How To Link To A Blog Category In Wordpress

How To Link To Current Category In WordPress

Last updated on:

I was working on a new Premium WordPress theme today and one of the things I was including was custom breadcrumb navigation. In order for it to look best you need to have a structure like this: home > category > post title. Below is the code I used in order to get the current category of any post and link to it.

Display Category Link With Custom Code

Simply paste this code wherever you want your category link to appear. This will display a link to the first category of your post. This code can be placed in any theme template file, even outside the loop, but it won't work when placed in functions.php unless it's "hooked" into an action hook that runs once WordPress has initialized such as "init".

            <?php $get_cat        = get_the_category(); $first_cat      = $get_cat[0]; $category_name  = $first_cat->cat_name; $category_link  = get_category_link( $first_cat->cat_ID ); ?> <a href="<?php echo esc_url( $category_link ); ?>" title="<?php echo esc_attr( $category_name ); ?>"><?php echo esc_html( $category_name ); ?></a>          

Category link for Custom Taxonomy

If you want to display the first category link for custom taxonmy then the code is a bit different. For example if you are using a premium theme such as our "Total WordPress Theme" then you will notice there are custom post types like Portfolio, Staff and testimonials and some of these have custom taxonomies like "Portfolio Category". So if you wanted to display the first category a portfolio post is in then you would do something like this:

            <?php $get_cat        = wp_get_post_terms( get_the_ID(), 'portfolio_category' ); $first_cat      = $get_cat[0]; $category_name  = $first_cat->cat_name; $category_link  = get_category_link( $first_cat->cat_ID ); ?> <a href="<?php echo esc_url( $category_link ); ?>" title="<?php echo esc_attr( $category_name ); ?>"><?php echo esc_html( $category_name ); ?></a>          

Notice how in this example we used wp_get_post_terms() instead of get_the_category() ? That is because get_the_category() will only work for the core category taxonomy in WordPress not for any custom taxonomies.

How To Display Category Link With Yoast SEO Breadcrumbs

Yoast SEO

Your other option is to simply use the breadcrumbs features built into the Yoast SEO plugin. Generally when displaying the current category for a post it's a good idea to display it in your breadcrumbs because it provides an easy navigation across your site for users, but also it can assist in your SEO efforts. Many free and premium WordPress themes actually use and recommend Yoast SEO for adding breadcrumbs because it is easy & effective.

To use the Yoast SEO breadcrumbs feature you'll first need to make sure your WordPress theme is compatible. If it's not this is easy to fix. Simply paste the following code into your theme file where you want to show your breadcrumbs (usually single.php or page.php above the page title):

            <?php if ( function_exists('yoast_breadcrumb') ) {      yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } ?>          

Once your theme is ready you can log into WordPress and go to SEO > Advanced > Breadcrumbs.

Yoast SEO Breadcrumbs

Now you can add your custom breadcrumb settings. Click save and your breadcrumbs will be displayed as you set them!

Subscribe to the Newsletter

Get our latest news, tutorials, guides, tips & deals delivered to your inbox.

How To Link To A Blog Category In Wordpress

Source: https://www.wpexplorer.com/link-category-wordpress/

Posted by: hennesseybecomeavoing.blogspot.com

0 Response to "How To Link To A Blog Category In Wordpress"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel