0

I want to set term name as variable

if(isset($wp_taxonomies)) {
    $term = get_term_by(
        'slug', 
         get_query_var('term'), 
         get_query_var('taxonomy')
    );
    if($term) {
        // do stuff
    }
} 

function assignPageTitle(){     
    return $term>name;
}
add_filter('wp_title', 'assignPageTitle');
get_header();

This code is in the taxonomy.php file and the $term>name; returns the name, but only if I echo, on the title is giving me these errors:

Notice: Use of undefined constant name - assumed 'name' in /home/jshomesc/public_html/wp-content/themes/jshomes/taxonomy.php on line 12

Notice: Undefined variable: term in /home/jshomesc/public_html/wp-content/themes/jshomes/taxonomy.php on line 12

hakre
  • 193,403
  • 52
  • 435
  • 836
Adrian Florescu
  • 4,454
  • 8
  • 50
  • 74
  • 1
    you need to change > to -> but then $term will still not be defined because it is not in scope. – Paul Oct 10 '11 at 03:17
  • Also please see [Reference - What does this error mean in PHP?](http://stackoverflow.com/q/12769982/367456) – hakre Dec 11 '12 at 16:31

1 Answers1

1

There are a couple of problems:

  1. To access the term's name property, the syntax is like so $term->name.

  2. The assignPageTitle function doesn't know about the global $term variable you've just retrieved above it. It's trying to retrieve the name property of a local $term variable that hasn't been defined.

To fix it, add the global keyword (and check if it has been populated):

function assignPageTitle(){  

    // Now the function will know about the $term you've retrieved above  
    global $term;

    // Do we have a $term and name?
    if(isset($term, $term->name)){
        return $term->name;
    }
    return "No term for you...";
}

You can read more about PHP variable scoping to help.

Pat
  • 25,237
  • 6
  • 71
  • 68