过滤内容,但仍需要在过滤器中显示未过滤的内容

时间:2015-03-04 作者:William Bay

我正在开发一个插件,可以创建和显示自定义帖子类型。我将ACF用于其他字段。

当我需要将实际内容添加到过滤器中时,问题就出现了。普通wysiwyg编辑器中有一个幻灯片,需要在那里显示。请参见屏幕抓图。

是否有办法在使用\\u内容的筛选器中显示\\u内容?还是有其他的编码方式?

这是我的代码:

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 个回复
SO网友:Milo

首先,过滤器应该返回,而不是回显任何内容。如果您直接在函数中回音,则内容将显示在错误的位置。

原始内容the_content 作为参数传递给过滤器函数-

function fds_the_content( $content ) { // <--- original value of the_content
    // append original $content
    $new_content = \'new content\' . $content;
    return $new_content;
}
编辑-回声$content 在输出缓冲区中-

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\');

结束

相关推荐

Search with filters and title

我想搜索custom_post 按标题和ACF字段。所以,我用了WP_Query WordPress函数,但我不能按标题过滤,只能按过滤器过滤。当我提交表单时,我有这样的URL:http://example.com/?s=titre&filter1=condition1&filter2=condition2&filter3=condition3 我的代码:$title = $_GET[\'s\']; $args = array( \'pagenam