I inherited a website that includes a custom post type (Cities). Due to some wonky url rewrites and redirects, SEO plugins won’t work on those pages.
This custom post type has a taxonomy (States) and I need to insert the state name into the title tag in my header.php. I’ve tried the following, to no avail:
<title>Log Cabin Rentals in <?php single_term_title(); ?> - Find Your Dream Vacation Rental in <?php single_term_title(); ?></title>
I also tried:
<?php
$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
echo '<title>Cash for Gold Near ' . $term->name . ' - Find Gold Buyers Near ' . $term->name .'</title>';
echo '<meta name="description" content="Looking for cash for gold near ' . $term->name . '? Our cash for gold directory helps you find reputable local gold buyers and pawnshops near ' . $term->name . ', as well as info on how to earn the most cash for gold.">';
?>
What am I doing wrong?
Many thanks!
Cynthia
2
Answers
Give this a shot instead:
If that echoes out your taxonomy name, you could replace the echoes in your title tag with $term->name
Solution One:
get_terms()
– Retrieve the terms in a given taxonomy or list of taxonomies.You can fully inject any customizations to the query before it is sent, as well as control the output with a filter.
The ‘get_terms’ filter will be called when the cache has the term and will pass the found term along with the array of $taxonomies and array of $args. This filter is also called before the array of terms is passed and will pass the array of terms, along with the $taxonomies and $args.
get_terms returns an array of objects. You cannot echo an array, if you do, you will just get
Array()
. What you can do isprint_r($array)
orvar_dump($array)
to see the data it contains.Solution Two:
You can use the function called
get_taxonomies()
in order to query out the taxonomy that you need.Syntax:
Example:
This example uses the ‘object’ output to retrieve and display the taxonomy called ‘genre’: