Join our new community forum for support & discussion

Join Now

Home Support General HivePress Display categories as title

Display categories as title

  • Author
    Posts
  • emilgadzhiyev

    Hi, @ihor! How it’s going?

    I wrote some code to display the listing’s categories as it’s the title. But it displays only the last category in the category chain. The chain: “All listings->Auto listings->FIAT->145”. It displays only “145”. How I can do to display it exactly: FIAT, 145. Thanks a lot.

    add_action(
    	'hivepress/v1/models/listing/update',
    	function($listing_id) {
    		$listing = HivePress\Models\Listing::query()->get_by_id($listing_id);
    		if ($listing) {
    			$title = null;
    			// replace one, two, three with attribute names
    			if ($listing->get_categories()) {
    				$title .= $listing->display_categories();
    			}
    			if ($listing->get_two()) {
    				$title .= ', ' . $listing->display_two();
    			}
    			if ($listing->get_title() !== $title) {
    				$listing->set_title($title)->save_title();
    			}
    		}
    	}, 1000
    );
    add_filter(
    	'hivepress/v1/models/listing',
    	function($model) {
    		$model['fields']['title']['required']     = false;
    		$model['fields']['title']['max_length']   = 512;
    		$model['fields']['title']['display_type'] = 'hidden';
    		return $model;
    	}, 1000
    );
    ihor developer

    Sorry, but this requires further customizations, in WordPress the listing itself is assigned to the child category only, and each category saves the parent category ID (to keep the hierarchy). If you’re familiar with PHP you can try using this function to get a list of parent category labels https://developer.wordpress.org/reference/functions/get_term_parents_list/

    emilgadzhiyev

    I tried to use:

    add_action(
    	'hivepress/v1/models/listing/update',
    	function($listing_id) {
    		$listing = HivePress\Models\Listing::query()->get_by_id($listing_id);
    		if ($listing) {
    			$title = null;
    			// replace one, two, three with attribute names
    			// Get the current category term id.
    			$query_obj = get_queried_object();
    			$term_id   = $query_obj->term_id;
    			if ($listing->get_categories()) {
    				$title .= get_term_parents_list($term_id, 'category') . ', ' . $listing->display_categories();
    			}
    			if ($listing->get_title() !== $title) {
    				$listing->set_title($title)->save_title();
    			}
    		}
    	}, 1000
    );
    add_filter(
    	'hivepress/v1/models/listing',
    	function($model) {
    		$model['fields']['title']['required']     = false;
    		$model['fields']['title']['max_length']   = 512;
    		$model['fields']['title']['display_type'] = 'hidden';
    		return $model;
    	}, 1000
    );

    But it doesn’t work too. Please, tell me what I do wrong. Thanks.

    ihor developer

    You can try using this one:

    $category_ids = $listing->get_categories__id();
    
    if ( $category_ids ) {
    	$title = get_term_parents_list(
    		reset( $category_ids ),
    		'hp_listing_category',
    		[
    			'separator' => ',',
    			'link'      => false,
    		]
    	);
    }
    emilgadzhiyev

    Thank you very much, it works now!

Viewing 5 posts - 1 through 5 (of 5 total)

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.