简体   繁体   中英

If page has posts in certain category - wordpress

I have a client built site that I am adding some functionality too - I don't usually develop using Wordpress. They have built pages using Visual Composer to display posts from varying categories

If the post is within a certain category 'Deals' I want to do stuff … non-working code (in functions.php) below:

function deals () {
    if ( in_category('Deals') ) {
        echo '<style>.entry-thumb{display: none !important;}</style>';
    }
}

Calling function from within child theme page template.

Any help would be great thanks

You should try is_category() function like this:

function deals () {
  if ( is_category('Deals') ) {
   echo '<style>.entry-thumb{display: none !important;}</style>';
  }
}

You can check if current post is in category by using

if( has_category('Deals') ) { // do stuff here }

If $post global variable is set has_category('Deals') will be ok. Otherwise you will need to pass post ID as second parameter. https://developer.wordpress.org/reference/functions/has_category/

PS If you are calling it in a loop it looks like you are trying to echo the same inline css multiple times. This will hide all .entry-thumb s regardless of the category. So it may be better to add a class to your deal posts and then use something like .deal .entry-thumb{ display: none; } .deal .entry-thumb{ display: none; } in your style.css.

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