0

Filter

    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 ;
    }

Function

    //////////////////// 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- Why this function not shown at bottom of content ?

2- Why when we used this filter at top of content it's show review box at

summary of content at category page !!

  • 1
    How are those two pieces of code related? – s_ha_dum Nov 16 '15 at 0:40
  • We edited question, Simply we need to add filter that add function before or after content inside loop – meno Nov 16 '15 at 11:04
0

1- Why this function not shown at bottom of content ?

Your filter must return content, not echo content, but you call sandy_reviews() inside the filter callback and sandy_reviews() prints content when it runs. You need to rewrite that function to return content instead of echo it.

2- Why when we used this filter at top of content it's show review box at

summary of content at category page !!

Your category archive is using the_content() to display the summaries and your code does nothing to prevent that. You want something like:

add_filter( 'the_content', 'sandy_posts_filter' );
function sandy_posts_filter( $content )
{
  if (!is_single()) return $content;
  // the rest of your code
0

Based on your question, you can add a filter to the_content to add custom content before or after the actual content. This will give you a headstart.

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' );
  • $myvar extracted depend on foreach loop, So can we do something like this foreach ( $my_box_pos as $key => $val ) { if ( $val[moz] == "bottom" || $val[moz] == "top_and_bottom" ){ add_filter( 'the_content', 'sandy_filter_the_content' ); /////OR $content = sandy() . $content; } – meno Nov 16 '15 at 15:59
  • Of course you can do that. – Abhik Nov 16 '15 at 16:34
  • Can you tell us how? Or add a code here Thanks – meno Nov 16 '15 at 16:38
0

Filter hook should return code not echo or print. You can use the_content filter

    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;
    }

See: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

  • We edited question, Now can you help – meno Nov 17 '15 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.