• Resolved Jaaaarne

    (@jaaaarne)


    Recently I found out one thing that has disrupted my plans a bit. I’d really like to fix it, because I currently cannot find any workarounds.

    When all posts are listed on the index page (or on archive view), there is a date and a category listed for each post. However, when a post belongs to more than one category, only one of them is listed (the first by alphabetical order, I believe).

    Listing only one category is quite okay with me. However, I’d like it to be the higher level category instead of the first by alphabetical order (or whatever other ordering is applied, I’m not sure).

    For example, a post belongs to a parent “Category” and a child “Sub-Category”. I’d like to only have the “Category” listed, even if its older or starts later in the alphabet.

    Is it possible to achieve?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Theme Author Ben Sibley

    (@bensibley)

    Yea one of Tracks’ users actually came up with a nice solution for this: https://wordpress.org/support/topic/main-category-of-blog-post?replies=4#post-7714797

    Here’s the function:

    function ct_tracks_category_link() {
            $category      = get_the_category();
    
    		$category_name = array();	//create array for all category names of the element
    		$category_link = array();	//create array for all category links of the element
    
    		foreach($category as $cat) {
    
    			$category_parent_id = $cat->category_parent;
    
    			if ($category_parent_id) {
    				$category_parent_link = get_category_link($category_parent_id);
    				$category_parent_name = get_cat_name($category_parent_id);
    				$html_parent          = "<a href='" . $category_parent_link . "'>" . $category_parent_name . "</a> / ";
    			}
    
    			$category_link[] = get_category_link( $cat->term_id );	//save all category names of the element
    			$category_name[] = $cat->cat_name;						//save all category links of the element
    
    		}
    
    		for ($index=0; $index<sizeof($category_name); $index++) {
    			if ($category_name[$index] != $category_parent_name) {
    				$html         .= "<a href='" . $category_link[$index] . "'>" . $category_name[$index] . "</a> & ";
    			}
    		}
    
    		$html = $html_parent . $html;	//place parent at first place and add the "lower" categories after it
    
    		$html = substr($html, 0, -3);
    
            echo $html;
        }

    This function can be copied and pasted into the functions.php file of a child theme.

    If that all sounds like gibberish let me know and I can put together a child theme for you 😉

    Thread Starter Jaaaarne

    (@jaaaarne)

    Thanks for the offer, but I already have a child theme which I’ve been customizing for a couple of months now. 🙂 And I have no problem whatsoever with copying and pasting. 🙂

    However, this solution is not quite what I had in mind. It lists all categories the post belongs to. I was content with having only one, only I wanted it to be the parent category that is listed, in case the post belongs to a subcategory as well.

    Well, maybe that is actually tricky.

    Theme Author Ben Sibley

    (@bensibley)

    Woops sorry I didn’t remember that correctly.

    Try this instead:

    function ct_tracks_category_link() {
    		$category = get_the_category();
    		$category = $category[0];
    		if ( $category->parent > 0 ) {
    			$category = get_category( $category->parent );
    		}
    		$category_link = get_category_link( $category->term_id );
    		$category_name = $category->cat_name;
    		$html          = "<a href='" . esc_url( $category_link ) . "'>" . esc_attr( $category_name ) . "</a>";
    		echo $html;
    	}

    This is very similar to the original function, but I’ve updated it to check if the selected category has a parent category. If it does, the category being used is then switched to the parent category.

    Thread Starter Jaaaarne

    (@jaaaarne)

    Thanks, this works great!

    Have I already mentioned that you provide wonderful support on top of crafting wonderful themes? 🙂 I would’ve bought a pro version to acknowledge that, if not for the current exchange rate situation. :/ Unfortunately, it is only a “thank you” at the moment.

    Theme Author Ben Sibley

    (@bensibley)

    No worries, I’m happy to help 🙂

    Would you mind taking a minute to leave a review instead? I would really appreciate it.

    Review Tracks

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Category links on posts on the index page’ is closed to new replies.