好的,下面的代码有效。 我已经为烂番茄的分数设置了一个自定义字段,我可以将其添加到我的蓝光评论中。 根据它的分数(高于 60 将获得"新鲜"评级,低于"烂")它将显示相应的图像。
这工作正常。
但。。。它还显示在每个页面上;即使是那些还没有分配分数的人。
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$result = ( get_post_meta($postid, 'ecpt_tomatometer', true));
if ($result >= 60) {
echo '<img src="/images/misc/fresh.png" width="102" height="25"> <span class=tomatometer>' . get_post_meta($postid, 'ecpt_tomatometer', true) . '%</span><br />';
}
else {
echo '<img src="/images/misc/rotten.png" width="102" height="25"> <span class=tomatometer>' . get_post_meta($postid, 'ecpt_tomatometer', true) . '%</span><br />';
}
?>
现在下面是另一个有效的代码片段,但我似乎无法将两者交织在一起。 这(下面)基本上是说"如果自定义字段中有内容,则显示代码(上面),否则不显示任何内容。
所以我有我想工作的两个部分,但我似乎无法让他们一起工作。
<?php
global $wp_query;
$postid = $wp_query->post->ID;
if( get_post_meta($postid, 'tomatometer', true))
{ ?>
This won't show up if there's nothing in the field.
<?php }
elseif( get_post_meta($postid, 'ecpt_tomatometer', true)) {
?>
this will display all of the information I need it to.
<?php } ?>
想法?
仅当结果具有值时才进行结果比较(如果没有评级,则假设为 null 或 false)。
global $wp_query;
$postid = $wp_query->post->ID;
if($result = get_post_meta($postid, 'ecpt_tomatometer', true)){
if ($result >= 60) {
echo '<img src="/images/misc/fresh.png" width="102" height="25"> <span class=tomatometer>' . get_post_meta($postid, 'ecpt_tomatometer', true) . '%</span><br />';
}
else {
echo '<img src="/images/misc/rotten.png" width="102" height="25"> <span class=tomatometer>' . get_post_meta($postid, 'ecpt_tomatometer', true) . '%</span><br />';
}
}
不确定$result的可能值,但我会尝试类似的东西:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$result = ( get_post_meta($postid, 'ecpt_tomatometer', true));
if (!($result===false) && intval($result) > 0)
{
if ($result >= 60) {
echo '<img src="/images/misc/fresh.png" width="102" height="25"> <span class=tomatometer>' . get_post_meta($postid, 'ecpt_tomatometer', true) . '%</span><br />';
}
else {
echo '<img src="/images/misc/rotten.png" width="102" height="25"> <span class=tomatometer>' . get_post_meta($postid, 'ecpt_tomatometer', true) . '%</span><br />';
}
}
?>
这应该有效。