Wordpress从某些类别中排除日期

问题描述 投票:0回答:2

我想在WordPress中隐藏特定类别的日期和时间。我在下面的代码是每个页面显示的代码,并自动应用于每个类别。这是我可以排除某些类别以停止显示日期信息的方法:

 <?php if(!in_category(60)):?>

此代码段允许我仅排除一个类别,但它不适用于多个类别。

提前致谢!

if (have_posts()):

while(have_posts() ): the_post(); ?>
  <div align="center" class="blogimage">  <?php the_post_thumbnail( 'shop_single' ); ?>
   <h3><?php the_title(); ?></h3>
       <?php the_date(); ?>  
       <p> By </p>
         <?php the_author();?>
         </div>
    <p><?php the_content(); ?></p>
php wordpress
2个回答
0
投票

你可以像这样使用

<?php 
    if(!check_in_category(60)):
?>

在functions.php中添加此功能

check_in_category($categoryid){

    $categoryIdArr = array(60,62,85,90);
    if(in_array($categoryid,$categoryIdArr)){
        return true;
    }
    return false;
}

要么

<?php 
    if((!in_category(60)) || (!in_category(62))):
?>

0
投票

和Vel的回答一样,我的第一个想法是使用“in_array()”。然而; the codex for in_category表示in_category应该适用于多个类别,前提是它们作为一系列ID(或名称或slu)传递。

$my_excluded_cats = array(60,62,85);
if ( ! in_category($my_excluded_cats) ) :
  // your code
endif; // as you're using if():

“这段代码只允许我排除一个类别,但它不适用于多个类别。”

如果这对您不起作用,则可能是由于variable scope,即您声明并分配了数组的位置。在这种情况下(使用您的示例代码)尝试:

<h3><?php the_title(); ?></h3>
<?php 
  if ( ! in_category(array(60,62,85)) ) {
    the_date();
  }
?>  
<p> By </p>
© www.soinside.com 2019 - 2024. All rights reserved.