WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Duplicate Parent Category in Post Meta (5 posts)

  1. sajobe2
    Member
    Posted 1 year ago #

    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>
  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    http://codex.wordpress.org/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?

  3. sajobe2
    Member
    Posted 1 year ago #

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

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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 ;-)

  5. sajobe2
    Member
    Posted 1 year ago #

    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>

Topic Closed

This topic has been closed to new replies.

About this Topic