Support » Theme: Tracks » "Main"-Category of Blog Post

Viewing 6 replies - 1 through 6 (of 6 total)
  • Found a solution for myself and understand that retourning the “right” category is tricky. Although returning the parent category would be easy!

    I modified the code in functions.php, that all used categories are shown. That’s no problem for me because i don’t show author and date. 😉

    Maybe some people have the same problem as me and can use my solution to it.

    // for above the post titles
    if( ! function_exists( 'ct_tracks_category_link' ) ) {
        function ct_tracks_category_link() {
            $category      = get_the_category();
    
    		foreach($category as $cat) {
    			$category_link = get_category_link( $cat->term_id );
    			$category_name = $cat->cat_name;
    			$html         .= "<a href='" . $category_link . "'>" . $category_name . "</a> / ";
    		}
    
    		$html = substr($html, 0, -3);
    
            echo $html;
        }
    }
    Theme Author Ben Sibley

    (@bensibley)

    Hi Steffen,

    Thanks for choosing Tracks!

    That’s a great solution, thanks for sharing it. One recommendation I would make is to move the modified solution into a “child theme”. This way, you won’t lose your edits if you update to a newer version of Tracks.

    You can add it to a functions.php file in a child theme like this:

    function ct_tracks_category_link() {
            $category      = get_the_category();
    
    		foreach($category as $cat) {
    			$category_link = get_category_link( $cat->term_id );
    			$category_name = $cat->cat_name;
    			$html         .= "<a href='" . $category_link . "'>" . $category_name . "</a> / ";
    		}
    
    		$html = substr($html, 0, -3);
    
            echo $html;
        }

    Because of the function_exists check in Tracks’ functions.php file, if a version of the function is found in your child theme first, that version will be used instead.

    Thanks! I will do that!

    I made another possible solution. It only works if you have 2-dimensional categories in use, e.g. “Photography” as a parent and “Landscape” as a child.

    This function will put the “highest/parent”-category at the front and the childs behind it in alphabetical order. Also the parent is seperated from the other categories with a special hyphen.

    function ct_tracks_category_link() {
        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;
        }
    }

    Here again for a better overview:
    http://ideone.com/4dUUdc

    And a image of the final result:
    http://www.directupload.net/file/d/4185/9yogrout_jpg.htm

    Don’t wonder, it’s german and “Fotografie” is the parent and “Konzert” and “Reportage” are the childs.

    Have a nice day!
    Steffen

    Hello,

    Is this also applicable for the TwentySeventeen Theme Sir? I was searching for solution(s) to move “category” above post title & started a topic about it at the support forums as well but no luck at all.

    I wanted to achieve something like this (see image link below):

    https://www.dropbox.com/s/h4cckfpxunwebkv/untitled5.JPG?dl=0

    “Category” above post title & “post date” below post title.

    Please help!

    Regards,

    Xyza

    Theme Author Ben Sibley

    (@bensibley)

    Hello,

    I’m sorry but this forum is only for the Tracks theme. I’m not familiar with the code in TwentySeventeen, but you won’t need to make a new function. Try to find the function in a template file currently outputting the categories and move it lower in the code.

    Hello everyone!

    I was wondering if there was a way to modify Steffen’s code so that the child categories were sorted by their hierarchy and not alphabetically? To fit something like Parent > Child > Grandchild. 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘"Main"-Category of Blog Post’ is closed to new replies.