按功能在内容顶部或底部添加审阅框

时间:2015-11-16 作者:meno

滤器

    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-为什么我们在内容顶部使用此过滤器,它的显示审阅框位于

分类页面的内容摘要!!

3 个回复
最合适的回答,由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

SO网友:Mahfuzul Hasan

过滤器挂钩应返回不回显或打印的代码。您可以使用the_content 滤器

    add_filter( \'the_content\', \'my_the_content_filter\', 20 );
    /**
     * Filtering The Content.
     */
    function my_the_content_filter( $content ) {

        $after = "<div>After div box</div>";
        $before = "<div>Before div box</div>";

        $content = $after . $content . $before;

        // Returns the content.
        return $content;
    }
请参见:https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

SO网友:Abhik

根据您的问题,您可以将筛选器添加到the_content 在实际内容之前或之后添加自定义内容。这将给你一个开端。

function wepse208756_filter_the_content( $content ) {

    $customcontent = \'My Custom Content\';

    if ( isset($myvar) && $myvar == \'top\' ) { //Adds Before The Content

        $content = $customcontent . $content;

    } elseif ( isset($myvar) && $myvar == \'bottom\' ) { //Adds After The Content

        $content = $content . $customcontent;

    }

    return $content;
}
add_filter( \'the_content\', \'wepse208756_filter_the_content\' );

相关推荐

是否可以取消对特定帖子类型的POSTS_PER_PAGE限制?

我想知道我是否可以取消特定帖子类型的posts\\u per\\u页面限制。在存档中。php页面我显示不同的帖子类型,对于特定的“出版物”帖子类型,我想显示所有帖子。我如何在不影响传统“post”类型的情况下实现这一点?