简体   繁体   English

如果页面上有特定类别的帖子-Wordpress

[英]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. 我有一个客户端构建的网站,我也添加了一些功能-我通常不使用Wordpress进行开发。 They have built pages using Visual Composer to display posts from varying categories 他们使用Visual Composer构建了页面来显示不同类别的帖子

If the post is within a certain category 'Deals' I want to do stuff … non-working code (in functions.php) below: 如果帖子属于某个类别“交易”,那么我想做些…下列无效代码(在functions.php中):

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: 您应该尝试使用is_category()函数,如下所示:

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. 如果设置了$post全局变量, has_category('Deals')就可以了。 Otherwise you will need to pass post ID as second parameter. 否则,您将需要传递帖子ID作为第二个参数。 https://developer.wordpress.org/reference/functions/has_category/ 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. PS:如果您在循环中调用它,则好像您试图多次回显同一行内联CSS。 This will hide all .entry-thumb s regardless of the category. 无论类别如何,这都将隐藏所有.entry-thumb So it may be better to add a class to your deal posts and then use something like .deal .entry-thumb{ display: none; } 因此,最好在deal添加一个类,然后使用.deal .entry-thumb{ display: none; } .deal .entry-thumb{ display: none; } in your style.css. .deal .entry-thumb{ display: none; }放在您的style.css中。

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

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