0

I'm working on a plugin that creates and displays Custom Post Types. I'm using ACF for additional fields.

Problem comes when I need to add the actual content into the filter. There's a slideshow in the normal wysiwyg editor that needs to be displayed there. See screengrab.

Is there a way to have the_content be displayed inside a filter that is using the_content? Or is there a different way of coding this?

Here is my code:

add_filter('the_content', 'fds_the_content');
function fds_the_content( $content ) {

    if (is_singular( 'services' ) && in_the_loop()) {
        ob_start(); ?>

        <div class="fds-top">

                    <div id="top-content" class="contain-columns">

                        <div id="portfolio" class="m-all t-2of3 d-3of4 gl">

                            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                            <div class="slideshow">

                                <section class="entry-content cf">
                                    <?php the_content(); ?>
                                </section> <!-- end article section -->

                            </div>

                            <?php endwhile; ?>

                            <?php else : ?> 

                            <?php endif; ?>

                        </div> <!-- end #portfolio -->
                        <div class="2-percent"></div> <!-- What a hack! -->
                        <div id="service-sidebar" class="m-all t-1of3 d-1of4 gl">

                            <h2>Recent 
                                    <?php
                                $category = get_the_category(); 
                                echo $category[0]->cat_name;
                                    ?>                                                      
                            </h2>

                            <?php 
                                global $post;
                                $categories = get_the_category();
                                foreach ($categories as $category) :
                                ?>

                                <?php
                                    // The Query
                                    $the_query = new WP_Query( 

                                        array( 
                                            'posts_per_page' => 2,
                                            'category__and' => array( $category->term_id ) ) );
                                ?>  
                                <?php if ( $the_query->have_posts() ) : ?>


                                <!-- the loop -->
                                <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                                    <a class="recent-post" href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" >
                                        <div class="recent-thumb"><?php the_post_thumbnail('medium'); ?></div>
                                        <h3 class="recent-blog-title-desktop"><?php the_title(); ?></h3>                    
                                    </a>                                                

                                <?php endwhile;  
                                    wp_reset_query(); ?>
                                    <!-- end of the loop -->

                                    <!-- pagination here -->
                                    <?php wp_reset_postdata(); ?>

                                <?php else:  ?>
                                  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
                                <?php endif; ?>



                            <?php endforeach; ?>    

                        </div>
                    </div>

                <div class="clear"></div>

                <div id="midriff" class="contain-columns">

                    <div id="service-info" class="m-all t-all d-all gl">
                        <header class="article-header">
                            <h1 class="single-title custom-post-type-title"><?php the_title(); ?></h1>
                        </header>

                        <section class="entry-content cf">

                            <div class="segment">
                                <div class="segment-photo-left">
                                    <?php 
                                    $image = get_field('general_photo');
                                    $size = 'medium'; // (thumbnail, medium, large, full or custom size)
                                    if( $image ) {
                                        echo wp_get_attachment_image( $image, $size );
                                    }
                                    ?>
                                </div>
                                <div class="segment-text"><?php the_field('general_info'); ?></div> 
                            </div>

                            <div class="segment">
                                <h2>Rates</h2>
                                <div class="segment-photo-right">
                                    <?php 
                                    $image = get_field('rates_photo');
                                    $size = 'medium'; // (thumbnail, medium, large, full or custom size)
                                    if( $image ) {
                                        echo wp_get_attachment_image( $image, $size );
                                    }
                                    ?>
                                </div>
                                <div class="segment-text"><?php the_field('rates'); ?></div>    
                            </div>

                            <div class="segment">
                                <h2>Areas Served</h2>
                                <div class="segment-photo-left">
                                    <?php 
                                    $image = get_field('areas_served_photo');
                                    $size = 'medium'; // (thumbnail, medium, large, full or custom size)
                                    if( $image ) {
                                        echo wp_get_attachment_image( $image, $size );
                                    }
                                    ?>
                                </div>
                                <div class="segment-text"><?php the_field('areas_served'); ?></div> 
                            </div>  

                            <div class="segment">
                                <h2>Kind Words</h2>

                                    <?php 

                                    // args
                                    $args = array(
                                        'posts_per_page'    =>  1,
                                        'post_type'         =>  'reviews',
                                        'meta_key'          =>  'review',
                                        'orderby'           =>  'rand'
                                    );

                                    // get results
                                    $the_query = new WP_Query( $args );

                                    // The Loop
                                    ?>
                                    <?php if( $the_query->have_posts() ): ?>
                                        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                                            <blockquote>

                                                <?php the_field('review'); ?>

                                            </blockquote>   
                                            <h3 class="client-sig">~<?php the_field('review_client_name'); ?></h3>  
                                        <?php endwhile; ?>
                                    <?php endif; ?>
                                    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

                            </div>  

                            <div class="segment">
                                <h2>Contact</h2>
                                <div class="segment-photo-right">
                                    <?php 
                                    $image = get_field('contact_photo');
                                    $size = 'medium'; // (thumbnail, medium, large, full or custom size)
                                    if( $image ) {
                                        echo wp_get_attachment_image( $image, $size );
                                    }
                                    ?>
                                </div>
                                <div class="segment-text"><?php the_field('contact'); ?></div>  
                            </div>

                        </section>

                    </div> <!-- End #service-info -->

                </div>      

                </div>

            </div>


        <?php
        echo ob_get_clean();    
    }
        return $content;
}

This shows the output from above code

1

First, your filter should return, not echo anything. The content will appear in the wrong place if you echo directly within the function.

The original contents of the_content are passed to the filter function as an argument-

function fds_the_content( $content ) { // <--- original value of the_content
    // append original $content
    $new_content = 'new content' . $content;
    return $new_content;
}

EDIT- echo $content in an output buffer-

function fds_the_content( $content ) {
    ob_start();
    ?>
    <div>
        <h4>Some Markup</h4>
        <?php echo $content; ?>
    </div>
    <?php
    return ob_get_clean();
}
add_filter('the_content', 'fds_the_content');
  • Thanks for the response. Wouldn't that just append the content to the new content? I'm hoping to have the original content inside the new content. – William Bay Mar 4 '15 at 21:53
  • you can insert $content anywhere you want it. – Milo Mar 4 '15 at 22:12
  • That's what I'm trying to figure out. How do I get the original contents to display inside the code that I have? – William Bay Mar 6 '15 at 20:07
  • see edit above. – Milo Mar 6 '15 at 20:21
  • That doesn't seem to work either... Here's a live example: aaronandwhitney.com/services/kansas-city-wedding-photography I used the echo and that doesn't seem to be doing anything... Is this possible to do? Or does it have to be recoded somehow? – William Bay Mar 6 '15 at 20:36

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.