Support » Plugin: A-Z Listing » parent category in brackets?

  • Hi Daniel,
    I have many parent and child categories. Some child categories have the same name but are in different parent categories.
    At the moment your plugin is listing 5 or more categories with the same name.

    Do you know a way to show a difference between these. Perhaps the parent category in brackets or something else…?
    Thanks
    Matthias

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Dani Llewellyn

    (@diddledani)

    You can customise the listing by copying the file from wp-content/plugins/a-z-listing/templates/a-z-listing.php into your theme’s top-level folder alongside style.css. You can then edit to suit your requirements. For this example you can try changing lines 57 to 59. Currently thes lines read as below:

    <a href="<?php $a_z_query->the_permalink(); ?>">
    	<?php $a_z_query->the_title(); ?>
    </a>

    You can include the parent terms with get_term_parents_list():

    <a href="<?php $a_z_query->the_permalink(); ?>">
    	<?php $a_z_query->the_title(); ?>
    </a>
    (<?php echo get_term_parents_list( $a_z_query->get_the_item_id(), 'category', array( 'separator' => ', ' ) ); ?>)

    This will add the list of parent-terms as a comma-separated list of their names inside parentheses after the title of the current term. You might not want all the parents, so you could try to use get_ancestors() directly to pull only the immediate parent:

    <a href="<?php $a_z_query->the_permalink(); ?>">
    	<?php $a_z_query->the_title(); ?>
    </a>
    (<?php
    $all_parents = get_ancestors( $a_z_query->get_the_item_id(), 'category', 'taxonomy' );
    $immediate_parent = array_shift( $all_parents );
    $immediate_parent_term = get_term( $immediate_parent, 'category' );
    echo $immediate_parent_term->name;
    ?>)
    • This reply was modified 2 years, 7 months ago by Dani Llewellyn. Reason: add missing example code
    Thread Starter Matthias

    (@matthias70)

    Hi Daniel,
    what a great addition to your plugin.

    Thanks for that support!!!

    Unfortunately the brackets are shown in the tag-list. The tags have no parents so the brackets are empty.
    Is there a easy solution?
    Thanks
    Matthias

    Plugin Author Dani Llewellyn

    (@diddledani)

    Hi,

    To omit the brackets when there are no parents, you can store the parents list or immediate parent in a variable and then test whether that variable has any content:

    For the full list of parents:

    <a href="<?php $a_z_query->the_permalink(); ?>">
    	<?php $a_z_query->the_title(); ?>
    </a>
    <?php
    $parents_list = get_term_parents_list( $a_z_query->get_the_item_id(), 'category', array( 'separator' => ', ' ) );
    if ( ! empty( $parents_list ) ) :
    	echo '(' . $parents_list . ')';
    endif;
    ?>

    For the immediate parent only:

    <a href="<?php $a_z_query->the_permalink(); ?>">
    	<?php $a_z_query->the_title(); ?>
    </a>
    <?php
    $all_parents = get_ancestors( $a_z_query->get_the_item_id(), 'category', 'taxonomy' );
    $immediate_parent = array_shift( $all_parents );
    if ( $immediate_parent ) :
    	$immediate_parent_term = get_term( $immediate_parent, 'category' );
    	echo '(' . $immediate_parent_term->name . ')';
    endif;
    ?>
    Thread Starter Matthias

    (@matthias70)

    Hi Daniel,
    you made my day! The code works perfect 🙂
    Thank you very much!!!
    Matthias

    Thread Starter Matthias

    (@matthias70)

    Hi Daniel,
    there is one issue if I use the code for immidate parent.
    I have categories with no files, but subcategories with files.
    https://gtauscht.de/kategoriesuche/
    So it shows category_A (0) files. Can I show the sum of all files in their subcategories? Or will we get a conflict, with the file number of categories which include files?

    Thanks
    Matthias

    • This reply was modified 2 years, 5 months ago by Matthias.
    • This reply was modified 2 years, 5 months ago by Matthias.
    Plugin Author Dani Llewellyn

    (@diddledani)

    Hi,

    I suspect you could alter the template with code that references get_term_children(). Something like this might work:

    <?php
    while ( $a_z_query->have_items() ) : $a_z_query->the_item();
        ?>
        <a href="<?php $a_z_query->the_permalink(); ?>">
            <?php $a_z_query->the_title(); ?>
            <?php
            $all_parents = get_ancestors( $a_z_query->get_the_item_id(), 'category', 'taxonomy' );
            $immediate_parent = array_shift( $all_parents );
            if ( $immediate_parent ) :
                $immediate_parent_term = get_term( $immediate_parent, 'category' );
                $children = get_term_children( $immediate_parent, 'category' );
                $count = 0;
                foreach ( $children as $child ) {
                    // add the child's count to the value used in the title
                    $count += $child->count;
                }
                echo "({$count}) - {$immediate_parent_term->name}";
            endif;
            ?>
        </a>
        <?php
    endwhile;
    ?>
    Thread Starter Matthias

    (@matthias70)

    Hi Daniel,
    can not get the last code to work.
    The code below just shows another 0 behind the post count
    see: https://imgur.com/iuuW6Kv

    <ul class="columns <?php echo $column_class; ?>">
    <?php
    while ( $a_z_query->have_items() ) : $a_z_query->the_item();
    ?>
    <li>
    <a class="a-z-link" href="<?php $a_z_query->the_permalink(); ?>">
    <strong><?php $a_z_query->the_title(); ?></strong> (<?php $a_z_query->the_item_post_count(); ?>)
    <?php
    $all_parents = get_ancestors( $a_z_query->get_the_item_id(), 'download_category', 'taxonomy' );
    $immediate_parent = array_shift( $all_parents );
    if ( $immediate_parent ) :
    $immediate_parent_term = get_term( $immediate_parent, 'download_category' );
    $children = get_term_children( $immediate_parent, 'category' );
    $count = 0;
    foreach ( $children as $child ) {
       // add the child's count to the value used in the title
    $count += $child->count;
                }
    echo "({$count}) - {$immediate_parent_term->name}";
    endif;
    ?></a></li>
    <?php endwhile; ?>
    </ul>

    Thanks
    Matthias

    • This reply was modified 2 years, 5 months ago by Matthias.
    • This reply was modified 2 years, 5 months ago by Matthias.
    Thread Starter Matthias

    (@matthias70)

    Hi Daniel,
    do you have any suggestions, to the post above?
    Thanks
    Matthias

    Plugin Author Dani Llewellyn

    (@diddledani)

    Hi,

    The foreach loop is wrong. Sorry about that:

    foreach ( $children as $child ) {
        $term = get_term_by( 'id', $child, 'category' );
        // add the child's count to the value used in the title
        $count += $term->count;
    }

    You have two numbers in parentheses because you also have the following:

    <strong><?php $a_z_query->the_title(); ?></strong> (<?php $a_z_query->the_item_post_count(); ?>)
    

    I suspect you’ll want to remove the second-half of that line – i.e. the parentheses and the content within.

    Thread Starter Matthias

    (@matthias70)

    Hi Daniel,
    thanks for your code.
    That’s what I have now

    								<li>
    <a class="a-z-link" href="<?php $a_z_query->the_permalink(); ?>">
    <strong><?php $a_z_query->the_title(); ?></strong>
    
    <?php
    $all_parents = get_ancestors( $a_z_query->get_the_item_id(), 'download_category', 'taxonomy' );
    $immediate_parent = array_shift( $all_parents );
    if ( $immediate_parent ) :
    	$immediate_parent_term = get_term( $immediate_parent, 'download_category' );
    	            $children = get_term_children( $immediate_parent, 'category' );
    foreach ( $children as $child ) {
        $term = get_term_by( 'id', $child, 'category' );
        // add the child's count to the value used in the title
        $count += $term->count;
    }
                echo "({$count}) - {$immediate_parent_term->name}";
            endif;
    ?></a></li>

    Unfortunately, the brackets are empty know 🙁
    https://gtauscht.de/kategoriesuche/
    Do you another idea?
    Thanks
    Matthias

    • This reply was modified 2 years, 4 months ago by Matthias.
    Plugin Author Dani Llewellyn

    (@diddledani)

    The only thing I can immediately see is that you are not setting a default value to $count before the foreach loop:

    $children = get_term_children( $immediate_parent, 'category' );
    $count = 0;
    foreach ( $children as $child ) {
        $term = get_term_by( 'id', $child, 'category' );
        // add the child's count to the value used in the title
        $count += $term->count;
    }
    Thread Starter Matthias

    (@matthias70)

    Hi Daniel,
    this does not work!
    I have only a 0 in the brackets ;-(

    I think there are to many changes in the code now.

    The idea was to show child categories with their parent.
    The bracket with the number of results should show the entries of all subcats.

    Thanks
    Matthias

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘parent category in brackets?’ is closed to new replies.