• Resolved saintandrews

    (@saintandrews)


    There have been references to pieces of what I want to do in years past, but nothing current I’ve been able to find.

    What I’d like to do is:

    A. Noindex all individual posts assigned to a particular category created for that purpose; then

    B. Hide that category reference from public view (or vice versa).

    If I can’t hide it I can always make the category reference obscure and meaningless such as simply a letter or a numeral, but from what I’ve seen in past years I should be able to hide/exclude a category from public view.

    I’d like to be able to add these code snippets to my child theme header or functions files if possible.

    No, I’m not talking about just noindexing the category taxonomy itself; I already do that. I want instead to use a category as an administrative tool to be able to steer search engines to index/noindex posts depending on whether I have assigned them to my noindex category or not while using my admin category listings to explicitly tell me which ones I’ve noindexed.

    Hopefully this isn’t nearly as complicated as I’ve had to make it sound to explain it.

    Any code snippet help greatly appreaciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It makes sense to add this to your header template since the noindex meta tag belongs in the <head> section. Since you’re not in the loop at this point, you get the post ID with get_queried_object_id(). Then use get_the_category() to get all category objects of the post. Loop through the results looking for a particular category. If that one category is found, then output the meta tag.

    I’m sorry I don’t have a good code example to give you, but if you encode the logic I’ve outlined you should get pretty close.

    Thread Starter saintandrews

    (@saintandrews)

    Thanks, bcworkz. As it turns out, I think I’ve finally been able to cobble together what I was looking for.

    So, to create hidden administrative categories in WordPress to, among other things, automatically noindex any post assigned to a category created for that purpose (e.g., to backstop an SEO plugin):

    A. Noindex posts by category: put the following snippet in the <head> section of one’s theme/child-theme header.php file.

    <?php if (is_single() && in_category(array(###)))  {
    echo '<meta name="robots" content="noindex, follow">';
    } ?>

    where number(s) is Category ID#

    B. Hide any categories from posts on the front page (all I’ve tested): put the following snippet in one’s theme/child-theme functions.php file.

    function the_category_filter($thelist,$separator=' ') {
    	if(!defined('WP_ADMIN')) {
    		//list the category names to exclude
    		$exclude = array('Category 1', 'Category 2');
    		$cats = explode($separator,$thelist);
    		$newlist = array();
    		foreach($cats as $cat) {
    			$catname = trim(strip_tags($cat));
    			if(!in_array($catname,$exclude))
    				$newlist[] = $cat;
    		}
    		return implode($separator,$newlist);
    	} else
    		return $thelist;
    }
    add_filter('the_category','the_category_filter',10,2);

    where Category 1, etc. are the familiar text names of the categories.

    Thread Starter saintandrews

    (@saintandrews)

    Any improvement upon this would still be appreciated, naturally, but until then I’ll mark this as resolved.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Noindex individual posts by category, then hide category’ is closed to new replies.