Shiba

Adventures in WordPress

  • Home
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress Design / WordPress Excerpt – How to Style It

WordPress Excerpt – How to Style It

by ShibaShake 36 Comments

WordPress Excerpt - How to Style It

WordPress excerpts are most useful when you need to display a series of posts in a single page. This often occurs in your –

  1. WordPress blog homepage – your most recent posts.
  2. Category page – All posts belonging to the selected category.
  3. Tag page – All the posts containing the selected tag.
  4. Archive page – All posts published during the selected date.
  5. Search results page – All posts and pages matching the search query.

You can set the maximum number of posts that are shown in these pages by going to Settings >> Reading and changing the number on the Blog pages show at most option.

When multiple posts are shown, it is easier for your readers to find what they want by only giving them a summary of each post rather than showing them the entire post. You can enable this post summary functionality by using the WordPress the_excerpt command.

Here is an example blog with styled WordPress excerpts.

Example blog with styled WordPress excerpts.

1. Enable WordPress Excerpts

This is something that may already be supported in your WordPress theme, but if not, you can enable WordPress excerpts by doing the following –

Go into the index.php file of your theme. Find the line that contains the WordPress command

the_content

.

For example, in the ‘default’ theme you have the lines below in the theme index.php file –

<div class="entry">
	<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

Replace the line

<?php the_content('Read the rest of this entry &raquo;'); ?>

with the code below –

<?php 
    if (is_single() || is_page()) 
         the_content('Read the rest of this entry &raquo;');
    else {
         the_excerpt();
    }
?>

This will style your blog so that it only shows text summaries when there are multiple posts, but shows the entire document for single posts or single pages.

You can further change the length of your post summaries by following these WordPress codex instructions.

2. Add Background Images to Your WordPress Excerpt

You can pretty up your WordPress excerpts by adding in background images to the top, bottom, or side of each excerpt. This is done by adding a background image entry to certain standard WordPress classes.

The exact class names you need to use will be greatly dependent on your current WordPress theme. However, there are certain standards that are adhered to by most themes.

In particular, each post is usually encapsulated in a post class. If that post is an excerpt, it will further be encapsulated within an excerptcontent class.

.excerptcontent {
	background: url("side_graphic_URL.jpg") top center repeat-y; border: none; 
}	

.post {
	background: url("top_graphic_URL.jpg") 90% 0% no-repeat; border: none; 
}

Above, we set a side border graphic and a top background graphic for our excerpts. Just add the above styles into the style.css file of your current WordPress theme.

3. Add Thumbnail Images to Your WordPress Excerpt

To add thumbnail images to our WordPress excerpts, we need a function that extracts the first image from our posts. The get_first_image function below was obtained from the WordPress.org support forum.

function get_first_image() {
	global $post;
	$first_img = '';
	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
	$first_img = $matches [1] [0];

	return $first_img;
}

Just add this function to your theme functions.php file.

Next we need to call this function from our WordPress theme. Add the code below, right above the_excerpt command, in your theme index.php file.

?>
        <div class="alignright" style="margin:20px;">
             <a href="<?php the_permalink() ?>">
             <img src="<?php echo get_first_image();?>" width="200" height="200"/>
             </a>
       </div>
<?php

This adds a right-aligned image to each post excerpt, which we size at 200px by 200px. The code in your index.php file will now look like this –

<div class="entry">
<?php 
    if (is_single() || is_page()) 
         the_content('Read the rest of this entry &raquo;');
    else {
?>
        <div class="alignright" style="margin:20px;">
             <a href="<?php the_permalink() ?>">
             <img src="<?php echo get_first_image();?>" width="200" height="200"/>
             </a>
       </div>
<?php

         the_excerpt();
    }
?>
</div>

4. Resize the Images in Your WordPress Excerpt

However, the results are still not exactly what we want because the aspect ratio of the images are not maintained. This may make some images look stretched or compressed.

Here is a simple function to resize the image and maintain its aspect ratio. Add the code to your theme functions.php file. –

function url_exists($url) {
	$headers = wp_get_http_headers($url);
	
	if (!is_array($headers)) :
		return FALSE;
	elseif (isset($headers["content-type"]) && (strpos($headers["content-type"],"image") !== false)) :
		return TRUE;
	else :
		return FALSE;
	endif;
}

