Posts Tagged ‘wordpress’

Make a category not considered as a post in WordPress

Thursday, September 24th, 2009

In other words, how do you make posts that are in a certain category not count towards total page post count in WordPress?

A while back I set up Asides on this blog. The problem was that previously I was displaying 5 posts per page. Now with asides it still displayed 5 posts per page, but as asides are probably one sentence long at most I personally don’t consider them to be blog posts. This meant that it didn’t display 5 “real” posts per page. So, how do I fix this?

Disclaimer: I’m not experienced in the under-the-hood of WordPress and as a result some of this solution might be hackery. However it works for me, and that’s what counts.

Problem 1: displaying 5 real posts per page regardless of how many asides there are.

WordPress loops through a series of posts per page and displays them one by one. Initially I thought they would increment a counter, of which I could easily change so that if the post category was in “asides”, it will not increment the counter. However there were a couple flaws: 1) There was no counter, or I completely missed one, 2) The database queries sets the LIMITs from the administrator settings right at the very beginning, and 3) pagination will be completely messed up.

The solution was pretty simple, firstly we set the database query LIMIT to an obscenely large amount – more posts than we ever think we’ll need on a page. This can be done in the administrator panel. Change “display posts per page” to a random large value. I chose 15 because it seems pretty realistic that real posts + aside posts < 15 for 99.9% of the time.

The second step is to manually change the criteria for when the loop terminates. This way it will not actually show 15 posts, but instead up to 15 posts. What we’ll do is create a new counter, where ever time we display a post that isn’t an “aside”, we increment the counter, until it hits 5 posts (if I wanted 5 real posts per page) – at which time it’ll terminate the loop.

