gpt4 book ai didi

php - 创建多个 Wordpress 类别 div

转载 作者:可可西里 更新时间:2023-10-31 23:39:34 24 4
gpt4 key购买 nike

我有一个 Wordpress 网站,它使用图像代替类别文本。它被设置为有一个 post div(填充:20% 0;为了一致的响应高度)然后有一个 div 绝对定位,vert/horiz 居中在这个 div 之上。

html

<div class="indx-img" style="background-image:url('...');">

<?php
$testStyle = "";

if(has_category( 'update' ) || has_category( 'uncategorized' )) {
$testStyle = "style=\"background-image:url('".get_bloginfo('template_directory')."/img/logo.png')\"";
} elseif(has_category( 'event' )) {
$testStyle = "style=\"background-image:url('".get_bloginfo('template_directory')."/img/logo.png')\"";
}
?>

<div class="test" <?php echo $testStyle; ?>></div>

</div>

CSS

.indx-img {
position: relative;
padding: 20% 0;
}

.test {
position: absolute;
left: 50%;
margin-left: -5em;
transform: translateY(-50%);
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
height: 10em;
width: 10em;
}

这是测试用的背景图片。这样做的问题是,如果一篇帖子有多个类别,则只会显示一个。

理想情况下,我希望在“测试”中为应用到具有类别图像的帖子的每个类别创建一个 div,并彼此相邻对齐。

最佳答案

根据您的代码,我猜您的 HTML div 代码正在为单个帖子运行。当您使用 has_category 检查单个类别时,它只会为您提供类别。

要获取任何帖子的所有类别,您需要 wp_get_post_categories 函数。

$post_categories = wp_get_post_categories( $post_id );
$cats = array();

foreach($post_categories as $c){
$cat = get_category( $c );
$cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
}

这样就可以得到类目列表,并可以相应地放置应用样式代码。见https://codex.wordpress.org/Function_Reference/wp_get_post_categories wp_get_post_categories 用法的更多详细信息。

关于php - 创建多个 Wordpress 类别 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30518944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com