WordPress.org

Ready to get started?Download WordPress

Forums

Get Better Excerpt
improvement over v 1.02 (6 posts)

  1. michapixel
    Member
    Posted 3 years ago #

    i added some simple additional arguments to satisfy my need of better controlling the output in regards of tags.

    function get_better_excerpt($options='') {
    
    	$ns_options = array(
                        "sentence" => false,
                        "words" => "20",
                        "before"  => "",
                        "after" => "",
    					"show" => false,
    					"skipexcerpt" => true,
    					"link" => false,
    					"trail" => " …",
    					"allowed" => ''
                       );
    
    	$options = explode("&",$options);
    
    	foreach ($options as $option) {
    
    		$parts = explode("=",$option);
    		$options[$parts[0]] = $parts[1];
    
    	}
    	if ($options['sentence']) {$ns_options['sentence'] = $options['sentence'];}
    	if ($options['words']) {$ns_options['words'] = $options['words'];}
    	if ($options['before']) {$ns_options['before'] = $options['before'];}
    	if ($options['after']) {$ns_options['after'] = $options['after'];}
    	if ($options['show']) {$ns_options['show'] = $options['show'];}
    	if ($options['skipexcerpt']) {$ns_options['skipexcerpt'] = $options['skipexcerpt'];}
    	if ($options['link']) {$ns_options['link'] = $options['link'];}
    	if ($options['allowed']) {$ns_options['allowed'] = $options['allowed'];}
    	if (isset($options['trail'])) {$ns_options['trail'] = $options['trail'];}
    
    	global $more;
    	$more = 1;
    	if ($ns_options['skipexcerpt']) {
    		if ($ns_options['sentence']) {
    			$excerpt = explode('.', strip_tags(get_the_content(), $ns_options['allowed']), $ns_options['sentence']+1);
    		} else {
    			$excerpt = explode(' ', strip_tags(get_the_content(), $ns_options['allowed']), $ns_options['words']+1);
    		}
    	} else {
    		if ($ns_options['sentence']) {
    			$excerpt = explode('.', strip_tags(get_the_excerpt(), $ns_options['allowed']), $ns_options['sentence']+1);
    		} else {
    			$excerpt = explode(' ', strip_tags(get_the_excerpt(), $ns_options['allowed']), $ns_options['words']+1);
    		}
    	}
    	array_pop($excerpt);
    	$excerpt = implode(" ",$excerpt);
    	$excerpt = $excerpt.$ns_options['trail'];
    
    	if ($link) {
    		$link = get_permalink();
    		$title = get_the_title();
    		$excerpt = "<a href='$link' title='$title'>".$excerpt."</a>";
    	}
    
    	$excerpt = $ns_options['before'].$excerpt.$ns_options['after'];
    
    	if ($show) {echo $excerpt;} else {return $excerpt;}
    }
  2. michapixel
    Member
    Posted 3 years ago #

    i added "allowed" => '' to the $ns_options.
    and since you already used "strip_tags" i though it would be better to give is some more sense, so i put the $ns_options as 2nd argument in there, eg: $excerpt = explode('.', strip_tags(get_the_content(), $ns_options['allowed']), $ns_options['sentence']+1);

  3. christopherross
    Member
    Plugin Author

    Posted 3 years ago #

    Thanks micha, would you be able to send me the changes via email (http://thisismyurl.com), I'm always keen to add additions to the code.

  4. michapixel
    Member
    Posted 3 years ago #

    gosh, where on earth do you hide your email?

  5. christopherross
    Member
    Plugin Author

    Posted 3 years ago #

    hm ... sorry, it's info@thisismyurl.com

  6. michapixel
    Member
    Posted 3 years ago #

    just sent you an email containing the changed file.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic