简体   繁体   English

如何让我的 PHP 代码在 Wordpress 的 img src 中工作?

[英]How can I get my PHP code working inside img src in Wordpress?

I've been trying to get a small image of an icon inside of my article thumbnail custom fields in Wordpress website.我一直在尝试在 Wordpress 网站的文章缩略图自定义字段中获取图标的小图像。 But the image itself isn't loading but the site understands that image is supposed to be there.但是图像本身没有加载,但网站知道图像应该在那里。

This is my PHP code right now这是我现在的 PHP 代码

<?php 
$filepath= "<?php bloginfo('template_url'); ?>/img/downarrow.png";
$meta_value = get_post_meta( $post->ID, 'Magamistuba', true ); 
            if  (!empty( $meta_value )) {echo   '<img src="'.$filepath.'">'.$meta_value;}
            else {} 

?>

On this picture it shows how it appears in the console.在这张图片上,它显示了它在控制台中的显示方式。

Here is how it is when i tryed这是我尝试时的情况

$filepath = bloginfo('template_url') ."/img/downarrow.png";

在此处输入图像描述

<?php 
$filepath= bloginfo('template_url') ."/img/downarrow.png";
$meta_value = get_post_meta( $post->ID, 'Magamistuba', true ); 
            if  (!empty( $meta_value )) {echo   '<img src="'.$filepath.'">'.$meta_value;}
            else {} 

You are already in PHP mode, so this is how you write it.您已经处于 PHP 模式,所以这就是您的编写方式。

Also, never end a PHP script by closing PHP tags.此外,永远不要通过关闭 PHP 标记来结束 PHP 脚本。

You don't need to write <?php a second time.您不需要第二次写<?php

Instead it is sufficient to write:相反,编写以下内容就足够了:

$filepath = bloginfo('template_url') . '/img/downarrow.png';
<?php 
    $filepath= bloginfo('template_url') . "/img/downarrow.png";
    $meta_value = get_post_meta( $post->ID, 'Magamistuba', true );
    if  (!empty( $meta_value )) {echo   '<img src="'.$filepath.'">'.$meta_value;}
    else {}
?>

That's because $filepath is a String and not PHP code.那是因为$filepath是一个字符串,而不是 PHP 代码。

You must put your code outside the string and concatenate .您必须将代码放在字符串之外并连接

<?php 
$filepath = bloginfo('template_url') ."/img/downarrow.png";
$meta_value = get_post_meta( $post->ID, 'Magamistuba', true ); 

if  (!empty( $meta_value )) {
    echo   '<img src="'.$filepath.'">'.$meta_value;
}

?>

Try with this.试试这个。

$filepath = get_template_directory_uri()."/img/downarrow.png";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM