WordPress.org

Forums

how to echo custom category from page category (11 posts)

  1. macaela007
    Member
    Posted 2 years ago #

    Hi I've added category register_taxonomy on on the pages so that users can have different category to tick, but the problems is when i run a
    if ( is_category( 'well_done' )) it doesnt pick that the page is category

    this is my code to register register_taxonomy

    register_taxonomy('page_category', 'page', array(
        'label' => 'Categories',
        'hierarchical' => true,
    ));

    and this is to echo based on the category on the page.php template

    <?php
    if ( is_category( 'view_sample_menu_button' )) {
    	echo "edy";
    } elseif ( in_category( array( 'Tropical Birds', 'small-mammals' ) )) {
    	echo "nope";
    } else {
    	// & c.
    }
    ?>
  2. Adam
    Member
    Posted 2 years ago #

    is_category only works on the Build in taxonomy 'Categories', so when you create your own taxonomy, you need is_tax

  3. macaela007
    Member
    Posted 2 years ago #

    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 ?

  4. Adam
    Member
    Posted 2 years ago #

    is_tax takes two arguments.
    I suspect in your case it should be:
    if (is_tax('page_category', 'hello')) {...}

  5. macaela007
    Member
    Posted 2 years ago #

    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>";
    }
    ?>
  6. Adam
    Member
    Posted 2 years ago #

    And you are trying to view pages where the page category is "hello"?

  7. macaela007
    Member
    Posted 2 years ago #

    yes both where the category is hello and one that the category is not hello.

  8. Adam
    Member
    Posted 2 years ago #

    Oh, my fault, sorry, can you try:
    is_tax('page_category') {...}
    I just rememebred that is_tax can take one argument.

  9. macaela007
    Member
    Posted 2 years ago #

    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,
    ));
  10. Adam
    Member
    Posted 2 years ago #

    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.

  11. macaela007
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic