分类中的Wordpress条件语句,循环内的术语无效 [英] Wordpress conditional statements for taxonomy, term, inside a loop not working
问题描述
这是我遇到的一个基本问题,但是我尝试了很多不同的功能,无法使用。
在我的wordpress网站上我创建了一个名为 文件格式的分类法。
在此分类法中,我创建了这些术语(我认为这些是术语吗?) ... PDF , MOV , PPT 和 DOC
请参见下面的屏幕截图...
我遇到的问题似乎无法解决出来,是我有一个简单的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(); ?>
< ;!-这是我的条件语句要去的地方,请参见下文->
<?php同时; ?>
<?php unset($ downloads);万一; wp_reset_query(); ?>
。 。并且在此循环中,我想有条件地显示图标,具体取决于已为该帖子分配了什么术语。
例如,如果帖子已分配为'PDF一词,那么我希望显示我的PDF图标图像,等等。
请参阅下面的条件语句PHP,它位于上面的循环中。但我不知道为什么总是回响最后一个条件。
<?php
if(term_exists(array(
'term_id'=> 4,
'term_taxonomy'=>'文件格式'
))){
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> '
}
否则if(term_exists(array(
'term_id'=>; 5,
'term_taxonomy'= >>'文件格式'
)) ){
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';
}
?>
任何帮助会令人惊讶,因为当这是一件有问题的基本事情时,这似乎很难。
预先感谢。
我认为term_exists()会让您失望;通常检查给定分类法是否包含 x 项。如果要与给定帖子相关的术语,请使用 get_the_terms(),该方法将返回一个数组。然后,您的代码可以根据该数组进行检查,以确定要插入哪个图像。
This is such a basic problem I'm having but I've tried so many different functions and can't get any to work.
On my wordpress website I have created a taxonomy called 'file-formats'
and in this taxonomy I have created these terms (I think these are terms?)... PDF, MOV, PPT and DOC
See screenshot below...
The problem I have which I can't seem to figure out, is I have a simple WP_Query loop, see below...
<?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(); ?>
...and inside this loop, I want to conditionally display an icon, depending on what term has been assigned to that post.
For example if a post has been assigned 'PDF' term, then I want my PDF icon image to be displayed, etc.
See below my conditional statement PHP, which sits within the loop above. But I can't figure out why the last condition is always being echoed. Help!
<?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' ;
}
?>
and I've also tried this...
<?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' ;
}
?>
and this...
<?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' ;
}
?>
Any help would be amazing because this seems to be so hard when it's a faily basic thing.
Thanks in advance.
I think term_exists() is throwing you off; that checks if a given taxonomy contains x term, generally. If you want the terms associated with a given post, use get_the_terms(), which returns an array. Your code can then check against that array to decide which image to insert.
这篇关于分类中的Wordpress条件语句,循环内的术语无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!