Web Design Forum: [WP] Custom Excerpt Issue - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[WP] Custom Excerpt Issue Rate Topic: -----

#1 User is online   MikeChipshop 

  • Small but imperfectly formed
  • Group: Moderators
  • Posts: 7,046
  • Joined: 19-April 10
  • Reputation: 503
  • Gender:Male
  • Location:Scotland

Posted 24 January 2012 - 12:23 PM

Morning chaps, got an issue with custom excerpts in WordPress.
I'm using this in my functions file...

function twentyten_custom_excerpt_more( $output ) {
	if ( has_excerpt() && ! is_attachment() ) {
		$output .= twentyten_continue_reading_link();
	}
	return $output;
}
add_filter( 'get_the_excerpt', 'twentyten_custom_excerpt_more' );

/* Custome Excerpt Lengths */
function cust_excerpt($charlength) {
   $excerpt = get_the_excerpt();
   $charlength++;
   if(strlen($excerpt)>$charlength) {
       $subex = substr($excerpt,0,$charlength-5);
       $exwords = explode(" ",$subex);
       $excut = -(strlen($exwords[count($exwords)-1]));
       if($excut<0) {
            echo substr($subex,0,$excut);
       } else {
       	    echo $subex;
       }
       echo "...";
   } else {
	   echo $excerpt;
   }
}


and calling it in the loop like so...

<?php echo cust_excerpt(120); ?>


The issue i am having is with the following code...

<!-- Get posts -->
<?php get_featured_posts(array('method' => 'the_loop')); while (have_posts()) : the_post(); ?>

<!-- get thumbnail -->
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
<?php if ( has_post_thumbnail() ) the_post_thumbnail('header-large-thumb');	else { ?>
<img src="<?php bloginfo( 'template_directory' ); ?>/images/placeholder-image.jpg" width="490px" height="277px" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /><?php } ?>
</a>

<!-- get post title -->
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>

<!-- get post meta -->
<?php the_time('F jS, Y') ?> &bull; by <?php the_author_posts_link(); ?>

<!-- Get social plugin -->
<?php
    if(function_exists('display_social4i'))
    echo display_social4i("large","float-left");
?>

<!-- Get custom excerpt -->
<?php echo cust_excerpt(300); ?>

<!-- kill loop -->
<?php endwhile; wp_reset_query(); ?>


The issue is that when i try and have an excerpt length of 300 it just won't happen, it stays at around 120 and also the ellipses go from what they should be ... to the default WP ones of [...] this makes me think it's defaulting to the original excerpts somehow.

Any thoughts? Or anyone know of a better way of getting custom excerpt lengths?

This post has been edited by MikeChipshop: 24 January 2012 - 12:25 PM

0

#2 User is online   sash_oo7 

  • Forum Newcomer
  • PipPipPipPip
  • Group: Members
  • Posts: 838
  • Joined: 15-August 10
  • Reputation: 45
  • Gender:Male
  • Location:Mars
  • Experience:Nothing
  • Area of Expertise:Nothing

Posted 24 January 2012 - 01:00 PM

have u tried this or this ? orthis

This post has been edited by sash_oo7: 24 January 2012 - 01:13 PM

0

#3 User is online   Spitfire 

  • Mighty Pirate™
  • PipPipPipPip
  • Group: Members
  • Posts: 891
  • Joined: 05-February 11
  • Reputation: 189
  • Gender:Male
  • Location:Berkshire
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 24 January 2012 - 01:52 PM

I use this function to tidy up the excerpts on my blog.

function improved_trim_excerpt($text) {
	global $post;
	if ( '' == $text ) {
		$text = get_the_content('');
		$text = apply_filters('the_content', $text);
		$text = str_replace('\]\]\>', ']]&gt;', $text);
		$text = str_replace('[...]', '...', $text);
		$text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
		$text = strip_tags($text, '<p>');
		$excerpt_length = 80;
		$words = explode(' ', $text, $excerpt_length + 1);
		if (count($words)> $excerpt_length) {
			array_pop($words);
			array_push($words, '...');
			array_push($words, ' <a href="'. get_permalink($post->ID) . '">' . 'Continue Reading &raquo;' . '</a>');
			$text = implode(' ', $words);
		}
	}
	return $text;
}

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'improved_trim_excerpt');


