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 badge
Add a comment  | 

1 Answer 1

Reset to default 0

Your 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