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.; ?>" />
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?
Hi Christian,
Sorry about that. An example would be helpful.
If post type is "book", then it shows:
<meta name="description" content="resources=book" />
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.; ?>" />
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.; ?>" />
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; ?>" />
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" />
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/
My issue is resolved now. Thank you!