简体   繁体   中英

Wordpress exclude date from certain categories

I want to hide the date and time from specific categories at WordPress. I have the code below which is what every page displays and automatically applies to every category. This is how I can exclude certain categories to stop showing date information:

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

This snippet allows me to exclude one category only, but it does not work with multiple ones.

Thanks in advance!

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>

You can use like this

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

add this function in functions.php

check_in_category($categoryid){

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

or

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

Like Vel's answer my first thought was to use "in_array()". However; the codex for in_category indicates that in_category should work for multiple categories providing they are passed as an array of IDs (or names, or slugs).

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

"This snippet allows me to exclude one category only, but it does not work with multiple ones."

If this is not working for you then it may be due to variable scope ie where you declared and assigned the array. In which case (using your example code) try:

<h3><?php the_title(); ?></h3>
<?php 
  if ( ! in_category(array(60,62,85)) ) {
    the_date();
  }
?>  
<p> By </p>

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