繁体   English   中英

WordPress:如果 the_category != "category2"

[英]Wordpress: If the_category != "category2"

快速 Wordpress 问题。 是否可以检查特定类别而不显示它? 我试过了,但我的类别仍然被回显(没有错误)。

 <?php if (the_category() != "NAMEOFMYCATEGORY") { the_category(' | '); } ?>

或者我需要使用新功能吗?

澄清一下:我想隐藏 1 个特定类别,这样它就不会显示出来。

这应该有效:

<?php
foreach (get_the_category() as $category) {
    if ( $category->name !== 'FORBIDDEN CATEGORY NAME' ) {
        echo '<a href="' . get_category_link($category->term_id) . '">' .$category->name . '</a><br />'; //Markup as you see fit
    }

名称对大写敏感。

为什么不使用 codex 版本?

if (is_home()) {query_posts('cat=-1,-2,-3'); }  // excludes categories 1 2 3

你还记得 in_category() 吗?

if (have_posts() && (!in_category('3')) {

//do domething;

} else // do different loop

如果我正确回答了你的问题,我认为你需要做这样的事情:)

foreach((get_the_category()) as $category) {
   if($category->cat_name = 'mycheckcatname')
   {
   DO THIS
   }
   else
   {
   Do THAT
   }
}

新编辑——

或者这是你正在寻找的其他东西---

<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages, 
I could be left blank</p>
<?php endif; ?>
<?php 
    $categories =  get_categories('');
    $excluded_categories = array('Sem Categoria','Uncategorized');

    foreach  ($categories as $category) {
        if(in_array( $category->cat_name, $excluded_categories)){
            continue;
        }
        echo $category->name;
    }                                     
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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