【问题标题】:PHP/Wordpress - Including an IF function inside an EchoPHP/Wordpress - 在 Echo 中包含一个 IF 函数
【发布时间】:2012-10-25 16:06:19
【问题描述】:

我有以下 WordPress 查询:

$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
    echo '<div class="' . $tax_term->slug . '" style="display:none; background:url(' . $whatdoiputhere . ');">';
    while ($my_query->have_posts()) : $my_query->the_post();

在后台 URL 中,我需要包含由以下代码生成的图像 URL:

<?php if (function_exists('z_taxonomy_image_url')) echo z_taxonomy_image_url(); ?>

如何在后台 URL 中包含这个 IF 函数?

我试过了,但你可以猜到它不起作用:

background:url(' . if (function_exists('z_taxonomy_image_url')) echo z_taxonomy_image_url(); . ');

对于我将如何做到这一点的一些帮助,我将不胜感激。

【问题讨论】:

  • 明确地说,您是在尝试将 PHP 添加到外部 CSS 文件中,还是该 CSS 内嵌在 HTML 中?
  • 它在 HTML 中的内联,因为它为每个返回的项目动态变化。

标签: php wordpress if-statement echo


【解决方案1】:
<?php
    if (function_exists('z_taxonomy_image_url')) $background_url = 'background:url(' . z_taxonomy_image_url() . ')';
    echo '<div class="' . $tax_term->slug . '" style="display:none; ' . $background_url . '">';
?>

【讨论】:

    【解决方案2】:

    三元运算符:

    background:url(' . (function_exists('z_taxonomy_image_url') ? z_taxonomy_image_url() : '') . ');
    

    【讨论】:

      【解决方案3】:

      我会这样做:

      <?php 
          if (function_exists('z_taxonomy_image_url')) {
              echo "background:url('" . z_taxonomy_image_url() . "');";
          }
      ?> 
      

      如果您愿意,您可以通过 else 子句设置默认背景图片,否则您的 CSS 中将不存在背景定义。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-30
        • 2012-02-13
        • 2012-02-15
        • 1970-01-01
        相关资源
        最近更新 更多