Join our new community forum for support & discussion
Join NowHome › Support › Themes › ListingHive › Getting Parent Category Name for a Sub Category
Getting Parent Category Name for a Sub Category
-
AuthorPosts
-
devpatelx 8 months, 2 weeks ago
Hello,
<?php echo ( new \HivePress\Blocks\Part( [ 'path' => 'listing-category/view/listing-category-item-count', 'context' => [ 'listing_category' => $listing_category ], ] ) )->render(); ?> <?php echo esc_html( $listing_category->get_name() ); ?>
————————
This code gives the name of the category/subcategory, but suppose I want to get the name of the parent category, then what should be done???
——————————
Also, I am new to PHP, I don’t get what we are actually doing here, can you pls explain or give me some link where I can understand the above coding. This path and context above, I don’t get it, what is this.Hi,
If you mean outputting child category titles you can try getting an array of categories this way:$listing_category->get_children()
Sorry, there’s no easy way to guide on PHP and customizations if you’re not familiar with coding, if customizations beyond the available features are required I recommend hiring a developer with WP experience https://www.fiverr.com/categories/programming-tech/wordpress-services
devpatelx 8 months, 1 week agoThanks Ihor.
This thing – $listing_category->get_name(), outputs category/sub-category name. But how can I output name of parent category for a subcategory, Because if B is subcategory of A, then “$listing_category->get_name()”, outputs B only, I want to output A also with that. [A is parent category]
Please try using the parent category this way:
$parent_category=$listing_category->get_parent();
Then you can get its name in the same way:
if($parent_category) { echo $parent_category->get_name(); }
devpatelx 7 months, 3 weeks agoI want to change this code to generate a parent category if exists [plugin template file] —-
<?php // Exit if accessed directly. defined( 'ABSPATH' ) || exit; if ( $listing->get_categories__id() ) : ?> <div class="hp-listing__categories hp-listing__category"> <?php foreach ( $listing->get_categories() as $category ) : ?> <a>router->get_url( 'listing_category_view_page', [ 'listing_category_id' => $category->get_id() ] ) ); ?>"><?php $current_cat = esc_html( $category->get_name() ); echo $current_cat; ?></a> <?php endforeach; ?> </div> <?php endif;
Please try using this function to get all the parents for a category https://developer.wordpress.org/reference/functions/get_ancestors/ You can get the category IDs this way:
$listing->get_category__id()
The category taxonomy is
hp_listing_category
.Hope this helps.
-
AuthorPosts
New Reply
This forum has been archived and is no longer accepting new posts or replies. Please join our new community forum for support & discussion.