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 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Daniel Llewellyn

    (@diddledan)

    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 1 month, 3 weeks ago by Daniel Llewellyn. Reason: add missing example code

    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 Daniel Llewellyn

    (@diddledan)

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

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

    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 5 days, 19 hours ago by Matthias.
    • This reply was modified 5 days, 19 hours ago by Matthias.
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.