我有以下代码片段用于调用帖子作者、时间和类别
<?php
printf( __( '<span>%s</span> by %s in %s', 'xxxx' ),
get_the_time( 'M j' ),
et_get_the_author_posts_link(),
get_the_category_list( ', ' )
);
?>
我想用一张图片替换“by”(作者之前)和“in”(类别之前)
我试过这个:
'<span>%s</span> <img src="by.png"> %s <img src="in.png"> %s', 'xxxx'
还有这个:
$img = '<img src="/images/myimage.png" alt="Some Image" />';
printf( __( '<span>%s</span> %s %s in %s', 'xxxx' ),
get_the_time( 'M j' )
$img,
et_get_the_author_posts_link(),
get_the_category_list( ', ' )
);
这一个在作者之前打印出图像的 alt 而不是图像。
如能提供任何帮助,我们将不胜感激。谢谢
最佳答案
printf
按照您指定的顺序填写参数。如果你想要 <img>
出现在<span>
里面那么你必须首先在参数中列出图像:
printf('format string here', $img, get_the_time('M j'), etc...
^^^^^^^^^^^^^^^^^^^^^^^^^--- swap these two
例如
printf('%s %s %s', $arg1, $arg2, $arg3);
^------------^
^---------------^
^-------------------^
如果你弄乱了参数顺序,例如$arg3, $arg2, $arg1
, 然后它们将按顺序打印...
关于php - 无法使用 printf 显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19527793/