最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Cannot figure out how to add a read more link to a manual excerpt. Please help

programmeradmin0浏览0评论

Nothing I do works. I am using a manual excerpt and when I paste this code into functions.php the words read more appear but they are not a link. Why is this?

add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
    $output = $excerpt;

    if ( has_excerpt() ) {
        $output = sprintf( '%1$s <a href="%2$s">Read more</a>',
            $excerpt,
            get_permalink()
        );
    }

    return $output;
}

Nothing I do works. I am using a manual excerpt and when I paste this code into functions.php the words read more appear but they are not a link. Why is this?

add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
    $output = $excerpt;

    if ( has_excerpt() ) {
        $output = sprintf( '%1$s <a href="%2$s">Read more</a>',
            $excerpt,
            get_permalink()
        );
    }

    return $output;
}
Share Improve this question asked May 10, 2019 at 3:50 Samantha SheaSamantha Shea 1 4
  • get_permalink() accepts the post ID as the first parameter, if it is not passed, the function will use the global $post. Check if global $post is set. – nmr Commented May 10, 2019 at 6:05
  • get_permalink is wrong. The function is called "get_the_permalink()". – user3135691 Commented May 10, 2019 at 7:00
  • @user3135691 get_the_permalink() is only an alias for get_permalink() – nmr Commented May 10, 2019 at 8:01
  • Really? working with wp for several years, never heard of that. Thanks, that is baffeling me :-P – user3135691 Commented May 10, 2019 at 8:04
Add a comment  | 

1 Answer 1

Reset to default 0

There are several options you have. You can write your own excerpt, or just use the tools which WordPress offers to you.

There are three options you have:

  1. Use WordPress core function the_excerpt();
  2. Use WordPress core function get_the_excerpt();
  3. Use WordPress filter wp_trim_excerpt();

The easiest and propably most fitting for you is the first one.

<?php

    // WordPress template function
    the_excerpt();

 ?>

Excerpt from official WordPress docs:

Displays the excerpt of the current post after applying several filters to it including auto-p formatting which turns double line-breaks into HTML paragraphs. It uses get_the_excerpt() to first generate a trimmed-down version of the full post content should there not be an explicit excerpt for the post.

Now, you want to add a "read more custom link".

Basically, the first step is to add a WordPress filter to the excerpt. The filter we are using is called excerpt_more.

<?php
    // Mechanism to add a filter to something
    function functionname() {
        // Custom code to filter/modify

    }
    add_filter('filtername', 'functionname');
?>

The next step is to call the excerpt function where you want it.

 <?php
    // Put these 3 lines of code in your functions.php
    function wpdocs_excerpt_more( $more ) {
        return '<a href="'.get_the_permalink().'">Permalink</a>';
    }
    add_filter( 'excerpt_more', 'wpdocs_excerpt_more' );

    // This function call needs to be placed within "the loop" of your home.php
    the_excerpt();
 ?>

The function "the_excerpt" needs to be called within the loop. So, make sure you are adding it in the correct place.

发布评论

评论列表(0)

  1. 暂无评论