在 WordPress 主题中显示 single.php 模板的帖子摘录

问题描述 投票:0回答:1

我在新主题中显示帖子摘录时遇到问题,因为它不支持帖子摘录,所以我必须手动添加它。但由于我是编码新手,我不知道该怎么办。我应该在哪里添加代码以及应该使用什么代码?

通过一些搜索,我发现我必须在主题的 single.php 文件中使用 get_the_excerpt 函数。但我不知道把它放在哪里以及实际使用什么线。 我尝试添加在网上搜索时找到的行,但没有一个起作用。

我想将摘录文本放在帖子特色图片下方。

提前致谢。

我的主题 Single.php 文件:

<?php
/**
 * @author  RadiusTheme
 * @since   1.0
 * @version 1.0
 */

// Layout class
if ( NeeonTheme::$layout == 'full-width' ) {
    $neeon_layout_class = 'col-sm-12 col-12';
}
else{
    $neeon_layout_class = NeeonTheme_Helper::has_active_widget();
}
$neeon_has_entry_meta  = ( NeeonTheme::$options['post_date'] || NeeonTheme::$options['post_author_name'] || NeeonTheme::$options['post_comment_num'] || ( NeeonTheme::$options['post_length'] && function_exists( 'neeon_reading_time' ) ) || NeeonTheme::$options['post_published'] && function_exists( 'neeon_get_time' ) || ( NeeonTheme::$options['post_view'] && function_exists( 'neeon_views' ) ) ) ? true : false;

$neeon_comments_number = number_format_i18n( get_comments_number() );
$neeon_comments_html = $neeon_comments_number == 1 ? esc_html__( 'Comment' , 'neeon' ) : esc_html__( 'Comments' , 'neeon' );
$neeon_comments_html = '<span class="comment-number">'. $neeon_comments_number .'</span> '. $neeon_comments_html;
$neeon_author_bio = get_the_author_meta( 'description' );

$neeon_time_html       = sprintf( '<span>%s</span><span>%s</span>', get_the_time( 'd' ), get_the_time( 'M' ), get_the_time( 'Y' ) );
$neeon_time_html       = apply_filters( 'neeon_single_time', $neeon_time_html );
$youtube_link = get_post_meta( get_the_ID(), 'neeon_youtube_link', true );

if ( ! empty(has_post_thumbnail() ) || ( !empty( $neeon_post_gallerys_raw ) && 'gallery' == get_post_format( $id ) ) ) {
    $img_class ='show-image';
}else {
    $img_class ='no-image';
}

if( NeeonTheme::$options['image_blend'] == 'normal' ) {
    $blend = 'normal';
}else {
    $blend = 'blend';
}

?>
<?php get_header(); ?>

