Wordpress自定义搜索结果页面不显示摘录



我有一个自定义的content-searchresults.php页面。然而,由于某些原因,它没有显示摘录,但我在这一点上做了其他修改,所以它使用这个模板。

下面是我的代码。

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

        <?php if ( has_post_thumbnail() && ! post_password_required() && !is_single() ) : ?>

            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
                <?php the_post_thumbnail('full', array('class' => 'img-responsive'));   ?>
            </a>

        <?php endif; ?>
        <div class="clearfix">
            <header class="entry-header ">
                <?php if ( in_array( 'category', get_object_taxonomies( get_post_type() ) ) && CrippsTheme_categorized_blog() ) : ?>
                <div class="entry-meta">
                    <span class="cat-links"><?php echo get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'CrippsTheme' ) ); ?></span>
                </div>
                <?php
                    endif;
                    if ( is_single() ) :
                        the_title( '<h1 class="entry-title">', '</h1>' );
                    else :
                        the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' );
                    endif;
                ?>
            </header><!-- .entry-header -->
        </div>

    <div class="entry-summary clearfix">
        <?php the_excerpt(); ?>
    </div><!-- .entry-summary -->
    <div class="clearfix">
    <a class="read-more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
        Read more
    </a>
    </div>

这不是因为它没有显示摘录,而是因为我有一个高级自定义字段,其中包含主副本。为了显示高级自定义字段摘录,我使用了以下命令:

添加到functions.php

function custom_field_excerpt() {
global $post;
$text = get_field('main_copy');
if ( '' != $text ) {
    $text = strip_shortcodes( $text );
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $excerpt_length = 45; // 45 words
    $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return apply_filters('the_excerpt', $text);
}

然后在您希望显示的位置添加以下内容:

<?php echo custom_field_excerpt(); ?>

最新更新