Building a Custom WordPress Module

Web Dev Add comments

I’m sure you’ve seen the front page of this site ( click here if not ) so you’ve seen how we display images along with our posts. This theme was originally designed by Darren Hoyt so firstly I’d like to recognise his efforts and show you how a stripped down version can be created.

Let’s say we want to build the same module that displays the latest post from our ‘General’ category.

First things first we need to create the PHP that will call for the information we need:

  1.  

These two lines call query the database for the latest post in category 1 (in our case that’s General). The second line starts the loop that will run until all data asked for have been displayed.

This line ends the loop:

  1.  

We could build in a small error message, but it won’t be needed in this case as we can be sure we’ll always have a post in this category.

Building the Module

The PHP calls you’ll need are below along with a brief explanation;

  1. <a href="http://www.designprophet.co.uk/wp-admin/%3C?php%20the_permalink%28%29%20?%3E" rel="bookmark" title="Permanent Link to &lt;?php the_title(); ?&gt;"><img src="http://www.designprophet.co.uk/wp-admin/%3C?php%20echo%20get_option%28%27home%27%29;%20?%3E/wp-content/themes/your-theme/images/%3C?php%3Cbr%20/%3E$values%20=%20get_post_custom_values%28" />" alt="" id="leadpic" /&gt;</a>

This chunk of code calls for the story image to be displayed next to the text and makes it a link to the post. For now all you need to know is the path to the image needs to be changed to the location you’ll be using.

Calling the Post Title

  1. <a href="http://www.designprophet.co.uk/wp-admin/%3C?php%20the_permalink%28%29%20?%3E" rel="bookmark" title="Permanent Link to &lt;?php the_title(); ?&gt;"></a>

This line creates a link to the post and uses post title as the text;

  1.  

Display the Excerpt (either automatic or one defined while posting)

  1.  

Finally a ‘Read More’ link to take the visitor to the story

  1. <a href="http://www.designprophet.co.uk/wp-admin/%3C?php%20the_permalink%28%29%20?%3E" rel="bookmark" title="Permanent Link to &lt;?php the_title(); ?&gt;">Read More</a>

This is the full code with a small bit of CSS to get you started

  1. <p id="”module”">//Display Image
  2.  
  3. <a href="http://www.designprophet.co.uk/wp-admin/%3C?php%20the_permalink%28%29%20?%3E" rel="bookmark" title="Permanent Link to &lt;?php the_title(); ?&gt;"><img src="http://www.designprophet.co.uk/wp-admin/%3C?php%20echo%20get_option%28%27home%27%29;%20?%3E/wp-content/themes/your-theme/images/%3C?php%20$values%20=%20get_post_custom_values%28" />" alt="" id="leadpic" /&gt;</a>
  4.  
  5. //Post Title
  6.  
  7. <a href="http://www.designprophet.co.uk/wp-admin/%3C?php%20the_permalink%28%29%20?%3E" rel="bookmark" title="Permanent Link to &lt;?php the_title(); ?&gt;"></a>
  8.  
  9. //The Excerpt
  10.  
  11. // Read More Link
  12.  
  13. <a href="http://www.designprophet.co.uk/wp-admin/%3C?php%20the_permalink%28%29%20?%3E" rel="bookmark" title="Permanent Link to &lt;?php the_title(); ?&gt;">Read More</a>
  14.  
  15. //End the Loop
  16.  
  17. #module {
  18. height: 300px;
  19. width: 500px;
  20. background: #fff;
  21. border: 1px solid #ccc;
  22. }

Getting it to Work

When we called the story image we included the following code:

  1. ‘Image‘ is the Key name we need to give to a new custom field. This can be found below the post editor when making a new post.
  2.  
  3. The value should be the filename of the image you wish to use i.e. Myholiday.jpg
  4.  
  5. Hopefully you’ve remembered to upload the image to the folder we specified earlier!
  6.  
  7. Now all you need to do is post!
  8.  
  9. Recap
  10.  
  11. Change the image path to the folder you plan to use
  12.  
  13. Find the ID of the category you want to use and include it here:
  14.  
  15. [code lang="php"]

Create a custom field called Images and specify your image filename in ‘Value’

Post and Enjoy!

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Technorati
  • del.icio.us
  • Reddit
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • YahooMyWeb
  • Wists
  • blinkbits
  • StumbleUpon
Trackback This Post

2 Responses to “Building a Custom WordPress Module”

  1. Tom Collinson Says:

    If you’d like to modify this slightly to display the latest post from a handful of categories change this line:

    < ?php query_posts(’showposts=1&cat=1′); ?>

    To this:

    < ?php query_posts(’showposts=1&cat=1, #, #′); ?>

    Where # is your category ID.

    This will determine if, say, your general category has been posted to last and display that instead of the latest in the other categories.

  2. Mark Says:

    Hi Tom, this is something I’ve wanted to implement for a very long time. I want to try this out soon. Could you please let me know how I can also apply it to search results - so that thumbnails display for each page within the results (and the same behavior for when someone clicks a tag, and sees the results page)?

    Thanks!

Leave a Reply

Entries RSS Comments RSS Login