php

...now browsing by tag

 
 

Showing Latest Post on Static (non-WordPress) Page

Sunday, October 18th, 2009

I’ve got a client who has a very nice website which was built quite a while back.  Since launching, they wanted to add a blog and a few other things to their site, for which we used WordPress.  Also along the way, they wanted to show their most recent post on their home page, which is outside of “the loop” for WordPress.

Now I’ve never claimed to be a WP pro, but I have figured out how to make this work.  Below is the code to get it done.  The trick is the first line, the required wp-load file.  That’s what lets you use some of the wp functions outside of the loop.

<?php require( 'path_to_blog_root/wp-load.php' );
$getpostq=mysql_query('SELECT ID,post_title FROM wp_posts WHERE post_status="publish" ORDER BY post_date DESC LIMIT 1');
if (!$getpostq) { die('Error selecting info: '.mysql_error()); }
while ($getpostr=mysql_fetch_array($getpostq)) {
$post_title=$getpostr['post_title'];
$pid= $getpostr['ID'];
$postlink=get_permalink($pid);
echo "Recent Discussion: <a href=\"$postlink\" title=\"Link to $post_title\">$post_title</a>";

If you also want the post date, you can add these lines:

<?php require( 'path_to_blog_root/wp-load.php' );
$getpostq=mysql_query('SELECT ID,post_title FROM wp_posts WHERE post_status="publish" ORDER BY post_date DESC LIMIT 1');
if (!$getpostq) { die('Error selecting info: '.mysql_error()); }
while ($getpostr=mysql_fetch_array($getpostq)) {
$post_date=$getpostr['post_date'];
$post_date=date('F j, Y', strtotime($post_date));
$post_title=$getpostr['post_title'];
$pid= $getpostr['ID'];
$postlink=get_permalink($pid);
echo "Recent Discussion: <a href=\"$postlink\" title=\"Link to $post_title\">$post_title</a>%lt;i>($post_date)</i>"; ?>

By the way, if you have a better way to do this, I’d love to see it.

Is Your 404 Page Found?

Friday, June 6th, 2008

Every designer screws up. Somewhere in that ever-growing website of yours is a link that goes to a page that doesn’t exist. Perhaps the designer fat-fingered it. Maybe he made a link with the intention of making the new page. Whatever it is, its fubar’d now.

So in the very likely event that a customer clicks on that link, and gets the generic 404 page from their browser, its your designer (or you, if you’re the designer) who gets the credit for losing that customer. Bad bad.

I’m sure you’re not interested in losing customers, not even one. So make sure you have a 404 page that’s not only there, but one that’s helpful too. Here are some tips to make the page better.

  • Match the layout with your current site
    Make the page look like any other page on your website, but with a nice little message that tells the user that they’ve encountered a page that doesn’t exist. Match the headings, the text size, everything. Basically we’re going for a page that looks nothing like the big bad error that it is.
  • Notify the User of the Error
    This would be a good time to explain that someone screwed up (you may want to put it a little kinder than that). Assure the visitor that this isn’t a common happening. You could even go a little further and tell the user that someone has already been notified about the bad link (which your designer/SEO will see in any server log/analytics report or on Google Webmaster Tools) (also see this script for emailing the webmaster important info)
  • Give the User Some Options
    Don’t just tell the user the page is gone, give them some fodder to help them figure out what you screwed up.

    • If you’re a good coder, you can perhaps take apart the bad URL and suck some keywords out of it, do a search for related pages and list those for the visitor
    • Give them a nudge to use the search box (which you’ve included on the 404 page)
    • Provide them with a nice sitemap that gives them plenty of options to find what they were looking for in the first place (see Apple’s 404 for a great example)
  • Put Your Contact Info on the Page
    Good lord, do I have to tell you this? Assuming its not already in your header, give them a way to contact you. This may be a good thing, as you’ll find out immediately about the broken link, and you’ll also have a warm lead that you can coddle and caress to your hearts delight.