This can be done in the wp-includes/query.php page. To begin with we’ll need a new variable in the class for our counter. So below class WP_Query { we should add:

var $counting_up = 0;

Just to make sure that $counting_up resets itself as it should, we’ll add this to the init() function:

$this->counting_up = 0;

Now the next step is to modify the the_post() function. When the loop has started and the category is not an aside, we’ll increment our counter. In this example my aside category ID is category 429. This will be different for you, so you change it. So simply add this to the the_post() function:

if ( !in_category(429) && $this->current_post != -1 ) {
 $this->counting_up++;
 }

Now we’ve got our counter, we’ll set up the loop to terminate correctly. This can be done in the have_posts() function. Notice this is the have_posts() function inside the WP_Query class, not outside. We can modify our if statement to terminate when our counter hits 4 posts (as the first isn’t counted – therefore in effect we’ll display 5 real posts), and also when we don’t have a do_not_terminate variable set to the WP_Query. Why this do_not_terminate variable is important is if we ever need to override this, as well as later I’ll explain when we look at pagination issues. Here is my completed modified if statement:

if ($this->counting_up == 4 && !$this->query_vars['do_not_terminate']) {
 $this->in_the_loop = false;
 do_action_ref_array('loop_end', array(&$this));
 $this->rewind_posts();
 return false;
 } elseif ($this->current_post + 1 < $this->post_count) {
 return true;
 } elseif ($this->current_post + 1 == $this->post_count && $this->post_count > 0) {
 do_action_ref_array('loop_end', array(&$this));
 $this->rewind_posts();
 }

Now we’ve solved problem 1, and 5 real posts are displaying on our front page, let’s move on to problem 2.

Problem 2: previous page, or going to older posts will no longer work.

Since pages are pretty obsolete at this point, we’ll switch to using offsets. This is because each page will no longer display a fixed number of posts, each may display a variable amount of posts, minimum being 5 (that we set just now), and maximum being 15 (that we set at the very beginning). So to start we’ll hop over to our index.php in our theme file, and simply get the offset from the URL and pass it through to our post loop. Here goes:

<?php if ($_GET['offset'] && is_numeric($_GET['offset'])) {
 query_posts('offset='. $_GET['offset']); $offsetting = $_GET['offset'];
 } else { $offsetting = 0; } ?>

So with that code in index.php, we can now visit myblogsite.com/?offset=20 and offset our posts by 20. To determine how many posts to offset by in previous pages, we simply take how much we’re currently offset by, and add all the posts we’ve displayed on the page, regardless of whether or not it is an aside or a real post. To do this we need another counter. So we’ll initialise our counter, perhaps near the beginning of index.php:

<?php $on_page = 0; ?>

… then within our while (have_posts()) { loop, (or whatever equivalent loop your theme uses), we’ll just increment it:

<?php $on_page++; ?>

So then we recode our “previous posts” link to go to:

<a href="http://yourblog.com/?offset=<?php echo $offsetting + $on_page; ?>" >Previous posts</a>

That was simple, eh? This brings us to problem 3.

Problem 3: newer posts don’t work, for obvious reasons.

Going forward in time is a little bit more complex. We want to calculate how much less we should offset by. To do this we’ll create a function to calculate this. The function will need to know how much we’re currently offset by. Based on that, it’ll query 15 posts into the future, then loop through those posts in reverse order. If it can’t go 15 posts into the future (eg: on the first page, and perhaps the second), it’ll go as far into the future as it can. When looping through, it’ll record the category of each of the posts. Whenever it hits a post, it’ll increment the count we want to offset less by. When we hit a post that category isn’t an aside (category 429 in my example), it’ll increment a counter that determines how many real posts we’ve hit so far. So therefore we have two counters. When the real counter hits 6 posts, it’ll terminate the offset counter. This is because I want 5 real posts per page, and based on how we coded problem 1, we know that the last post of any page must be a real post, not an aside.

We can place this function in the functions.php file of our theme. Here is the function, of which lazy people can copy and paste:

function back_to_the_future($offset = 0) {
 $new_offset = $offset-15;
 if ($new_offset < 0) {
 $new_offset = 0;
 }
 $diff_offset = $offset - $new_offset;
 $future_query = new WP_Query(array(
 'showposts' => $diff_offset,
 'order' => 'DESC',
 'offset' => $new_offset,
 'do_not_terminate' => TRUE
 ));

 $post_data = array();

 while ($future_query->have_posts()) {
 if ($diff_offset == 0) {
 break;
 } else {
 $diff_offset--;
 }
 $future_query->the_post();
 $cat_id = get_the_category();
 $cat_id = $cat_id[0]->cat_ID;
 $post_data[] = $cat_id;
 }

 $post_data = array_reverse($post_data);
 $count_posts = 0;
 $count_total = 0;

 foreach ($post_data as $post_cat) {
 if ($post_cat != 429) {
 $count_posts++;
 }
 if ($count_posts == 6) {
 break;
 } else {
 $count_total++;
 }
 }

 return $offset - $count_total;
}

People who looked through the code will realise that we passed the do_not_terminate variable to WP_Query that we set up when addressing Problem 1. This is required because if we didn’t, we won’t get 15 posts into the future, instead we’ll just get however many posts starting from 15 posts into the future that include 5 real posts – which is totally useless.

To finish off nicely we’ll edit our “newer posts” link to use this calculated offset in our index.php file, but only display when we have a proper offset to show and we’re not on the first page.

<?php if ($future_offset != 0 || !empty($offsetting)) { ?>
 <a href="http://yourblog.com/?offset=<?php echo $future_offset; ?>">Newer Posts</a>
 <?php } ?>

Tada – all done! I hope that helped somebody out there, but if not at least I have it for archival purposes. If you’re using it, let me know how it goes!

Implemented a new “Asides” feature.

Sunday, September 20th, 2009

As you might’ve guessed, the thinkMoult blog is not the fanciest WordPress implementation in the neighbourhood. Heck, our sidebar disappeared a while back. The site currently uses three plugins: a related posts feature, one that allows you to subscribe to comments, and finally good ol’ Akismet which likes to delete any comments that mention the word viagra.

WIPUP is gaining momentum and will replace some of my blog posts as the main way I release updates on projects, and as I’ve quit Twitter I have a small hole left inside me for short updates that happen once in a while. Astute readers would’ve noticed just below this post there is a post … without a post! Yes, it’s a tiny update and I shall use it for better means in the future than greeting mother Earth.

For those living under rocks, it’s basically a short sentence update that will not necessarily follow the post-every-2-days schedule I try to stick to. The update can also optionally include a link to another website.

For those curious on how I did this hackery, I followed the instructions from this guide. Well, not fully – I made some modifications and I couldn’t be bothered to style it so I just made it a header. It’s semantically incorrect – so sue me.

Now officially vulnerable to muscle aches! Oh, and WordPress 2.7.

Monday, December 15th, 2008

Oh joy! I get an excuse to talk about something else other than tech for a while. For the past few days, my skin seems to have been plagued with what resembles a nasty case of chicken pox, except that it’s restricted to certain regions, such as my hands (like I’m wearing weird lumpy gloves), my back, my feet, and occasionally popping up on my thighs. In addition to that, my lips have managed to achieve a stunning level of dryness and chap(piness?), as well as maintain a constant bleeding throughout the day. Seriously, how do you do that in a tropical country where skyjuice is plentiful? I snapped a picture of it whilst on the way to a charity fun fair this morning, of which I won’t mention details about. (oh, the picture doesn’t do its bark-like quality with oozing blood any justice. In fact, it makes it look redder than it really is – should be brown. Shame.)

It’s all a bit nasty and I wasn’t quite sure what caused it until a visit to the clinic this morning. Because it’s not exactly the smartest thing to announce “hey guys, here are my medical records, look how cool they are!” online, the most I can say is that I pretty much don’t like any sort of painkiller that exists. So any future muscle aches that occur … well, I’ll just have to sit it out and endure. Funnily enough, I actually had a dream about a week ago that I would get this sort of allergic reaction. It seems to have come pretty realistically true. Right down to the minute details of which figure would be the most swollen with lumps. On other news, I will be flying off to the U.K. on Thursday, but don’t expect that to curb the flow of creative blog posts (this one exempted), as I’ve got quite a bit to show.

On other news, I’ve just upgraded to WordPress 2.7. However, this isn’t just any upgrade, it’s an awesome upgrade. That’s because I now get a cooler backend for managing thinkMoult. The whole design looks all beefed and faster to access stuff. Take a look if you don’t believe me:

2008-12-15-094314_1280x800_scrot

Along with the upgrade, the flash upload now works again for me (no more tedious click, upload, reload, click), as well as new thumbnail sizes for adding images … there are probably a lot of new features I haven’t noticed yet, but all the same, I’m excited. It’s a new toy, it’s a new thang, it’s awesome.

Rethinking my post, it probably wasn’t a good idea to mix sickness and software together.

WordPress 2.6.5

Monday, December 1st, 2008

Well, it seems as though version 2.6.4 has – well, quite literally been scrapped and the developers have skipped right on to 2.6.5. I’m up to date, and again using it as an excuse to take up a post on my writing schedule. For want of something more interesting to talk about, apparently Mr. Stallman himself (I mean, nobody would ever do identity theft online, right?) has heard about my public accusation of his singing abilities being the cause of the slow progress on open-source, and decided that I’m correct. You can see proof of this on the post “Cause of the Open-Source Lag” itself, as well as grab a chance to listen to *choke* … a wonderful song.

Oh, and to make this post have some sort of interesting content, here’s a scrot of my new computer’s desktop, running KDE (4 series), all freshly personalised (still unfinished though). Again, click on it to get the full resolution image (Yes! No longer 1024×768, now it’s 1280×800!)

Wordpress Updated

Wednesday, October 29th, 2008

As usual, I’m about two versions behind. (2.6.1) I’ve decided to stop procrastinating and upgrade WordPress. Happy to say it was flawless and we’re now running on 2.6.3.

(Wow great excuse to use up a whole post – don’t worry I’ve got stuff in store)

Tada! 2.6.1!

Tuesday, September 9th, 2008

I’ve just upgraded the WordPress version to 2.6.1. Taaa daaaa!

WordPress 2.6

Saturday, July 19th, 2008

I have just upgraded (flawlessly) the installation to WP 2.6. Hurrah!

Hello world!

Sunday, May 4th, 2008

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

Yes, that’s right! We’ve migrated (To change location periodically, especially by moving seasonally from one region to another) to WordPress! The installation was smooth, and we’ve got a sexy theme up doing some jazz for the the thinkMoult page!