• Home
  • About Mitch
  • Speaking
  • Articles
  • Contact
  • Home
  • About Mitch
  • Speaking
  • Articles
  • Contact

Digital Strategist

WordPress Developer

Content Creator

Unapologetic Punk

Mitch Canter

  • X
  • Bluesky
  • GitHub
  • Twitch
  • YouTube
  • LinkedIn
Tutorial

50 Days to a Better Blog–Day 9: Resurrecting Old Content

CMDR Mitchcraft

Reading time: 2 minutes

This post is the ninth of an ongoing series entitled “50 Days to a Better WordPress Blog”.  During this time, Mitch will be providing small snippits of code, plugins, and things you can do to make your blog more attractive, attain new readers, and keep old ones coming back time and time again. You can see the entire series here.

I've always wanted to use this photo too - and it's sort of relevant, right? :)
I've always wanted to use this photo too – and it's sort of relevant, right? 🙂

Let’s face it – you’ve been blogging for a long time now.  You’ve got lots and lots of great content, but some of it can get buried in the shuffle.  You tend to have 3-5 posts that get a lot of content, but the rest are just sort of… there.  Freeloading and drinking your Google Juice.  Let’s put them to work!

Displaying Random Content

A simple function can call 5 posts from your entire database and display them.  Typically you’d only want the title, but if you want to add more information then it’s just a simple loop – add to it what you wish (and tweak this code to suit your needs!)

Drop this in your sidebar:

<li class=”widget widget_oldposts”>
<h4 class=”widgettitle”>Old Posts</h4>
<ul>
<?php $recentPosts = new WP_Query(); $recentPosts->query(‘showposts=5&orderby=rand’); ?> <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<li><a href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
</li>

The special part is the “orderby” in the query – that allows us to literally pull 5 random articles.

Displaying Random Relevant Content

Random is great, but not necessary relevant.  Let’s specify even further by pulling 5 random posts form the category you’re currently viewing.  Best used as “related articles” under a post, by the way.

<?php $category = get_the_category();  $relcat = $category[0]->cat_ID; ?>
<h4>Older Posts</h4>
<ul>
<?php $recentPosts = new WP_Query(); $recentPosts->query     (‘showposts=5&orderby=rand&cat=’ . $relcat); ?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<li><a href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>

The variable calls the global post variable’s category ID, and then displays that as a query variable in the new loop we create.  This allows us to pull content based on the main category a post is inserted in (it will pull in only one category, so multi-category posts will only related to the first category (main category) chosen.

  • Thursday Conversations: HTML5

    Thursday Conversations: HTML5

    Reading time: 2 minutes

    One of the best new features of 3.1 – this brings Internal Linking to the forefront, allowing you to quickly link to old posts, pages, or other content types. Admin Bar Also super important – this adds a bar to any logged in user that has commonly used functions or actions within easy reach –…

    WordPress
  • Better Know a WordPress Tag: ‘siteurl’

    Better Know a WordPress Tag: ‘siteurl’

    Reading time: 1 minute

    When you’re working on a development site it’s hard to set things up correctly because you know you’re going to change the site, and putting in elements that are more than likely “stationary”, such as links, will have to be changed, and that causes un-necessary headache when it comes time to move the site live. …

    WordPress