Forum Moderators: rogerd & travelin cat

Message Too Old, No Replies

How to add meta tags to header of specific WP page without a plugin

meta tag WP page not post

         

JamesSC

9:22 pm on Aug 11, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



How do I alternately

1. add a meta robots tag to the header of a specific WordPress Page - not a Post

2. assign a Page to a specific Category (already scripted to accomplish that)

without using a plugin.

Yes, I'm already familiar with Joost de Valk's and others' plugins. I'm trying to move as many functions as I can to my own control in header.php, functions.php, etc.

I can already add meta tags to certain classes of WordPress pages - e.g., author, date, search, category, tag - but I don't want to meta tag all Pages I may create within WP as a class, just select ones.

If I could add a Page to a Category as I can a Post, I could already do so, but that seems impossible within WordPress. Searching here has revealed nothing and searching online to approach the problem from either angle has only returned bowls of word association spaghetti.

I concede this may be impossible, but if not, thanks for any help.

not2easy

10:36 pm on Aug 11, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The way I do it is to create a page for a category I want to have and then create the pages for that category and set them as 'children' of the 'category' page. It seems to work just fine. As for creating specifically purposed templates, there are usually some optional templates in themes afaik. Mine has optional home page, landing page for example. If you are php fluent, you can adapt any page template.

WordPress offers developer information and invites people to expand their platform: [wordpress.org...] lists some of the basic changes that can be made and those inks go as deep as you need or want, depending on your skills.

JamesSC

1:17 pm on Aug 12, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



Thank you not2easy.

JamesSC

7:33 pm on Aug 12, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



This seems to work in WP 5.5, in header.php of theme or child theme:

<?php if ($post->ID == ##) { echo '<meta name="robots" content="noindex,nofollow">'; } ?>

where ## is the post or page ID (hover over post/page name to reveal).

For multiple posts/pages to be given the same meta tag (which, I suppose could be anything legit):

<?php if ($post->ID == ## || $post->ID == ## || $post->ID == ##) { echo '<meta name="robots" content="noindex,nofollow">'; } ?>

martinibuster

3:03 am on Aug 14, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



This seems to work in WP 5.5, in header.php of theme or child theme:


You probably should not do it with the header.php of the theme itself because it'll get erased in the next update.

As for the child header.php, if it works and doesn't break anything then good. That code looks like something that should work.

Here's a tutorial on how to do it with a child function.php:

[kinsta.com...]