Skip Navigation

[Resolved] populate custom post type to custom meta tag

This support ticket is created 4 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by columD 4 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1933959

Shane helped me earlier. I also need help populating custom post type to the meta tag as well. I have this but it's not working. Can I check what I am doing wrong?

<meta name="description" content="<?php $post_type = get_post_type( $post->ID ); echo'resources='.$post_type.; ?>" />

#1933983

Hello, what exactly do you want to output in the HTML meta tag if the post type slug is "book", for example? Can you show me the desired output in this case?

#1933985

Hi Christian,

Sorry about that. An example would be helpful.

If post type is "book", then it shows:
<meta name="description" content="resources=book" />

#1934181

OK it's difficult to know what the problem could be without seeing the entire template code, but the first thing I would suggest is declaring the global $post variable since you use the $post variable to get the post type:

<meta name="description" content="<?php global $post; $post_type = get_post_type( $post->ID ); echo'resources='.$post_type.; ?>" />
#1934185

Hi Christian,

Sorry about that. I've added this to the "header.php". I've tried your code but it gives me a 502 error.

<?php global $post;
 ?>
<meta name="description" content="<?php $terms = get_the_terms($post->ID, 'topic');foreach($terms as $term){echo 'topics='.$term->name.';';} ?>" />
<meta name="description" content="<?php $terms = get_the_terms($post->ID, 'industry');foreach($terms as $term){echo 'industries='.$term->name.';';} ?>" />
<meta name="description" content="<?php $terms = get_the_terms($post->ID, 'product');foreach($terms as $term){echo 'products='.$term->name.';';} ?>" />
<meta name="description" content="<?php $post_type = get_post_type( $post->ID ); echo'resources='.$post_type.; ?>" />

Everything work except when I added:

<meta name="description" content="<?php $post_type = get_post_type( $post->ID ); echo'resources='.$post_type.; ?>" />
#1934219

Maybe it doesn't like echo without a space after it? Or maybe it doesn't like that trailing "." after $post_type? Not sure offhand. Maybe try it like this:

<meta name="description" content="<?php $post_type = get_post_type( $post->ID ); echo 'resources=' . $post_type; ?>" />
#1934247

Hi Christian,

That works without the 502 error! But it's not as I was expected. Do you know how to make it so it executes the name of the post type instead of the post type slug?

Example:
<meta name="description" content="resources=video-file" />
to:
<meta name="description" content="resources=Video File" />

#1934271

It's a bit more complex to get the name, and it's probably not very readable all inline. I'd do something like this:

<?php 
global $post;
$post_type = get_post_type( $post->ID );
$post_type_obj = get_post_type_object( $post_type );
$post_type_name = $post_type_obj->labels->singular_name;
 ?>
<meta name="description" content="<?php $terms = get_the_terms($post->ID, 'topic');foreach($terms as $term){echo 'topics='.$term->name.';';} ?>" />
<meta name="description" content="<?php $terms = get_the_terms($post->ID, 'industry');foreach($terms as $term){echo 'industries='.$term->name.';';} ?>" />
<meta name="description" content="<?php $terms = get_the_terms($post->ID, 'product');foreach($terms as $term){echo 'products='.$term->name.';';} ?>" />
<meta name="description" content="resources=<?php echo $post_type_name;?>" />

WP's get_post_type_object documentation:
https://developer.wordpress.org/reference/functions/get_post_type_object/

#1935323

My issue is resolved now. Thank you!