function resize_image($image, $alt, $newwidth, $newheight) {
	if (!file_exists($image) && !url_exists($image)) return '';
	list($width, $height, $type, $attr) = getimagesize($image);
	if (!$width || !$height) return '';

	if ($newwidth) 
		$newheight = intval($newwidth/$width * $height);
	else
		$newwidth = intval($newheight/$height * $width);
	return '<img src="' . $image . '" width=' . $newwidth . ' height='. $newheight . ' alt=' . $alt . '/>';
}

  • $image is the image file or url.
  • $alt is the alternate text to use.
  • $newWidth and $newHeight are the new width or height you want to resize the image to. Only one of these parameters will be used to resize the image. If $newWidth = 0 then $newHeight will be used to resize the image. Otherwise, $newWidth will be used.

The resize image function will return the HTML string for the input image, at the proper size. If the image cannot be found, a NULL string is returned.

Finally you just need to call this resize_image function from your theme. Replace the previous call to get_first_image in your index.php file with the code below.

?>
        <div class="alignright" style="margin:20px;">
           <a href="<?php the_permalink() ?>">
              <?php echo resize_image(get_first_image(), get_the_title(), 200, 0); ?>
           </a>
        </div>
<?php

Here is what the final index.php file looks like, instead of the original the_content command.

<?php 
    if (is_single() || is_page()) 
         the_content('Read the rest of this entry &raquo;');
    else {
		?>
        <div class="alignright" style="margin:20px;">
           <a href="<?php the_permalink() ?>">
              <?php echo resize_image(get_first_image(), get_the_title(), 200, 0); ?>
           </a>
        </div>
		<?php
         the_excerpt(); 
    }
?>		

WordPress Excerpt - How to Style It

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comments

« Previous 1 2 3
  1. James says

    July 2, 2018 at 11:03 am

    Great post. I really need some help. I have set border lines around my excerpts on my blog page/post page. The problem is my excerpts are touching the borderline on the left and the right. Could you please help me with some codes to set left and right paddings around my excerpt?

    Reply
  2. catherine says

    September 1, 2014 at 4:50 pm

    Thank you – excellent tutorial

    Reply
  3. Carlos Coelho says

    August 29, 2014 at 9:38 am

    Hi Shiba! First, congrats for your post, it’s very good and detailed.

    So, I have a problem with excerpt function and I’m newbie related to HTML/CSS/PHP/etc.

    I have excerpt in the front page featured pages (img1) and in which page I begin with a quote (img3) which I don’t want to appear in the front page. How do I remove it? Using a filter? Which? How?

    If you could help me I would be very thankful. I’m building this site as a gift for my parent who has an architecture company but doesn’t have money to pay for one web designer. It’s kinda surprise and I want the site to be good, pleasant and comfortable.

    If you want I can turn the website visible, just contact me by email!

    (img1 link) -> http://prntscr.com/4hopcw
    (img2 link) -> http://prntscr.com/4hoqlk

    Thanks!
    Carlos Coelho

    Reply
  4. CJ says

    May 25, 2014 at 11:33 am

    Cool tutorial. This helped me!

    Reply
« Previous 1 2 3

Recent Posts

  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text
  • Screenshot of the Success, WordPress has been installed page.Migrating Your WordPress Website to Amazon EC2 (AWS)
  • Screenshot of WinSCP for creating a SFTP configuration.How to Set-Up SFTP on Amazon EC2 (AWS)
  • WordPress Gutenberg code view screenshot of this article.How to Prevent Gutenberg Autop from Messing Up Your Code, Shortcodes, and Scripts
  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS)

Recent Comments

  • Create Pop-up Windows in Your WordPress Blog with Thickbox (57)
    • Jim Camomile
      - I have used this tutorial several times and it is one of the few simple and effective ways to add popups with dynamic content, ...
  • How to Add Admin Columns in WordPress (7)
    • Andy Globe
      - Hi Friends, I am facing two problem in WordPress admin1. Load custom CSS for wp-admin on user-role base (editor) 2. ...
  • Example blog front-page using excerpts and the Shiba Theme.Optimize Your WordPress Plugins with Tags (5)
    • DF
      - thanks, i went the other way and added a filter if pages I wanted.
  • WordPress Search Widget – How to Style It (57)
    • Nelson
      - Tanks master - Fine
  • Update custom inputs with the proper data using Javascript.Expand the WordPress Quick Edit Menu (59)
    • Mike G
      - This is exactly what is happening to me. It is updating the value in the database and in the column, but I have to refresh ...

Copyright © 2022 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·