我在自定义Wordpress模板中使用“高级自定义字段”来为审阅网站供电。我当前的代码可以显示所需信息,但是现在,如果任何评论获得90分以上(满分100分),我想显示一些额外的文字。
我使用以下代码获取所有帖子及其评分:
<?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 ): ?>
满分100分保存在movie_rating_john
密钥中,我可以这样输出:
<?php the_field('movie_rating_john'); ?>
有什么主意,如果键的值为90或更大,如何将一些文本添加到此输出中?
假设in中的值movie_rating_john
只是一个数字,没有其他文本或字符,那么您可以执行以下操作:
get_field
而不是将the_field
其保存在变量中intval
将其隐藏为整数放在一起,您将获得下面的代码。替换<?php the_field('movie_rating_john'); ?>
为此。
<?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;
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句