In short - it lets you alter the excerpt length, changes the horrible [...], and keeps the paragraphs intact but removes all other HTML and scripts.

This post has been edited by Spitfire: 24 January 2012 - 01:55 PM

0

#4 User is online   MikeChipshop 

  • Small but imperfectly formed
  • Group: Moderators
  • Posts: 7,046
  • Joined: 19-April 10
  • Reputation: 503
  • Gender:Male
  • Location:Scotland

Posted 24 January 2012 - 01:53 PM

View Postsash_oo7, on 24 January 2012 - 01:00 PM, said:

have u tried this or this ? orthis


Cheers sash, i think i'm going to look at the plugin as all the hard coded options i've worked through still baulk at this bit.
0

#5 User is online   MikeChipshop 

  • Small but imperfectly formed
  • Group: Moderators
  • Posts: 7,046
  • Joined: 19-April 10
  • Reputation: 503
  • Gender:Male
  • Location:Scotland

Posted 24 January 2012 - 02:02 PM

View PostSpitfire, on 24 January 2012 - 01:52 PM, said:


In short - it lets you alter the excerpt length, changes the horrible [...], and keeps the paragraphs intact but removes all other HTML and scripts.


Cheers Spitfire, what i was actually trying to do was have custom length excerpts for different situations (normal length for the post list, shorter for highlights box and medium length for an editors pick box). However any hard coded solutions where baulking when i tried to make them longer than 120 characters (well, around that anyway).

Thankfully that plugin that sash posted above does the job. I'd still much rather do it without a plugin but beggars can't be choosers eh? ;)
0

#6 User is online   funsella 

  • Advanced Member
  • Group: Platinum Membership
  • Posts: 250
  • Joined: 26-July 11
  • Reputation: 19
  • Gender:Male
  • Location:London
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 24 January 2012 - 02:27 PM

I bookmarked this a while back but havnt had a chance to try it out yet.

Hope this works
0

#7 User is online   funsella 

  • Advanced Member
  • Group: Platinum Membership
  • Posts: 250
  • Joined: 26-July 11
  • Reputation: 19
  • Gender:Male
  • Location:London
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 24 January 2012 - 03:50 PM

I found a solution which you can hard code into your theme.

function the_excerpt_rereloaded($words = 40, $link_text = 'Continue reading this entry »', $allowed_tags = '', $container = 'p', $smileys = 'no' )
{
	global $post;
        
    if ( $allowed_tags == 'all' ) $allowed_tags = '<a>,<i>,<em>,<b>,<strong>,<ul>,<ol>,<li>,<span>,<blockquote>,<img>';
    
    $text = preg_replace('/\[.*\]/', '', strip_tags($post->post_content, $allowed_tags));

    $text = explode(' ', $text);
    $tot = count($text);
    
    for ( $i=0; $i<$words; $i++ ) : $output .= $text[$i] . ' '; endfor;
    
    if ( $smileys == "yes" ) $output = convert_smilies($output);
 
    ?><p><?php echo force_balance_tags($output) ?><?php if ( $i < $tot ) : ?> ...<?php else : ?></p><?php endif; ?>
    <?php if ( $i < $tot ) : 
        if ( $container == 'p' || $container == 'div' ) : ?></p><?php endif; 
            if ( $container != 'plain' ) : ?><<?php echo $container; ?> class="more"><?php if ( $container == 'div' ) : ?><p><?php endif; endif; ?>
            
    <a href="<?php the_permalink(); ?>" title="<?php echo $link_text; ?>"><?php echo $link_text; ?></a><?php
    
            if ( $container == 'div' ) : ?></p><?php endif; if ( $container != 'plain' ) : ?></<?php echo $container; ?>><?php endif;
        if ( $container == 'plain' || $container == 'span' ) : ?></p><?php endif; 
        endif;
        
}


and in your theme file

<?php the_excerpt_rereloaded('40'); ?>


all the info is here
0

#8 User is online   MikeChipshop 

  • Small but imperfectly formed
  • Group: Moderators
  • Posts: 7,046
  • Joined: 19-April 10
  • Reputation: 503
  • Gender:Male
  • Location:Scotland

Posted 24 January 2012 - 03:52 PM

Cheers mate, both excellent posts. I've started a to-do list on another job now but i'll back to this at some point tomorrow and try these things out.

~Again, many thanks.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users