0

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!

1 Answer 1

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
    }
}
?>
3
  • Thank you so much for your help! Your code works perfectly for the blog categories. I do have another question, do you know a certain way to exclude certain categories since I use some categories for internal use. Is there an easy way to go about it? Commented Apr 22, 2020 at 18:15
  • I updated code to exclude category by IDs. Downside - you need to know exact ID. Pros: it's the easiest way to do that :D
    – Worduoso
    Commented Apr 22, 2020 at 18:37
  • Wow, you are amazing! That did the trick. Thank you so much again! Commented Apr 22, 2020 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.