Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.........

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

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
Thank you for the suggestion. I have tried adding this to my functions.php but it did not make any difference I'm afraid. But thankyou for trying. I didn't post the whole excerpt section as I was concerned it was too big, will edit my question if it is required –  speedypancake May 28 '13 at 12:18
    
Scrub that. Posting the rest of the comment I realised you probably meant me to replace the exisitng if show more tag bit with your version - BINGO! Thank you so much for your help :) –  speedypancake May 28 '13 at 12:37
    
Yup, that's right... :) Glad it's working! All the best. –  userabuser May 28 '13 at 13:18

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.

share|improve this answer
    
Hey s_ha_dum - Thanks for the suggestion. I added your filter to the end of my functions.php file, but I'm afraid it didn't do anything. But never mind, it's probably something stupid I did wrong. Thanks anyway :) –  speedypancake May 28 '13 at 15:21
    
It will only work for a more tag generated by WordPress, or other, functions that run the the_content_more_link filter. –  s_ha_dum May 28 '13 at 18:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.