Forums

[resolved] How do truncate and show read more, need help... (3 posts)

  1. Spencer Hill
    Member
    Posted 2 years ago #

    Heya, I feel like this is a really basic question and that I'm overlooking documentation but I can't find anything on the topic. I'm writing a search.php page and I need to limit the text to a certain number of characters and show a "..." then a read more link. Can anyone help me out? Thanks!

    <?php if (have_posts()) : ?>
    
    		<h1>Search Results</h1>
    
    		<div class="search-form"><?php get_search_form(); ?></div>
    
    		<div class="navigation">
    			<div class="alignleft">next<?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright">previ<?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    
    			<?php echo substr($post->post_content, 0, 200); ?>
    
    			<hr />
    
    		<?php endwhile; ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    
    	<?php else : ?>
    
    		<h2 class="center">No posts found. Try a different search?</h2>
    		<?php get_search_form(); ?>
    
    	<?php endif; ?>
  2. Chris_K
    Member
    Posted 2 years ago #

    Are you using a really old theme? Don't often see folks going after post_content directly like that these days.

    Try replacing
    <?php echo substr($post->post_content, 0, 200); ?>

    with

    <?php the_excerpt() ?>

    See also:
    http://codex.wordpress.org/Customizing_the_Read_More
    http://codex.wordpress.org/Template_Tags/the_excerpt

    If you want to customize the excerpt beyond what you get from the_excerpt() check out the plugins section and look for one of the "excerpts" customization plugins.

  3. Spencer Hill
    Member
    Posted 2 years ago #

    Oh that's awesome, I thought that pulled from the excerpt panel not just an excerpt of the actual content. Thanks!

    PS Nope, this is one I'm writing but I found that snippet on an old forum post.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.