Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a portfolio template that shows all the portfolio_categories, with the possibility to filter among the categories. See: http://amazingtravel.se/wordpress/temaresor/

My question is how to rewrite this code to show only the categories of my choosing. For example, I want this template to only show the portfolio_categories "visa alla""fiske" and "dykning" in the filter.

    <li><strong><?php _e( 'Välj tema på din resa: ', 'onioneye' ); ?></strong></li>
    <li class="active"><a href="#" class="all" title="<?php _e( 'Visa alla resor', 'onioneye' ); ?>"><?php _e( 'Visa alla', 'onioneye' ); ?></a></li>
    <?php $terms = get_terms( 'portfolio_categories' ); ?>
    <?php $count_terms = count( $terms ); ?>

    <?php if ( $count_terms > 0 ) { ?>

        <?php foreach ( $terms as $term ) { ?>

            <li><a class="<?php echo $term->slug; ?>" href="#" title="<?php printf ( __( 'View all items filed under %s', 'onioneye' ), $term->name ); ?>"><?php echo $term->name; ?></a></li>

        <?php } ?>

    <?php } ?>

</ul>
<!-- END #filter -->
share|improve this question

closed as too localized by toscho Jul 19 '12 at 23:35

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

The function get_terms doesn't allow you to include / exclude specific categories / taxonomies. I think the get_categories function will help you.

<?php
    //first we get the id's of the terms you want to include
    $terms_inc = array("visa alla", "fiske");
    $term_ids = array();

    foreach($terms_inc as $term_inc)
    {
        //now we get the term ids by name and taxonomy
        $term = get_term_by('name', $term_inc, 'portfolio_categories');
        $term_ids[] = $term->term_id;
    }

    $terms_inc_string = implode(", ", $term_ids);
    //include only the ones we want
    $terms = get_categories(array('taxonomy'=>'portfolio_categories', 'include'=>$terms_inc_string));

    //You current code to loop over the terms.
?>

I haven't tested it, but I'm sure it will work. Let me know! Thanks.

share|improve this answer
hmm..I can´t get it to work but I am on the other hand a real novice. Should the code look something like this? amazingtravel.se/wordpress/code – Patrik Oct 22 '11 at 12:55
Try this... pastebin.com/rvZHUFgX – Rutwick Gangurde Oct 22 '11 at 13:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.