【发布时间】:2012-03-27 20:58:01
【问题描述】:
这是我遇到的一个基本问题,但我尝试了很多不同的功能,但都无法正常工作。
在我的 wordpress 网站上,我创建了一个名为“file-formats”的分类法
在这个分类中,我创建了这些术语(我认为这些是术语?)... PDF、MOV、PPT 和文档
请看下面的截图...
我似乎无法弄清楚的问题是我有一个简单的 WP_Query 循环,见下文...
<?php
$downloads = new WP_Query(array(
'post_type' => 'downloads',
'order' => 'ASC',
'posts_per_page' => -1
)); ?>
<?php if ($downloads->have_posts()) : ?>
<?php while ($downloads->have_posts()) : $downloads->the_post(); ?>
<!-- This is where my conditional statement will go, see below -->
<?php endwhile; ?>
<?php unset($downloads); endif; wp_reset_query(); ?>
...在这个循环中,我想有条件地显示一个图标,具体取决于为该帖子分配的术语。
例如,如果帖子被分配了“PDF”术语,那么我希望显示我的 PDF 图标图像,等等。
请看下面我的条件语句 PHP,它位于上面的循环中。但我不明白为什么最后一个条件总是被回显。救命!
<?php
if (term_exists(array(
'term_id' => 4,
'term_taxonomy' => 'file-formats'
))) {
echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/pdf.png" alt="" class="file-format-icon">' ;
}
else if (term_exists(array(
'term_id' => 6,
'term_taxonomy' => 'file-formats'
))) {
echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/ppt.png" alt="" class="file-format-icon">'
}
else if (term_exists(array(
'term_id' => 5,
'term_taxonomy' => 'file-formats'
))) {
echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/mov.png" alt="" class="file-format-icon">' ;
}
else {
echo 'nothing' ;
}
?>
我也试过这个......
<?php
if (is_tax('file-formats','pdf')) {
echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/pdf.png" alt="" class="file-format-icon">' ;
}
else if (is_tax('file-formats','ppt')) {
echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/ppt.png" alt="" class="file-format-icon">' ;
}
else if (is_tax('file-formats','mov')) {
echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/mov.png" alt="" class="file-format-icon">' ;
}
else {
echo 'nothing' ;
}
?>
还有这个……
<?php
if (is_object_in_taxonomy( 'pdf', 'file-formats' )) {
echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/pdf.png" alt="" class="file-format-icon">' ;
}
else if (is_object_in_taxonomy( 'ppt', 'file-formats' )) {
echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/ppt.png" alt="" class="file-format-icon">' ;
}
else if (is_object_in_taxonomy( 'mov', 'file-formats' )) {
echo '<img src="' . content_url( '/themes/mytheme/' ) . 'images/icons/mov.png" alt="" class="file-format-icon">' ;
}
else {
echo 'nothing' ;
}
?>
任何帮助都会令人惊叹,因为当它是一个失败的基本事物时,这似乎太难了。
提前致谢。
【问题讨论】:
标签: wordpress conditional-statements