• Resolved Reed Sutton

    (@reedus33)


    At the moment pages are as follows:

    {page_name} | {site title}

    For all pages of a specific custom post types, I want the default title to be

    {page_name} Offer | {site title}

    What is the easiest way to do this?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hi Reed!

    If you wish to automate this behavior, you’d want to implement a filter.

    Though you need to replace ‘my_post_type’ with your custom post type name, this should do:

    add_filter( 'the_seo_framework_title_from_generation', function( $title, $args ) {
    
    	$post_type = 'my_post_type';
    
    	if ( null === $args ) {
    		// Front-end
    		$is_my_post_type = is_singular( $post_type );
    	} elseif ( empty( $args['pta'] ) && empty( $args['taxonomy'] ) && $args['id'] ) {
    		$is_my_post_type = get_post_type( $args['id'] ) === $post_type;
    	}
    
    	if ( $is_my_post_type ?? false ) {
    		$title = "$title Offer";
    	}
    
    	return $title;
    }, 10, 2 );

    Cheers! 🙂

    Thread Starter Reed Sutton

    (@reedus33)

    Worked like a charm 🙂 Thanks!

    What about setting a default description? Could I do that in the same filter?

    • This reply was modified 2 years, 4 months ago by Reed Sutton.
    Thread Starter Reed Sutton

    (@reedus33)

    Any thoughts?

    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Reed,

    Sorry for my belated reply.

    The filter works the same for descriptions but under a different hook name. You’d want to change the 'My description here' part in the example below. Please note that no two unique pages should share a description.

    add_filter( 'the_seo_framework_generated_description', function( $description, $args ) {
    
    	$post_type = 'my_post_type';
    
    	if ( null === $args ) {
    		// Front-end
    		$is_my_post_type = is_singular( $post_type );
    	} elseif ( empty( $args['pta'] ) && empty( $args['taxonomy'] ) && $args['id'] ) {
    		$is_my_post_type = get_post_type( $args['id'] ) === $post_type;
    	}
    
    	if ( $is_my_post_type ?? false ) {
    		$description = 'My description here';
    	}
    
    	return $description;
    }, 10, 2 );
    Thread Starter Reed Sutton

    (@reedus33)

    Do you think this would work to have a dynamic description using Pods?

    
    add_filter( 'the_seo_framework_generated_description', function( $description, $args ) {
    
    	$post_type = 'my_post_type';
    
            $id = get_post_id();
            $offer = pods($post_type, $id);
            $offer_name =  get_the_title();
            $offer_rebate = $offer->field( 'offer_rebate' );
    
    	if ( null === $args ) {
    		// Front-end
    		$is_my_post_type = is_singular( $post_type );
    	} elseif ( empty( $args['pta'] ) && empty( $args['taxonomy'] ) && $args['id'] ) {
    		$is_my_post_type = get_post_type( $args['id'] ) === $post_type;
    	}
    
    	if ( $is_my_post_type ?? false ) {
    		$description = 'Get $offer_rebate in FlyerFunds when you sign up and are approved for the $offer_name';
    	}
    
    	return $description;
    }, 10, 2 );
    
    
    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Reed,

    No, that won’t work. The filter runs both at the front- and back end and sometimes for external queries, and the $args variable accounts for that.

    PHP won’t process variables in single-quote strings, you’d need to use double-quote for that. Have you tested the snippet? In any case, I’ll use sprintf() because it allows passing data from other sources.

    Please try this; you still must replace the 'my_post_type' part:

    add_filter( 'the_seo_framework_generated_description', function( $desc, $args ) {
    
    	// If Pods is deactivated, skip further processing.
    	if ( ! function_exists( 'pods_field' ) ) return $desc;
    
    	$post_type = 'my_post_type';
    
    	if ( null === $args ) {
    		// Front-end
    		$is_my_post_type = is_singular( $post_type );
    	} elseif ( empty( $args['pta'] ) && empty( $args['taxonomy'] ) && $args['id'] ) {
    		$is_my_post_type = get_post_type( $args['id'] ) === $post_type;
    	}
    
    	if ( $is_my_post_type ?? false ) {
    		$offer_rebate = pods_field( 'offer_rebate', $args['id'] ?? null ) ?: '';
    
    		if ( $offer_rebate )
    			$desc = sprintf(
    				'Get %s in FlyerFunds when you sign up and are approved for the %s.',
    				$offer_rebate,
    				tsf()->get_raw_generated_title( $args ),
    			);
    	}
    
    	return $desc;
    }, 10, 2 );

    Please note that your requests are bordering the scope of our regular support, so please consider hiring a developer for more extensive customization.

    Thread Starter Reed Sutton

    (@reedus33)

    Great this worked too 🙂 Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Set custom title tag for one type of custom post’ is closed to new replies.