Include post title in readmore link?

Trying to include the post title in my excerpt readmore links. I’ve read the codex on this, but my theme files are much more complicated so I could use a bit of expert help…

Extract from functions.php: ** Edited to show more code………

Read More
function theme_get_content($args = array()) {

$more_tag = theme_get_array_value($args, 'more_tag', __('Continue Bob <span class="meta-nav">&rarr;</span>', THEME_NS));
theme_ob_start();
the_content($more_tag);
$content = theme_ob_get_clean();
return $content . wp_link_pages(array(
            'before' => '<p><span class="page-navi-outer page-navi-caption"><span class="page-navi-inner">' . __('Pages', THEME_NS) . ': </span></span>',
            'after' => '</p>',
            'link_before' => '<span class="page-navi-outer"><span class="page-navi-inner">',
            'link_after' => '</span></span>',
            'echo' => 0
        ));

}

function theme_get_excerpt($args = array()) {
global $post;
$more_tag = theme_get_array_value($args, 'more_tag', __('My Words...<span class="meta-nav">&rarr;</span>', THEME_NS));
$auto = theme_get_array_value($args, 'auto', theme_get_option('theme_metadata_excerpt_auto'));
$all_words = theme_get_array_value($args, 'all_words', theme_get_option('theme_metadata_excerpt_words'));
$min_remainder = theme_get_array_value($args, 'min_remainder', theme_get_option('theme_metadata_excerpt_min_remainder'));
$allowed_tags = theme_get_array_value($args, 'allowed_tags',
    (theme_get_option('theme_metadata_excerpt_use_tag_filter')
        ? explode(',',str_replace(' ', '', theme_get_option('theme_metadata_excerpt_allowed_tags')))
        : null));
$perma_link = get_permalink($post->ID);
$more_token = '%%theme_more%%';
$show_more_tag = false;
$tag_disbalance = false;
if (post_password_required($post)) {
    return get_the_excerpt();
}
if ($auto && has_excerpt($post->ID)) {
    $excerpt = get_the_excerpt();

    $show_more_tag = theme_strlen($post->post_content) > 0;
} else {
    $excerpt = get_the_content($more_token);
    // hack for badly written plugins
    theme_ob_start();
    echo apply_filters('the_content', $excerpt);
    $excerpt = theme_ob_get_clean();
    global $multipage;
    if ($multipage && theme_strpos($excerpt, $more_token) === false) {
        $show_more_tag = true;
    }
    if (theme_is_empty_html($excerpt))
        return $excerpt;
    if ($allowed_tags !== null) {
        $allowed_tags = '<' . implode('><', $allowed_tags) . '>';
        $excerpt = strip_tags($excerpt, $allowed_tags);
    }
    if (theme_strpos($excerpt, $more_token) !== false) {
        $excerpt = str_replace($more_token, $more_tag, $excerpt);
    } elseif ($auto && is_numeric($all_words)) {
        $token = "%theme_tag_token%";
        $content_parts = explode($token, str_replace(array('<', '>'), array($token . '<', '>' . $token), $excerpt));
        $content = array();
        $word_count = 0;
        foreach ($content_parts as $part) {
            if (theme_strpos($part, '<') !== false || theme_strpos($part, '>') !== false) {
                $content[] = array('type' => 'tag', 'content' => $part);
            } else {
                $all_chunks = preg_split('/([s])/u', $part, -1, PREG_SPLIT_DELIM_CAPTURE);
                foreach ($all_chunks as $chunk) {
                    if ('' != trim($chunk)) {
                        $content[] = array('type' => 'word', 'content' => $chunk);
                        $word_count += 1;
                    } elseif ($chunk != '') {
                        $content[] = array('type' => 'space', 'content' => $chunk);
                    }
                }
            }
        }

        if (($all_words < $word_count) && ($all_words + $min_remainder) <= $word_count) {
            $show_more_tag = true;
            $tag_disbalance = true;
            $current_count = 0;
            $excerpt = '';
            foreach ($content as $node) {
                if ($node['type'] == 'word') {
                    $current_count++;
                }
                $excerpt .= $node['content'];
                if ($current_count == $all_words) {
                    break;
                }
            }
            $excerpt .= '&hellip;'; // ...
        }
    }
}


if ($show_more_tag) {
    $excerpt = $excerpt . ' <a class="more-link" href="' . $perma_link . '">' . $more_tag . ' </a>';

}

if ($tag_disbalance) {
    $excerpt = force_balance_tags($excerpt);
}
return $excerpt;

}

Any ideas how to include the post title within the readmore permalink???
Thanks

Related posts

2 comments

  1. If the following code is in-fact the code that is controlling your read more link then you perhaps the following may work;

    if ($show_more_tag) {
        global $post;
        $excerpt = $excerpt . ' <a class="more-link" href="' . $perma_link . '">' . get_the_title($post->ID) . ' </a>';
    }
    

    I’ve declared global $post; again, just in case the above call to the conditional statement above is built outside of the initial $post shown in your first snippet; because you haven’t provided the full code to assess.

    But try the above and see if it has any effect and let us know.

  2. You can filter the read more link with the the_content_more_link filter.

    function the_content_more_link_wpse_100990($more) {
      global $post;
      $pattern = '|>([^<]+)<|';
      return preg_replace($pattern,">$1{$post->post_title} <",$more,1);
    }   
    add_filter('the_content_more_link','the_content_more_link_wpse_100990');
    

    I’d probably do some error checking if I were to put that into production but that is the idea.

Comments are closed.