skip to Main Content

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


  1. Give this a shot instead:

    $term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
    echo $term->name;
    

    If that echoes out your taxonomy name, you could replace the echoes in your title tag with $term->name

    Login or Signup to reply.
  2. 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 is print_r($array) or var_dump($array) to see the data it contains.

    $taxonomy = 'shirt';
    $args=array(
      'hide_empty' => false,
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $tax_terms = get_terms( $taxonomy, $args );
    foreach ( $tax_terms as $tax_term ) {
        echo $tax_term->name;
    }
    

    Solution Two:

    You can use the function called get_taxonomies() in order to query out the taxonomy that you need.

    Syntax:

    <?php get_taxonomies( $args, $output, $operator ) ?>
    

    Example:

    This example uses the ‘object’ output to retrieve and display the taxonomy called ‘genre’:

    <?php 
    $args=array(
      'name' => 'genre'
    );
    $output = 'objects'; // or names
    $taxonomies=get_taxonomies($args,$output); 
    if  ($taxonomies) {
      foreach ($taxonomies  as $taxonomy ) {
        echo '<p>' . $taxonomy->name . '</p>';
      }
    }  
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search