第一张图片没有显示

时间:2014-02-15 04:15:50

标签: php image

我更改了下面的html代码

<a href="<?php echo pietergoosen_get_first_image() ?>" title="<?php printf( the_title_attribute( 'echo=0' ) ); ?>" rel="lightbox">
          <?php echo pietergoosen_adjust_first_image(pietergoosen_get_first_image(), get_the_title(), 525, 0); ?>
       </a>

到下面的函数,因为我需要一个带有if语句的图像大小

function pietergoosen_the_excerpt_image_output() {
$firstImage = pietergoosen_get_first_image();
$imageAttribute = esc_attr(the_title_attribute( 'echo=0' ));
$title = get_the_title(); 
if (isset($classes) && $classes == 'custom-sidebar-per-page-width'){
    $imageSize = '525';
} else {
    $imageSize = '380';
};

printf(
    '<a href="%1$s" title="%2$s" rel="lightbox"><pietergoosen_adjust_first_image(%1$s, %3$s, %4$d, %4$d)></a>',
        $firstImage,
        $imageAttribute,
        $title,
        $imageSize
);
 }

我像这样调用函数

<?php echo pietergoosen_the_excerpt_image_output(); ?>

但现在图像不再显示了。有关如何修复它的任何建议,请

2 个答案:

答案 0 :(得分:1)

你的printf()电话是一团糟。它有语法错误,完全不必要:

echo "<a href='{$firstImage}' title='$imageAttribute' etc....";

就是必要的。

使用您的版本:

printf('<a href="%1$s" title="%2$s" rel="lightbox"><p
       ^--single quoted string
                 ^^--- not a valid formatting char for printf
                   ^^--- variables are NOT interpolated in '-quoted strings

一个合适的(仍然是不必要的)printf()将是

printf('a href="%s" title="%s" ...., $var1, $var2);
                ^^--insert a string

在PHP中使用printf / sprintf()调用的唯一时间是你需要对插入的字符进行特殊格式化,例如

printf('%05d', 12); // outputs 00012

答案 1 :(得分:0)

您的新功能没有返回值回显,它是从函数内打印。最后一行应该是:

return sprintf(
    '<a href="%1$s" title="%2$s" rel="lightbox"><pietergoosen_adjust_first_image(%1$s, %3$s, %4$d, %4$d)></a>',
        $firstImage,
        $imageAttribute,
        $title,
        $imageSize
);
相关问题