English 中文(简体)
Echoing a string that contains PHP (in WordPress)
原标题:

I have a custom field in WordPress called "thumb-url" which contains the exact location of an image. I want to only display the image if "thumb-url" contains the location of the image.

I m start with an if statement that echoes photo exists if there s a value in the "thumb-url" custom field, otherwise it does nothing.

<div class="excerpt">
<?php
$key =  thumb-url ;
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta !=   ) {
echo  photo exists ;
}
?>

Now, here s the code that I really want the above if statement to echo if there s a value in "thumb-url":

<img alt="<?php the_title() ?>" src="<?php if ( function_exists( get_custom_field_value ) ){ get_custom_field_value( thumb-url , true); } ?>" align="absmiddle" height="62" width="62" class="writtenpostimg" />

How do I get that ↑ inside the echo part of the if statement?

Much appreciated...

最佳回答

Assuming you re echoing it to the page for some sort of copy/paste instructions:

<div class="excerpt">
<?php
$key =  thumb-url ;
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta !=   ) {
    echo htmlspecialchars( <img alt="<?php the_title() ?>" src="<?php if ( function_exists( get_custom_field_value ) ){ get_custom_field_value( thumb-url , true); } ?>" align="absmiddle" height="62" width="62" class="writtenpostimg" /> );
}

?>
问题回答

This escapes the code... The other comment actually prints out "<img...>", so it depends on what you re trying to do.

You can remove the PHP tags and use conditional expressions:

<div class="excerpt">
<?php
$key =  thumb-url ;
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta !=   ) {
echo  <img alt=" .the_title(). " src=" .(function_exists( get_custom_field_value )?get_custom_field_value( thumb-url , true):  ). " align="absmiddle" height="62" width="62" class="writtenpostimg" /> ;

It might be easier to understand if you actually use a variable for it:

$url = ""
if(function_exists( get_custom_field_value ))
  $url = get_custom_field_value( thumb-url , true);
echo  <img alt=" .the_title(). " src=" .$url. " align="absmiddle" height="62" width="62" class="writtenpostimg" /> ;




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签