滤器
add_filter( \'the_content\', \'sandy_posts_filter\' );
function sandy_posts_filter( $content )
{
global $post;
$review_box_pos =get_post_meta( $post->ID, \'repeatable_fields\', true );
if (is_array($review_box_pos)) {
foreach ($review_box_pos as $key => $val) {
switch ($val[nameooz]) {
case \'top\':
$content = sandy_reviews() . $content;
break;
case \'bottom\':
//$content .= "Extra Content" ; Work at footer Perfect//
$content .= sandy_reviews() ; //shown at top only !!!
break;
} //End Switch
}
}
return $content ;
}
功能
//////////////////// Reviews Box ///////////////////////
function sandy_reviews() {
$get_meta = get_post_custom($current_ID);
$reviews = (get_post_meta(get_the_ID(), \'repeatable_fields\', true));
$sumArray = array();
if (is_array($reviews))
foreach($reviews as $k => $val) {
foreach($val as $id => $value) {
$sumArray[$id] += $value;
}
}
$Reviwes_count = count($reviews);
$Total_Reviwes = round(($sumArray[range] / $Reviwes_count), 1);
if (!empty($reviews)) { ?>
<div class = "reviews-box" >
<div class = "reviews-box-title" >
<?php
$get_meta = get_post_custom($current_ID);
if (!empty($reviews))
foreach($reviews as $val) {
echo $val["nameoo"];
} ?>
</div>
<?php
//Get Round Number
if (!empty($reviews))
foreach($reviews as $key => $val) {
if (!empty($val[range]))
$reviews_title = $val[nameoo];
$ranges = $val[range];
$range1 = ($val[range] * 0.01) * (5);
$range2 = floor(($range1 * 2) / 2);
$Reviwes = ($range1 * 20);
?>
<div class = "reviews-box-row" >
<div class = "reviews-box-keywords" > <?php echo $val[name]; ?> </div> <div class = "reviews-box-ranges" >
<span style = "display: block;float:left; width: 65px; height: 13px; background: url( <?php echo get_template_directory_uri();?>/images/star-rating-sprite.png) 0 0;" >
<span style = "display: block;float:left; width: <?php echo $ranges.\'%\';?>; height: 13px; background: url( <?php echo get_template_directory_uri();?>/images/star-rating-sprite.png) 0 -13px;" > </span> </span> <?php
echo "</div></div>";}
?>
<div class = "reviews-box-percent" > Summary </div>
<!-- <div class="reviews-box-ranges"><?php //echo ($val[range]*0.01)*(5);?></div> -->
<div class = "reviews-box-ranges-percent" >
<div class = "Total_Reviwes" > <?php echo $Total_Reviwes.""; ?> </div>
<span style = "display: inline-block; margin:0 auto;width: 65px; height: 13px; background: url( <?php echo get_template_directory_uri();?>/images/star-rating-sprite.png) 0 0;" >
<span style = "display: block; margin:0 auto;float:left;width: <?php echo $Total_Reviwes.\'%\';?>; height: 13px; background: url( <?php echo get_template_directory_uri();?>/images/star-rating-sprite.png) 0 -13px;" > </span> </span>
<?php
echo "</div>";
echo "</div>";
}
}
//////////////////// Reviews Box END///////////////////////
1-为什么此功能未显示在内容底部?
2-为什么我们在内容顶部使用此过滤器,它的显示审阅框位于
分类页面的内容摘要!!
最合适的回答,由SO网友:s_ha_dum 整理而成
1-为什么此功能未显示在内容底部?
筛选器必须返回内容,而不是echo
内容,但你打电话sandy_reviews()
在筛选器回调中,以及sandy_reviews()
运行时打印内容。您需要重写该函数以返回内容,而不是echo
信息技术
2-为什么我们在内容顶部使用此过滤器,它的显示审阅框位于
分类页面的内容摘要!!
您的类别存档正在使用the_content()
要显示摘要,您的代码不会阻止这一点。您想要的是:
add_filter( \'the_content\', \'sandy_posts_filter\' );
function sandy_posts_filter( $content )
{
if (!is_single()) return $content;
// the rest of your code