Php 如果高级自定义字段中的值符合特定条件,则显示额外文本
我正在使用自定义Wordpress模板中的高级自定义字段来支持评论站点。 我当前的代码允许我显示我需要的内容,但现在我想显示一些额外的文本,如果任何评论在100分中得到90分或更多 我使用以下代码获取所有帖子及其评分:Php 如果高级自定义字段中的值符合特定条件,则显示额外文本,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,我正在使用自定义Wordpress模板中的高级自定义字段来支持评论站点。 我当前的代码允许我显示我需要的内容,但现在我想显示一些额外的文本,如果任何评论在100分中得到90分或更多 我使用以下代码获取所有帖子及其评分: <?php $posts = get_posts(array( 'posts_per_page'=> 12, 'paged' => $paged, 'post_type'=> 'movie',
<?php
$posts = get_posts(array(
'posts_per_page'=> 12,
'paged' => $paged,
'post_type'=> 'movie',
'meta_key' => 'movie_rating_john',
'orderby' => 'meta_value',
'order' => 'DESC'
));
if( $posts ): ?>
如果键的值为90或更大,您知道如何向该输出中添加一些文本吗?假设
电影评级中的值仅为一个数字,没有其他文本或字符,则您可以执行以下操作:
使用get_字段
而不是the_字段
将其保存在变量中
使用intval
将其转换为整数
检查该值以决定是否添加额外文本
把它们放在一起,你会得到下面的代码。将
替换为此
<?php
$rating_str = get_field('movie_rating_john'); // 1. Save value as variable
$rating_num = intval($rating_str); // 2. Convert to integer
if ($rating_num >= 90){ // 3. Check value
// if the value is greater than or equal to 90, echo the number and your text
echo $rating_num." this is your extra text here";
}
else{
// if the value is less than 90, just echo the number
echo $rating_num;
}
?>
<?php
$rating_str = get_field('movie_rating_john'); // 1. Save value as variable
$rating_num = intval($rating_str); // 2. Convert to integer
if ($rating_num >= 90){ // 3. Check value
// if the value is greater than or equal to 90, echo the number and your text
echo $rating_num." this is your extra text here";
}
else{
// if the value is less than 90, just echo the number
echo $rating_num;
}
?>