If statement within PHP echo

时间:2014-11-17 作者:Adrian

我有以下内容来显示我的类别列表:

      $categories = get_categories( $args );
  foreach ( $categories as $category ) {
     echo \'<li><img src=""/><a href="\' . get_category_link( $category->term_id ) . \'" rel="bookmark">\' . $category->name . \'\' . \'\' . $category->description . \'</a></li>\';
  }
但我正在使用“categories images”插件为每个类别分配一个图像(这是

img src=”/>

在代码内为。在src=“”中,我需要获得以下IF语句:

<?php if (function_exists(\'z_taxonomy_image_url\')) echo z_taxonomy_image_url(); ?>
甚至只是:

z_taxonomy_image_url();
我怎样才能做到这一点?

3 个回复
最合适的回答,由SO网友:BODA82 整理而成

根据插件文档(http://zahlan.net/blog/2012/06/categories-images/), 这似乎很简单:

<ul>
<?php 
$categories = get_categories( $args );
foreach ( $categories as $category ) {
    $img_src = z_taxonomy_image_url($category->term_id);
    if ( $img_src ) {
        echo \'<li><img src="\' . $img_src . \'" alt="" /><a href="\' . get_category_link($category->term_id) . \'" rel="bookmark">\' . $category->name . \' - \' . $category->description . \'</a></li>\';
    } else {
        echo \'<li><a href="\' . get_category_link($category->term_id) . \'" rel="bookmark">\' . $category->name . \' - \' . $category->description . \'</a></li>\';
    }
}
?>
</ul>

SO网友:Michelle

你可以试试这个。

$categories = get_categories( $args );
foreach ( $categories as $category ) {
     if (function_exists(\'z_taxonomy_image_url\')) { 
          $imgurl = z_taxonomy_image_url(); 
     }
     if ( $imgurl ) {
          echo \'<li><img src="\'. $imgurl . \'"/><a href="\' . get_category_link( $category->term_id ) . \'" rel="bookmark">\' . $category->name . \'\' . \'\' . $category->description . \'</a></li>\'; 
     } else {
          echo \'<li><a href="\' . get_category_link( $category->term_id ) . \'" rel="bookmark">\' . $category->name . \'\' . \'\' . $category->description . \'</a></li>\';
     }
}

SO网友:SkyShab

以下是我的看法:

$categories = get_categories( $args );
foreach ( $categories as $category ) 
{
    $image = (function_exists(\'z_taxonomy_image_url\') && z_taxonomy_image_url($category->term_id) ) ? sprintf(\'<img src="%s">\', z_taxonomy_image_url($category->term_id) ) : \'\';
    echo sprintf(\'<li>%s<a href="%s" rel="bookmark">%s %s</a></li>\', $image, get_category_link( $category->term_id ), $category->name, $category->description);
}

结束

相关推荐

Show Pages in Categories

通过将此代码添加到函数中,我创建了category函数。php:function page_category() { register_taxonomy_for_object_type(\'category\', \'page\'); } // Add to the admin_init hook of your theme functions.php file add_action( \'init\', \'page_category\' ); 但问