WordPress.org

Ready to get started?Download WordPress

Forums

Custom length recent post excerpt in html (3 posts)

  1. whooven
    Member
    Posted 7 years ago #

    Need a way to post a custom-length excerpt from your most recent post on a non-Wordpress HTML page (not PHP)? I did too and after much trial and error came up with a way to do it and wanted to share. The WordPress community has been a big help to me so here's a small contribution back.

    First off, this is semi-automated but there are some manual steps. This isn't a good solution for those who post regularly. It works for me because I don't update this particular blog often. If you update yours often, the better solution is using the php method described in these threads:

    http://wordpress.org/support/topic/20996

    http://wordpress.org/support/topic/25112

    I have a high-traffic index.html page that I wanted to include a small excerpt from a blog on but couldn't find a way to do it without changing the file extension to .php or .shtml, something I couldn't do because of search engine positioning. I also preferred the more manual approach because this particular page isn't dynamically generated and gets a lot of traffic, which equals low server load.

    I've outlined the steps in this post but I've included some extra non-essential detail on this page:

    http://whooven.googlepages.com/doityourselfwebdesign

    That page is not ad-supported or anything, I just didn't want this post to be longer than necessary.
    --------------------------------------
    How to do it:

    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".

    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 (WordPress assigned this when you created the page).

    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:

    http://www.domain.com/wpdirectory/index.php?pagename=mrb
    http://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>("><?php _e('View site') ?> »)</span></h1>
    </div>

    With:

    <div id="wphead">
    <h1><?php echo wptexturize(get_settings(('blogname'))); ?> <span>("><?php _e('View site') ?> »)</span><span>(<?php _e('View MRB') ?> »)</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 (or edit the source and copy the html) 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.

    If someone has a better way, please suggest it!

    Bill

    P.S. I'd like to suggest to the powers that be that some documentation be added to the WordPress docs section about how to include WordPress excerpts in non-WP pages, both php and html pages.

  2. Chris_K
    Member
    Posted 7 years ago #

    Hey Bill -

    a) Thanks for sharing

    b) You're a "powers that be". Head over to codex.wordpress.org (or click Docs at the top of the page) register for the wiki and go nuts!

  3. whooven
    Member
    Posted 7 years ago #

    Thanks. I'll have to schedule some time to do just that.

Topic Closed

This topic has been closed to new replies.

About this Topic