【问题标题】:(WordPress) How do I shorten latest post exerpts and add a read more link(WordPress) 如何缩短最新的文章摘录并添加阅读更多链接
【发布时间】:2016-12-10 16:26:49
【问题描述】:

我正在使用最新版本的 WordPress 开发网站。

我的网站设置为首页显示最新帖子。

摘录显示了每个帖子的全部内容。 如何缩短这些内容并为每个内容添加一个阅读更多链接?

以下是每个帖子的循环播放内容:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
    <?php
    if ( is_single() ) :
        the_title( '<h1 class="entry-title">', '</h1>' );
    else :
        the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
    endif;

    if ( 'post' === get_post_type() ) : ?>
    <div class="entry-meta">
        <?php blogtristan_posted_on(); ?>
    </div><!-- .entry-meta -->
    <?php
    endif; ?>
</header><!-- .entry-header -->

<div class="entry-content">
    <?php
        the_content( sprintf(
            /* translators: %s: Name of current post. */
            wp_kses( __( 'Continue reading %s <span class="meta-nav">&rarr;</span>', 'blogtristan' ), array( 'span' => array( 'class' => array() ) ) ),
            the_title( '<span class="screen-reader-text">"', '"</span>', false )
        ) );

        wp_link_pages( array(
            'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'blogtristan' ),
            'after'  => '</div>',
        ) );
    ?>
</div><!-- .entry-content -->

<footer class="entry-footer">
    <?php blogtristan_entry_footer(); ?>
</footer><!-- .entry-footer -->

【问题讨论】:

  • 这是你的 index.php 吗?你试过the_excerpt()函数了吗?
  • 这是因为你使用了the_content()函数,它返回整个帖子内容
  • 好的,我已经把它改成了 。它现在只显示部分内容。如何添加阅读更多链接?

标签: php wordpress loops


【解决方案1】:

正如我们在您帖子的 cmets 中看到的,要缩短文本量,将 the_content() 函数替换为 the_excerpt() 就足够了。

要在摘录中添加“阅读更多”链接,需要自定义功能本身。 the_excerpt 在文本末尾添加字符串 [...]。 有一些方法可以做到这一点。

来自https://developer.wordpress.org/reference/functions/the_excerpt/

使用

自定义functions.php
/**
 * Filter the "read more" excerpt string link to the post.
 *
 * @param string $more "Read more" excerpt string.
 * @return string (Maybe) modified "read more" excerpt string.
 */
function wpdocs_excerpt_more( $more ) {
    return sprintf( '<a class="read-more" href="%1$s">%2$s</a>',
        get_permalink( get_the_ID() ),
        __( 'Read More', 'textdomain' )
    );
}
add_filter( 'excerpt_more', 'wpdocs_excerpt_more' );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 2017-04-04
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    相关资源
    最近更新 更多