is_category only works on the Build in taxonomy ‘Categories’, so when you create your own taxonomy, you need is_tax
Hi I tried if is tax still didnt work this is how I did it.
<?php
if (is_tax('hello')) {
echo "<p>Western films are great</p>";
} else {
echo "<p>comedy</p>";
}
?>
this is how i’ve created the tax by adding this line inside the function do I need anything else I’ve ticked the category on the CMS but still nothing always echo the else on the page
register_taxonomy('page_category', 'page', array(
'label' => 'Categories',
'hierarchical' => true,
));
Do I need to do differently ?
is_tax takes two arguments.
I suspect in your case it should be:
if (is_tax('page_category', 'hello')) {...}
Hi just tried still didnt work the pages still only given me the else event thou I tick the page category, does category doesnt work on page only on post or something?
<?php
if (is_tax('page_category', 'hello')) {
echo "<p>Western films are great</p>";
} else {
echo "<p>comedy</p>";
}
?>
And you are trying to view pages where the page category is “hello”?
yes both where the category is hello and one that the category is not hello.
Oh, my fault, sorry, can you try:
is_tax('page_category') {...}
I just rememebred that is_tax can take one argument.
do you mean like this if so still didnt work
<?php
if (is_tax('page_category')) {
echo "<p>Western films are great</p>";
} else {
echo "<p>comedy</p>";
}
?>
register_taxonomy('page_category', 'page', array(
'label' => 'Categories',
'hierarchical' => true,
));
I am sorry, then I don’t know. since 2.9 (I think) taxonomies are possible on pages as well as posts. However pages work differently from posts, so perhaps the problem is the context in which you try to use them?
Hopefully someone else with more experience in taxonomies can shed more light on this than me for you.
Hi I found a function that made it work are you familiar with it.
/**
* Conditional function to check if post or page belongs to term in a custom taxonomy.
*
* @param tax string taxonomy to which the term belons
* @param term int|string|array attributes of shortcode
* @param _post int post id to be checked
* @return BOOL True if term is matched, false otherwise
*/
function pa_in_taxonomy($tax, $term, $_post = NULL) {
// if neither tax nor term are specified, return false
if ( !$tax || !$term ) { return FALSE; }
// if post parameter is given, get it, otherwise use $GLOBALS to get post
if ( $_post ) {
$_post = get_post( $_post );
} else {
$_post =& $GLOBALS['post'];
}
// if no post return false
if ( !$_post ) { return FALSE; }
// check whether post matches term belongin to tax
$return = is_object_in_term( $_post->ID, $tax, $term );
// if error returned, then return false
if ( is_wp_error( $return ) ) { return FALSE; }
return $return;
}
and then call it
<?php
if (pa_in_taxonomy('page_category', 'hello')) {
echo "Western films are great";
} else {
echo "edy";
}
?>
Thanks for your help