仅显示开机自检开始

时间:2017-03-28 作者:Shan-x

我知道这通常是一个很明显的问题,有很多答案(比如here), 但我只想用“阅读更多”按钮显示帖子的开头。我不使用摘录,所以我希望Wordpress自动抓取前55个单词或第一个短语来显示它。

我使用主题Toivo Lite。索引。php是:

<?php
get_header(); ?>

<?php if ( have_posts() ) : ?>

    <?php do_action( \'toivo_before_loop\' ); // Action hook before loop. ?>

    <?php /* Start the Loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>

        <?php
            /* Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */
            get_template_part( \'content\', ( post_type_supports( get_post_type(), \'post-formats\' ) ? get_post_format() : get_post_type() ) );
        ?>

    <?php endwhile; ?>

    <?php
        the_posts_pagination( array(
            \'prev_text\'          => __( \'Previous page\', \'toivo-lite\' ),
            \'next_text\'          => __( \'Next page\', \'toivo-lite\' ),
            \'before_page_number\' => \'<span class="meta-nav screen-reader-text">\' . __( \'Page\', \'toivo-lite\' ) . \' </span>\',
        ) );
    ?>

<?php else : ?>

    <?php get_template_part( \'content\', \'none\' ); ?>

<?php endif; ?>

<?php do_action( \'toivo_after_loop\' ); // Action hook after loop. ?>

<?php get_footer(); ?>
我试着替换这两个content 通过excerpt, 但是页面是空的(只有页眉和页脚)。

2 个回复
SO网友:Industrial Themes

尝试替换此:

        <?php
            /* Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */
            get_template_part( \'content\', ( post_type_supports( get_post_type(), \'post-formats\' ) ? get_post_format() : get_post_type() ) );
        ?>
使用此选项:

<?php echo my_excerpt(get_the_ID(), 300, \'Read More\'); ?>
然后将其添加到函数中。php文件:

function my_excerpt($postid, $len = 500, $more = false) {

    $excerpt = apply_filters(\'the_excerpt\', get_post_field(\'post_excerpt\', $postid));

    if(empty($excerpt)) $excerpt = strip_shortcodes(strip_tags(apply_filters(\'the_excerpt\', get_post_field(\'post_content\', $postid))));

    if(strlen($excerpt) > $len) $excerpt = substr($excerpt, 0, $len-3) . \'&hellip;\';

    if($more) $excerpt .= \'<a class="read-more" href="\' . get_the_permalink($postid) . \'">\' . $more . \'</a>\';

    return $excerpt;
}
默认情况下,将返回帖子内容的前300个字符。将my\\u摘录调用中的“300”更改为要返回的字符数(注意,这不是要返回的单词数,而是要返回的字符数)。

SO网友:Joe

您可以尝试以下操作:

    <?php
    get_header(); ?>

    <?php if ( have_posts() ) : ?>

        <?php do_action( \'toivo_before_loop\' ); // Action hook before loop. ?>

        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>

            <?php
            /* Include the Post-Format-specific template for the content.
            * If you want to override this in a child theme, then include a file
            * called content-___.php (where ___ is the Post Format name) and that will be used instead.
            */
            echo wp_trim_words( get_the_content(), $num_words = 50, $more = \'...\' );
            echo \'<a href="\'.get_the_permalink().\'">Read More</a>\';
            ?>

        <?php endwhile; ?>

        <?php
        the_posts_pagination( array(
        \'prev_text\'          => __( \'Previous page\', \'toivo-lite\' ),
        \'next_text\'          => __( \'Next page\', \'toivo-lite\' ),
        \'before_page_number\' => \'<span class="meta-nav screen-reader-text">\' . __( \'Page\', \'toivo-lite\' ) . \' </span>\',
        ) );
        ?>

    <?php else : ?>

        <?php get_template_part( \'content\', \'none\' ); ?>

    <?php endif; ?>

    <?php do_action( \'toivo_after_loop\' ); // Action hook after loop. ?>

    <?php get_footer(); ?>
我已替换:

    get_template_part( \'content\', ( post_type_supports( get_post_type(), \'post-formats\' ) ? get_post_format() : get_post_type() ) );
使用:

    wp_trim_words( the_content(), $num_words = 50, $more = \'...\' );
有人可能会告诉我这是错误的方法,但它可能会帮助你。它将显示内容的前50个单词,并带有“…”跟随它。

相关推荐

Child page excerpt

我有一个包含一些子页面的父页面。我想做的是在父页面上的自己的单元格中显示每个子页面摘录和一些其他信息。有人知道如何做到这一点吗?<div class=\"parent page\"> <?php $args = array( \'parent\' => $post->ID, \'post_type\' => \'page\', \'post_status\'