admin管理员组文章数量:1387306
I hope someone can help me with this. I needed to create a custom metatag to add inside the HTML header based on the post/page categories.
For example:
<meta name="custom-category" content="Cateogry 1/Cateogry 2/Cateogry 3">
I found something close to what I needed but wondering if this would do the trick, Get a list of commas separated categories inside a loop
// get the assigned terms to the post
$terms = get_the_terms( $post_id, 'category' );
// create an empty array for storing category names
$terms_meta = [];
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$terms_meta[] = $term->name;
}
}
if ( ! empty( $terms_meta ) ) {
$terms_string = implode( '/', $terms_meta );
} else {
$terms_string = '';
}
print_r( $terms_string );
Would I then add this to my theme's header.php as:
<meta name="custom-category" content="<?php
// get the assigned terms to the post
$terms = get_the_terms( $post_id, 'category' );
// create an empty array for storing category names
$terms_meta = [];
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$terms_meta[] = $term->name;
}
}
if ( ! empty( $terms_meta ) ) {
$terms_string = implode( '/', $terms_meta );
} else {
$terms_string = '';
}
print_r( $terms_string );
?>">
Any help would be greatly appreciated!
I hope someone can help me with this. I needed to create a custom metatag to add inside the HTML header based on the post/page categories.
For example:
<meta name="custom-category" content="Cateogry 1/Cateogry 2/Cateogry 3">
I found something close to what I needed but wondering if this would do the trick, Get a list of commas separated categories inside a loop
// get the assigned terms to the post
$terms = get_the_terms( $post_id, 'category' );
// create an empty array for storing category names
$terms_meta = [];
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$terms_meta[] = $term->name;
}
}
if ( ! empty( $terms_meta ) ) {
$terms_string = implode( '/', $terms_meta );
} else {
$terms_string = '';
}
print_r( $terms_string );
Would I then add this to my theme's header.php as:
<meta name="custom-category" content="<?php
// get the assigned terms to the post
$terms = get_the_terms( $post_id, 'category' );
// create an empty array for storing category names
$terms_meta = [];
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$terms_meta[] = $term->name;
}
}
if ( ! empty( $terms_meta ) ) {
$terms_string = implode( '/', $terms_meta );
} else {
$terms_string = '';
}
print_r( $terms_string );
?>">
Any help would be greatly appreciated!
Share Improve this question edited Apr 22, 2020 at 16:42 WP-Beginner asked Apr 22, 2020 at 15:53 WP-BeginnerWP-Beginner 11 bronze badge1 Answer
Reset to default 0Your code will work, I would clean it up a bit, so in case we have no categories (say, it's a page, not post), meta tag won't appear at all...
Depending on theme it could be "header.php" or "head.php" (or something else... look for closing tag). Another option is adding it as a hook, but that will be probably to much, keep it simple :)
<?php
// check if we are on a singular post page
if(is_singular( 'post' )) {
// get the assigned terms to the post
$terms = get_the_terms( $post_id, 'category' );
// create an empty array for storing category names
$terms_meta = [];
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
// if you want to exclude categories with ID 123 and 234 add condition
if(!in_array($term->term_id,[123,234])) {
$terms_meta[] = $term->name;
}
}
}
if ( ! empty( $terms_meta ) ) {
// only add tag if there were categories found
?>
<meta name="custom-category" content="<?php echo htmlspecialchars(implode( '/', $terms_meta )); ?>">
<?php
}
}
?>
本文标签: Get a list of categoriesseparated byto display inside the ltheadergt for custom metatags
版权声明:本文标题:Get a list of categories, separated byto display inside the <header> for custom metatags 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744542587a2611701.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论