简体   繁体   中英

WP Query Custom Taxonomy but exclude current term

I am setting up a wp_query to list all of the terms in a custom taxonomy. However, I'd like to ammend the code below to exclude the current taxonomy term.

For example, when on the archive.php page for 'term1', I'd like that to be excluded, meaning I am left with 'term2', 'term3' etc.

<? 
$args = array(
    'taxonomy' => 'topic',
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => '-1'
);
?>
<? $tax_menu_items = get_categories( $args );
    foreach ( $tax_menu_items as $tax_menu_item ):?>
        <li>
            <a href="<? echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>">
                <? echo $tax_menu_item->name; ?>
            </a>
        </li>
    <? endforeach; ?>

Try this, it does depend on where you are using it

// Get the category ID
$catID = get_query_var('topic');
// Now run the query excluding the current taxonomy
$args = array(
    'exclude' => $catID,
    'taxonomy' => 'topic',
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => '-1'
);

Then the rest of your code above. For more on get_categories() take a look at https://codex.wordpress.org/Function_Reference/get_categories the exclude parameter is definitely what you need - it is just making sure you exclude the correct category ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM