如果某个类别的帖子对应一个类别,我会尝试为每个帖子指定一个特定的背景色。我的函数可以工作,但仅限于在结果中显示一篇文章。如果我删除此功能,将显示所有帖子。
更多信息:
在我的页面上,我有按类别筛选帖子的按钮。如果单击一次,我将显示该类别的所有帖子。但每个帖子都包含几个类别。我选择了一些类别来为每个帖子设置背景色,但这些类别与过滤器按钮不同。
Wordpress显示此错误;您的站点出现严重错误,请了解有关调试WordPress的详细信息"E;在显示第一个帖子之后。
以下是我的全部代码:
<?php
add_action(\'wp_ajax_nopriv_filter\', \'filter_ajax\');
add_action(\'wp_ajax_filter\',\'filter_ajax\');
function filter_ajax(){
$category = $_POST[\'category\'];
$argsf = array(
\'post_type\' => \'post\',
\'posts_per_page\' => -1,
\'order\' => \'ASC\'
);
if(isset($category)){
$argsf[\'category__in\'] = array($category);
}
$postsf = get_posts($argsf);
if (!empty($postsf)) {
foreach ($postsf as $post) {
$link_post = get_permalink( $post->ID );
$image_post = get_the_post_thumbnail_url( $post->ID, $size = \'large\' );
$item1 = get_post_meta($post->ID, \'item1\', true);
$item2 = get_post_meta($post->ID, \'item2\', true);
$item3 = get_post_meta($post->ID, \'item3\', true);
$item4 = get_post_meta($post->ID, \'item4\', true);
$title = get_the_title($post->ID);
$post_slug = $post->post_name;
$cats_post = wp_get_post_categories( $post->ID );
function test($cats_post){
if (in_array("14", $cats_post)){ echo\'#710000\';}
elseif(in_array("5", $cats_post)){ echo\'#0a005d\';}
elseif(in_array("16", $cats_post)){ echo\'#65a0e8\';}
elseif(in_array("13", $cats_post)){ echo\'#90744b\';}
}
?>
<div class="shop w-24 pb-1" style="height:320px; min-width:320px;">
<div class="w-100 h-100 p-2">
<div class=" " style="background-color:<?php test($cats_post); ?>">
<div class="">
<!--<div class="" title="Locer">
<a href="<?= $item2; ?>"><i class="icofont-opposite "></i></a>
</div>-->
<div class="" title="something">
<a href="tel:<?= $item3; ?>"><i class="im im-phone"></i> </a>
</div>
<div class="">
<span><?= $item1; ?></span>
</div>
</div>
<span class="text-uppercase col-white tsh332 fwlr text-center" style="font-size:2rem;">
<?= $title; ?>
</span>
<?php if ( metadata_exists( \'post\', $post->ID , \'item4\' ) ){ ?>
<div class="">
<div class="b-yellow b-100">
<span><?= $item4; ?> </span>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
<?php } /* fin foreach */
} /* fin if */
wp_reset_postdata();
die();
}
?>
我试过了,但没用:获取\\u类别而不是wp\\u get\\u post\\u类别我不知道是否必须使用其他foreach循环以及如何使用!
谢谢