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
There are a couple of problems:
To access the term’s
nameproperty, the syntax is like so$term->name.The
assignPageTitlefunction doesn’t know about the global$termvariable you’ve just retrieved above it. It’s trying to retrieve thenameproperty of a local$termvariable that hasn’t been defined.To fix it, add the
globalkeyword (and check if it has been populated):You can read more about PHP variable scoping to help.