• Resolved sajobe2

    (@sajobe2)


    Okay so this theme didn’t originally have categories in the post meta and I finally added them, but then parents weren’t showing and I finally added them. Then however when I have multiple children I get the parent more than once: example. And I am officially over my head… is there any way to make it exempt duplicates? Or something? The code used is below:

    <span class="cat"><?php
    $categories = the_category('  &  ', 'multiple');
    $seperator = ' & ';
    $output = '';
    $parents = 'multiple';
    if($categories){
    	foreach($categories as $category) {
    		$output .= '<a href="'.get_category_link($category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$seperator;
    	}
    echo trim($output, $seperator);
    }
    ?></span>

Viewing 4 replies - 1 through 4 (of 4 total)
  • https://codex.www.ads-software.com/Function_Reference/the_category

    using the ‘multiple’ parameter will add the respective ancestor hierarchy to the output of each category; there is no way to exclude what you call ‘duplicate parent categories’.

    of your code, only this part actually does anything: the_category(' & ', 'multiple');

    because the_category() would directly echo the output and not return it to be used in a variable.

    what exactly are you trying to show, for instance in your example link?

    Thread Starter sajobe2

    (@sajobe2)

    Something about what you said clicked I found an easy fix. Thanks!

    could you post your solution?
    it might help somebody else in the future.

    if this is all fixed, please mark the topic as ‘resolved’ – thnaks ??

    Thread Starter sajobe2

    (@sajobe2)

    Reasons you should walk away when stuck. Really the best way for what I wanted was to check Book Review as a category and stop treating it as a parent seeing as I wanted list and not hierarchy. The code I used is below.

    <span class="cat"><?php
    $categories = get_the_category();
    $seperator = ' & ';
    $output = '';
    if($categories){
    	foreach($categories as $category) {
    		$output .= '<a href="'.get_category_link($category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$seperator;
    	}
    echo trim($output, $seperator);
    }
    ?></span>

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Duplicate Parent Category in Post Meta’ is closed to new replies.