[Support request] How to add a prefix to the Category Title for certain parent category ?

Home Forums Support [Support request] How to add a prefix to the Category Title for certain parent category ?

Home Forums Support How to add a prefix to the Category Title for certain parent category ?

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #1636012
    Budi

    Hello,

    I want to add prefix for subcategory which has parent category name=xxx or id=xxx
    I tried below code, but not worked.

    add_filter( ‘get_the_archive_title’, function( $title ) {

    $category = get_the_category();
    $category_parent_id = $category[0]->category_parent;
    if ( $category_parent_id = 10 ) {
    $title = ‘Prefix ‘. $title;
    }

    return $title;
    }, 50 );

    Thanks.

    #1636313
    Elvin
    Staff
    Customer Support

    Hi there,

    You can try this instead:

    add_filter( 'get_the_archive_title', function( $title ) {
    
        $archive_cat = get_queried_object();
    
    	if ($archive_cat->category_parent == 83) {
    		$title = 'Prefix '. $title;
    	}
    
    return $title;
    }, 50 );
    #1636385
    Budi

    Thanks, it works perfectly.

    Can you explain about the value 50 ?
    In some answers, I saw the value is 20.

    #1637130
    Budi

    Hello,

    I also want to add a prefix to the Tag Title on tag archive.
    What code should I insert for below code ?

    add_filter( ‘get_the_archive_title’, function( $title ) {

    $archive_cat = get_queried_object();

    if ($archive_cat->category_parent == 83) {
    $title = ‘Prefix ‘. $title;
    }

    return $title;
    }, 50 );

    Thanks.

    #1637595
    Elvin
    Staff
    Customer Support

    Try this:

    add_filter( 'get_the_archive_title', function( $title ) {
    
        $archive_cat = get_queried_object();
    	$tags = get_the_tags(get_queried_object()->ID);
    
    	if ($archive_cat->category_parent == 83) {
    		$title = 'Prefix '. $title;
    	}
    
    	if ($tags) {
    		foreach($tags as $tag) {
    			if ($tag->slug == 'test') {
    				$title = 'Prefix '. $title;
    			}
    		}
    	}
    
    return $title;
    }, 50 );
    #1638700
    Budi

    The prefix I want to add for the tag title is in tags archives, not tags in category archives.
    Url example: http://www.generatepress.com/tag/flower

    #1639754
    Tom
    Lead Developer
    Lead Developer

    The code should be almost identical to the original code that worked. The only difference I can see is you’d use parent instead of category_parent: https://dev.to/ko31/return-value-of-getqueriedobject-in-wordpress-1h7n

    These kinds of general WordPress coding questions are far better suited for a site like this: https://wordpress.stackexchange.com/

    Thanks!

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.