<div id="primary" class="content-area <?php echo esc_attr($blend); ?>">
    <?php if ( NeeonTheme::$options['before_post_ad_option'] ) { ?>
    <div class="content-top-ad">
        <div class="container">
            <div class="content-top-ad-item">
                <?php if ( NeeonTheme::$options['ad_before_post_type'] == 'post_before_adimage' ) { ?>
                <?php if (NeeonTheme::$options['post_before_ad_img_link']){
                    $target = NeeonTheme::$options['post_before_ad_img_target']? 'target="_blank"':'';
                    echo '<a '.$target.'  href="'.esc_url( NeeonTheme::$options['post_before_ad_img_link'] ).'">'.wp_get_attachment_image( NeeonTheme::$options['post_before_adimage'], 'full' ).'</a>';

                } else {
                    echo wp_get_attachment_image( NeeonTheme::$options['post_before_adimage'], 'full' );
                }?> 

                <?php } else {
                    echo NeeonTheme::$options['post_before_adcustom'];
                } ?>        
            </div>
        </div>
    </div>
    <?php } ?>

    <input type="hidden" id="neeon-cat-ids" value="<?php echo implode(',', wp_get_post_categories( get_the_ID(), array( 'fields' => 'ids' ) ) ); ?>">

    <?php if ( NeeonTheme::$options['post_style'] == 'style1' ) { ?>
        <div id="contentHolder">
            <div class="container">
                <div class="row">               
                    <?php if ( NeeonTheme::$layout == 'left-sidebar' ) { get_sidebar(); } ?>
                        <div class="<?php echo esc_attr( $neeon_layout_class );?>">
                            <main id="main" class="site-main"> 
                                <div class="rt-sidebar-sapcer <?php echo ( NeeonTheme::$options['scroll_post_enable'] == 1 ) ? esc_attr( 'ajax-scroll-post' ) : ''; ?>">
                                <?php while ( have_posts() ) : the_post(); ?>
                                    <?php get_template_part( 'template-parts/content-single', get_post_format() );?>                        
                                <?php endwhile; ?> 
                                </div> 
                            </main>
                        </div>
                    <?php if ( NeeonTheme::$layout == 'right-sidebar' ) { get_sidebar(); }  ?>
                </div>
            </div>
        </div>
    <?php } else if ( NeeonTheme::$options['post_style'] == 'style2' ) { ?>
        <div id="contentHolder">
            <div class="<?php echo ( NeeonTheme::$options['scroll_post_enable'] == 1 ) ? esc_attr( 'ajax-scroll-post' ) : ''; ?>">
                <?php while ( have_posts() ) : the_post(); ?>
                    <?php get_template_part( 'template-parts/content-single-2', get_post_format() );?>
                <?php endwhile; ?>              
            </div>
        </div>
    <?php } else if ( NeeonTheme::$options['post_style'] == 'style3' ) { ?>
    <div id="contentHolder">
        <div class="post-detail-style3">
            <div class="container">
                <div class="entry-thumbnail-area <?php echo esc_attr($img_class); ?>">
                    <?php
                    $swiper_data=array(
                        'slidesPerView'     =>1,
                        'centeredSlides'    =>false,
                        'loop'              =>true,
                        'spaceBetween'      =>0,
                        'slideToClickedSlide' =>true,
                        'slidesPerGroup' => 1,
                        'autoplay'              =>array(
                            'delay'  => 1,
                        ),
                        'speed'      =>500,
                        'breakpoints' =>array(
                            '0'    =>array('slidesPerView' =>1),
                            '576'    =>array('slidesPerView' =>1),
                            '768'    =>array('slidesPerView' =>1),
                            '992'    =>array('slidesPerView' =>1),
                            '1200'    =>array('slidesPerView' =>1),             
                            '1600'    =>array('slidesPerView' =>1)              
                        ),
                        'auto'   =>false
                    );

                    $swiper_data = json_encode( $swiper_data );
                    $neeon_post_gallerys_raw = get_post_meta( get_the_ID(), 'neeon_post_gallery', true );
                    $neeon_post_gallerys = explode( "," , $neeon_post_gallerys_raw );

                    if ( !empty( $neeon_post_gallerys_raw ) && 'gallery' == get_post_format( $id ) ) { ?>
                        <div class="rt-swiper-slider gallery-slider rt-swiper-nav-1" data-xld = '<?php echo esc_attr( $swiper_data ); ?>'>
                            <div class="swiper-wrapper">
                                <?php foreach( $neeon_post_gallerys as $neeon_post_gallery ) { ?>
                                <div class="swiper-slide">
                                    <?php echo wp_get_attachment_image( $neeon_post_gallery, 'full', "", array( "class" => "img-responsive" ) );
                                    ?>
                                </div>
                                <?php } ?>
                            </div>
                            <div class="swiper-navigation">
                                <div class="swiper-button-prev"><i class="fas fa-chevron-left"></i></div>
                                <div class="swiper-button-next"><i class="fas fa-chevron-right"></i></div>
                            </div>
                        </div>
                    <?php } else { ?>
                        <?php if ( has_post_thumbnail() && ( NeeonTheme::$options['post_featured_image'] == true ) ) { ?>
                            <?php the_post_thumbnail( 'neeon-size1' , ['class' => 'img-responsive'] ); ?>
                        <?php } ?>
                        <?php if ( ( 'video' == get_post_format( $id ) && !empty( $youtube_link ) ) ) { ?>
                            <div class="rt-video"><a class="rt-play play-btn-white-xl rt-video-popup" href="<?php echo esc_url( $youtube_link );?>"><i class="fas fa-play"></i></a></div>
                        <?php } ?>

                    <?php } ?>

                    <div class="entry-header">                      
                        <?php if ( NeeonTheme::$options['post_cats'] ) { ?><span class="entry-categories"><?php echo neeon_category_prepare(); ?></span><?php } ?>
                        <h1 class="entry-title"><?php the_title();?></h1>
                        <?php if ( $neeon_has_entry_meta ) { ?>
                        <ul class="entry-meta">             
                            <?php if ( NeeonTheme::$options['post_author_name'] ) { ?>
                            <li class="item-author"><?php esc_html_e( 'by ', 'neeon' );?><?php the_author_posts_link(); ?>
                            </li>
                            <?php } if ( NeeonTheme::$options['post_date'] ) { ?>   
                            <li><i class="far fa-calendar-alt"></i><?php echo get_the_date(); ?></li>   
                            <?php } if ( NeeonTheme::$options['post_comment_num'] ) { ?>
                            <li><i class="far fa-comments"></i><?php echo wp_kses( $neeon_comments_html , 'alltext_allow' ); ?></li>
                            <?php } if ( NeeonTheme::$options['post_length'] && function_exists( 'neeon_reading_time' ) ) { ?>
                            <li class="meta-reading-time meta-item"><i class="far fa-clock"></i><?php echo neeon_reading_time(); ?></li>
                            <?php } if ( NeeonTheme::$options['post_view'] && function_exists( 'neeon_views' ) ) { ?>
                            <li><i class="fas fa-signal"></i><span class="meta-views meta-item "><?php echo neeon_views(); ?></span></li>
                            <?php } if ( NeeonTheme::$options['post_published'] && function_exists( 'neeon_get_time' ) ) { ?>   
                            <li><i class="fas fa-clock"></i><?php echo neeon_get_time(); ?></li>
                            <?php } ?>
                        </ul>
                        <?php } ?>  
                    </div>      
                </div>
            </div>
        </div>
        <div class="container">
            <div class="row">
                <?php if ( NeeonTheme::$layout == 'left-sidebar' ) { get_sidebar(); } ?>
                    <div class="<?php echo esc_attr( $neeon_layout_class );?>">
                        <main id="main" class="site-main">
                            <div class="rt-sidebar-sapcer <?php echo ( NeeonTheme::$options['scroll_post_enable'] == 1 ) ? esc_attr( 'ajax-scroll-post' ) : ''; ?>">
                            <?php while ( have_posts() ) : the_post(); ?>
                                <?php get_template_part( 'template-parts/content-single-3', get_post_format() );?>                      
                            <?php endwhile; ?>
                            </div>
                        </main>
                    </div>
                <?php if ( NeeonTheme::$layout == 'right-sidebar' ) { get_sidebar(); }  ?>
            </div>
        </div>
    </div>
    <?php } ?>
</div>
<?php get_footer(); ?>
php wordpress wordpress-theming custom-wordpress-pages
1个回答
0
投票

我发现我找错地方了。 我必须编辑 Template-parts 文件夹中的 content-single.php 文件。

我在帖子摘录中使用了此代码:

        <div class="entry-content"><?php the_excerpt();?>
    <?php if ( $_post = get_post() ) {
    setup_postdata( $post = $_post );
    the_excerpt();
    wp_reset_postdata();
    } ?>
    </div>

在 the_content 上方插入摘录代码。

我使用 WP Subtitle 插件和此 API 代码在 the_title 下插入帖子字幕:

        <?php do_action( 'plugins/wp_subtitle/the_subtitle', array(
         'before'        => '<p class="subtitle">',
         'after'         => '</p>',
         'post_id'       => get_the_ID(),
         'default_value' => ''
          ) ); ?> 
© www.soinside.com 2019 - 2024. All rights reserved.