使用猫名称排除类别名称

时间:2011-03-07 作者:Jeremy Love

我试图排除一个类别,因为我有一篇文章,而我有多篇文章。例如,我有一篇文章属于最新新闻和图片类别,但我想排除最新新闻,只显示图片名称。我尝试使用$category=get\\u the\\u category();echo$类别[0]->cat\\U名称;但这没有帮助。

非常感谢您的帮助。

3 个回复
最合适的回答,由SO网友:Jeremy Love 整理而成

事实上我发现这个代码很有效。

<?php
//edit below for categories you want excluded
$exclude = array("Latest News", "Uncategorized");
//how do you want the list separated? just a space is okay
$separator = " | ";
//don\'t edit below here!
$new_the_category = \'\';
foreach((get_the_category()) as $category) {
if (!in_array($category->cat_name, $exclude)) {
$new_the_category .= \'<a href="\'.get_bloginfo(url).\'/\'.get_option(\'category_base\').\'/\'.$category->slug.\'">\'.$category->name.\'</a>\'.$separator;
}
}
echo substr($new_the_category, 0, strrpos($new_the_category, $separator));
?>  

SO网友:Tom

$recentPosts = new WP_Query(); $recentPosts->query(\'cat=-3\'); //-3 makes sure it shows all categories except for ID number 3
        while ($recentPosts->have_posts()) : $recentPosts->the_post(); 

the_title();
the_content();        

endwhile;
你喜欢这个工作吗?

SO网友:Marcin

好的,试试这个:

<?php
$category = get_term_by( \'name\', \'lastest news\', \'category\' );
$args = array (
    \'tax_query\' => array (
        array (
            \'taxonomy\' => \'category\',
            \'field\'    => \'slug\',
            \'terms\'    => $category->slug,
            \'operator\' => \'NOT IN\'
        )
    )
);
query_posts($args);
此示例需要WP 3.1才能工作。查看更多信息:Taxonomy parameters.

结束