Do It Yourself Web Design

Some helpful DIY how-to tips relating to web design/designing your own website. 

Home »  Do It Yourself » DIY Web Design

How to output a raw, custom-length recent post excerpt in Wordpress

Wordpress is a great program for maintaining a blog (weblog), diary or journal. But sometimes I need it to do a little more. One of my websites has a blog that's updated between two and ten times a month. The website isn't entirely a blog so I installed Wordpress 2.02 to run in a specific directory so it wouldn't control the index page for the entire website. On the index page, I placed links to the blog but I also wanted to embed an excerpt from the most recent blog post to further entice people to visit the blog. 

I've spent probably 10 or more hours and have come up with a decent solution, though by no means the best solution. There is no easy way to do this and even though I recently found a nifty way to force Wordpress to create a very basic page showing an excerpt of my most recent post with as many words as I want, I was unable to find a way to automatically include this output into my non-wp home page. For the rest of this note I will refer to my customized most recent bare post page as "MRB".

Here's what I tried:

1) a simple html include of the Wordpress location for MRB (for me it was /wpdirectory/mrb or alternately /wpdirectory/index.php?pagename=mrb). This looked like it was almost going to work but in the end only ended up including a Wordpress 404 page. BTW, if you want to try this in yourself and have problems, remember the page you're including something into needs to end in .shtml and you have to include from a relative path and not an absolute web address such as:

<!--#include virtual="/wpdirectory/index.php?pagename=mrb" -->

2) I could have tried a php include but my main home page was very well indexed in the search engines as index.html so I wasn't about to change it to a .php page. Incidentally I also wasn't about to change the index.html page to index.shtml either.

3) I tried to create a script that would output my static page with the Wordpress post text embedded but my skills aren't quite there yet.

So cutting to the chase, here's my workaround:

Summary: I use two Wordpress plugins to create my MRB page: the-excerpt-reloaded and get-a-post. Since these were designed to work within the loop, I created a page template (I copied my theme page template, named it mrb.php, added the code from the two plugins, stripped all unnecessary stuff from the template (like the header and sidebar) so the output is very basic, or "bare" as I've called it. I then created a new page in WP based on the mbr.php template. I called the page "mbr" and didn't add anything in the content area. Once done, I noted the number of the page. We'll use that in a minute. Next I edited the sidebar theme file where the page code was to exclude the page from showing to visitors. Next on the edit list was the admin-header.php file in the wp-admin directory. I edited that to show "View MRB" on the admin screens just like the "View Site" link. Now all I have to do is click the "View MBR" link at the top after I've posted something, copy the text shown, paste it into my static html page and I'm done. It's a few extra steps I wish I could do without but there just doesn't seem to be a better way at the moment. Not sexy, but it works pretty well.

Details (works on Wordpress 2.02, not sure about other versions):

1) Upload and activate the the-excerpt-reloaded and get-a-post plugins.

2) Make a copy of your page.php file in your theme directory (it might be called something else in your theme but it's usually /wp-content/themes/yourtheme/page.php). Call it anything you want but for this document we'll call it mrb.php. Make sure something like this is at the very top of your mbr.php file:

<?php
/*
Template Name: Most Recent Bare
*/
?>

You can call it anything you want, but I called mine "Most Recent Bare". Since this page is designed to be bare (since you're just going to copy text from it), remove the header, sidebar and footer code. Add the following to before the loop code:

<?php get_a_post(GETPOST); ?>
<?php the_excerpt_reloaded(50, '', 'none', TRUE, 'Keep reading >>', FALSE, 2); ?>

See the plugin help pages for the parameters on those but in the above example, the get-a-post plugin is going to pull up the most recent post (this is the default when we use the GETPOST option in version R1.2. For the-excerpt_reloaded plugin code, in this example we're asking for the first 50 pages and we want it to display "Keep reading >>" at the end with a link to the full post.

When I was done creating my custom MRB page template this is what it looked like:

<?php
/*
Template Name: Most Recent Bare
*/
?>

<?php get_a_post(GETPOST); ?>
<?php the_excerpt_reloaded(50, '', 'none', TRUE, 'Keep reading >>', FALSE, 2); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); /* start the loop */ ?>
    <div class="entry">
        <div class="entrybody" id="post-<?php the_ID(); ?>">
            <?php the_content(); ?>

        </div>     
    </div>
       

    <?php endwhile; ?>
   
<?php endif; ?>

</div>
</div><!-- The main content column ends  -->

Next, create a page from within your WP admin area. Use the title "mrb" and don't put anything else on the page unless you want something else there. Use "Most Recent Bare" as the page template. Save it. On the manage pages area, note the page number. Mine was 14. We'll use that soon.

Now I didn't want this bare page showing up for visitors since they wouldn't understand or care why it was there. So I wanted to make sure this page was hidden from visits by making it not show on the sidebar. To do that, go to the sidebar theme file in the theme editor (or edit it locally and upload) and make this change:

Replace:

        <?php wp_list_pages('title_li=<h2>' . __('pages') . '</h2>' ); ?>       

With:

                <?php wp_list_pages('exclude=14&title_li=<h2>' . __('pages') . '</h2>' ); ?>

Change "exclude=14" to whatever number page your MRB is.

Next I wanted an easy way to get to the MBR page every time I needed to copy it. You can manually enter either of the following:

www.domain.com/wpdirectory/index.php?pagename=mrb
www.domain.com/wpdirectory/mrb

A neat way to have that link handy in WP is to edit the admin-header.php file in the wp-admin directory. Make the following change.

Replace:

<div id="wphead">
<h1><?php echo wptexturize(get_settings(('blogname'))); ?> <span>(<a href="<?php echo get_settings('home') . '/'; ?>"><?php _e('View site') ?> &raquo;</a>)</span></h1>
</div>

With:

<div id="wphead">
<h1><?php echo wptexturize(get_settings(('blogname'))); ?> <span>(<a href="<?php echo get_settings('home') . '/'; ?>"><?php _e('View site') ?> &raquo;</a>)</span><span>(<a href="/wpdirectory/mrb"><?php _e('View MRB') ?> &raquo;</a>)</span></h1>
</div>

There are sexier ways but this works for me. Save and upload that file.

Now all you have to do is post something, click the MRB link in the admin section, copy the text and paste it whereever you want. I know it's still very manual but I don't post often so it's not that big a deal.

-Bill Hooven


Questions/problems:

work in progress...