php - 从相关帖子中删除具有特定类名的帖子 - WP
问题描述
我的一些帖子有一个类名“已禁用”,我必须从相关帖子中删除它们,因为它们有一个样式 display: none 所以它会插入它但不显示它。
如何过滤禁用类名的帖子并将它们添加到“posts__not_in”以避免它们?
<?php
$related = get_posts( array(
'numberposts' => 4,
'post__not_in' => array($post->ID)
) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>" class="card <?php if( get_field('public') == 'no') echo 'disabled' ?>">
<p><?php the_title();?></p>
</a>
<?php } wp_reset_postdata(); ?>
解决方案
我的建议是创建一个类别并将其分配给帖子。然后使用 tax_query 从您的 get_posts() 中过滤掉这些帖子。下面的示例是伪代码:
$related = get_posts([
'post_type' => 'post',
'numberposts' => 4,
'post__not_in' => [$post->ID],
'tax_query' => [
[
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'DISABLED_CATEGORY_SLUG',
'operator' => 'NOT IN'
],
],
]);
推荐阅读
- sql - Copy csv file to sql table with selected columns
- python - pyQt5 AttributeError: 'bool' 对象没有属性 'txtCustCode'
- html - CSS导航栏重叠和填充问题
- php - 在 php 中调用时,shell 命令不会将错误打印到文件中
- apache-spark - 使用 Argo 工作流程的 K8S Spark
- java - Java - 我如何让 Z 的下一个字母成为 A?
- c++ - 在 docker 容器之间传输时缺少 stdlib 符号
- docker - 无法从 docker 容器内部获取环境
- android - 您的应用包含易受跨应用脚本攻击的 WebView
- javascript - 无法在 searchIcon 道具中的 react-native-elements 的搜索栏上添加 onPress