• Hello

    Is there a simple way to hide everything within a div in the excerpt??

    I regularly use divs to contain images and their captions. In the excerpt the images are ignored but the captions come across which obviously kills the context of the caption (and the flow of the excerpt).

    Cheers!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter itsumishi

    (@itsumishi)

    Anyone have any ideas?

    I have half an idea here. Maybe someone can chime in with the other half.

    An easy way to hide content with CSS is to add “display:none;”. So, if you poke around the CSS structure of WordPress, I’d bet there’s a class or id used within the excerpt you could piggyback with your adjustment.

    -WCC

    The problem is that the excerpt is removing all html tags.
    So the solution is to write a function that looks for these div (better to identify them with a class), and with a hook, removes them from the excerpt before loading.

    Thread Starter itsumishi

    (@itsumishi)

    Hi everyone

    After a lot of fiddling around today I’ve worked out a solution that’s worked for me. It might need a bit of customising if anyone else wants to use it depending on how they format the posts.

    Firstly I’ve opened the wp-includes/formatting.php file and found the following snippet of code.

    * @param string $text The exerpt. If set to empty an excerpt is generated.
     * @return string The excerpt.
     */
    function wp_trim_excerpt($text) {
    	$raw_excerpt = $text;
    	if ( '' == $text ) {
    		$text = get_the_content('');
    
    		$text = strip_shortcodes( $text );
    
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    		$text = strip_tags($text);
    		$excerpt_length = apply_filters('excerpt_length', 55);
    		$words = explode(' ', $text, $excerpt_length + 1);
    		if (count($words) > $excerpt_length) {
    			array_pop($words);
    			array_push($words, '[...]');
    			$text = implode(' ', $words);
    		}
    	}
    	return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
    }

    Apparently you can copy this piece of code and paste it into your theme’s ‘functions.php’ file but I couldn’t get this to work (I think because I have my excerpts on a seperate page that is not directly linked to my wordpress blog and therefore probably skips the themes.

    I ended up changing the code in the original formatting.php file.

    I simply added
    $text = strip_tags($text, '<div>, <ul>, <li>');

    Then on my style sheet I added display:none; to the divs (and the ul and li) items that I wanted hidden. I found this has made my excerpts look a lit tidier.

    I’m sure there is a more elegant and flexible solution but this has served it’s purpose for myself.

    You’ll be very happy to repeat the modification at each upgrade…

    Thread Starter itsumishi

    (@itsumishi)

    As I said.
    I’m sure there is a more elegant and flexible solution but this has served it’s purpose for myself.

    I don’t upgrade as a matter of course anyway. If the page is working and serving it’s purpose why upgrade unnecessarily. Of course if new features come about that I feel are worth upgrading for then I will, I will also add the 21 characters of code back into this page to fix things.

    Just document your mods carefully for yourself so you can re-apply after any WP upgrade that you do.

    Or, convert the mods into a simple plugin, and you probably won’t need to re-add the custom code for each WP upgrade.

    -WCC

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Hiding a Div from an Excerpt’ is closed to new replies.