【问题标题】:Wordpress/PHP function w/nested conditional?带有嵌套条件的 Wordpress/PHP 函数?
【发布时间】:2015-02-20 04:48:25
【问题描述】:

简而言之:我正在创建一个 WP 函数,它将根据以下条件回显图像 url

这里是条件语句的工作原理

  1. 如果存在自定义分类术语,则从自定义分类术语构建 img src url

  2. elseif 自定义分类不存在 - 然后显示默认图像

  3. 存在其他自定义分类,但图像不可用。在外部 url 然后显示默认图片

这是我到目前为止所得到的,我无法完成第 3 步

我确实找到了这个解决方案:

function is_200(){

$urlimage = 'http://images.mysite.com/auto/mercedes-car.jpg';
if(@getimagesize($urlimage) !== false)
{
    echo "HAVE image";
}
else
{
    echo "Nop.";
}
}

但我无法让它与我的功能一起使用,如下所示

function cib_auto_thumbnail($width,$height) {
  $timurl = "http://www.mywebsite.com/wp-content/plugins/timthumb.php?src="; 

  $thumburl ="http://images.mywebsite.com/auto/"; 
  $defaultthumb ="http://images.mywebsite.com/auto/cib.jpg"; 
  $thumburl2 =".jpg"; 

  $timmake =""; $i = 1; foreach((get_the_terms( $post->ID, 'OriginalTag' )) as $timmakecat) {if ($timmakecat->parent == 0) {$timmake .= $timmakecat->name; if($i == 1) break;}} ;
  $timmodel =""; $i = 1; foreach((get_the_terms( $post->ID, 'OriginalTag' )) as $timmodelcat) {if ($timmodelcat->parent |= 0) {$timmodel .= $timmodelcat->name; if($i == 1) break;}} ;

 if (empty($timmake) || empty($timmodel)) {

    echo '<img src="' . $timurl . $defaultthumb .'" / >'; 

    // if no make or model i.e. custom taxonomy is blank; then get default image 
// problem is a custom taxonomy can be created by the author - but the corrosponding image might not exist in the external file folder, hence I need the additional elseif to check for that 

  }   else {

echo '<img src="' . $timurl . $thumburl . $timmake . '-' . $timmodel . $thumburl2 .'" / >';

    // else get image from make and model custom taxonomy and create image url 

 } 

**OriginalTags 是我的自定义分类术语的名称

*** 外部位置的每个图像都被适当命名为:make-model.jpg 即 $timmake-$timmodel.jpg

【问题讨论】:

    标签: php wordpress function conditional


    【解决方案1】:
    <?php
    $terms = apply_filters( 'taxonomy-images-get-terms', '', array('taxonomy' => 'taxonomy_name') );
    $terms = apply_filters( 'taxonomy-images-get-terms', '', array('taxonomy'=>'features', 'image_size' => 'details', 'term_args' => 'hide_empty=0', 'order' => 'ASC' , 'orderby' => 'title', 'count'   => 2) ); 
    foreach($terms as $term){
    
       echo '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'thumb' )  . '</a>';
    ?>
    

    【讨论】:

    • Maulik,请您详细说明一下 - 我有点不知道该放在哪里
    猜你喜欢
    • 1970-01-01
    • 2018-10-17
    • 2023-03-07
    • 2015-05-22
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多