Memes Reloaded
First of all, I want to wish a success packed very Happy New Year. I wasn’t too active in the recent holiday season and spent the last month getting back on track. As I stated in earlier I want to turn 2018 into a more techie year, thus I spent the last couple of days with geeking around with WordPress. The goal was to give memes a new way of appearance as I sent the Funny Friday series into early retirement.
I have to confess something. Before I started my blog and started using WordPress I was a hater. Back in the days when I entered web development, I was taught that it is evil. It is just a big pile of crappy spaghetti code and if you use it bad boys will hack your site and steal your data (maybe even your soul). I was a newbie and they were the seniors, so I accepted what they said and went on hating WordPress without any further investigation.
It is true that in those times these statements were partly valid. Those early versions were not so sophisticated and there were much more poorly coded third-party plugins and themes. These caused serious security problems in some cases. Still, I regret that I did not start examining the codebase and experimenting with template and plugin development earlier. I could be a WP Wizard these days.
Years went by. When I started to learn about blogging and realized that almost 30% (crazy!) percent of the websites on the internet is running on this platform this regret became even worse. Since I started this blog, I really wanted to dig a little bit into the topic and get my hands dirty. Unfortunately, while I was chasing my own deadlines I was not able to do so. Finally, the rethinking of the Funny Friday meme series brought the need for opening the hood and add tiny bits of code. As it turned out WP has a very handy develop documentation.
If you are not interested in a little bit of coding and you are satisfied with the functionality the codebase and the UI can provide, please, don’t read further. Just be delighted that the brand new MEMES sections became accessible from the top menu. Every time the muse visits me and I make a meme I will post it there. There will be new ones every sometimes 🙂
The tweak
You may ask why did I have to tweak my WP setup to make this possible. Well, maybe it is just my personal preference, but I wanted the memes listing page to work a little bit differently. First of all, created a separate category called “Memes”, created a separate menu item for its listing page and added the first post, Dory as my guinea pig (ok, maybe this is confusing). The post format was set to “Image”, well… because memes will be single/multi-image posts. The first thing I faced that this case Dory will jump to my main landing page as it will become the latest post.
Homepage exclusion
Maybe not a big deal, but as these posts will not have a featured image and it seems the excerpt, which shows up by default will not display the one and only image, the whole item looks odd. so I decided that the posts from this category should be excluded from the main listing page. A short search told me this is not something WP supports by default. There is a plugin for that (as for many other things), but I felt it an overkill for such a simple task. Same time, the fewer plugins you use the lower the overall complexity will be and because of this, I try to use as few plugins as possible. Further investigations lead me to the solution to supplement the theme’s functions. You can enter into theme file editor through Appearance menu’s Editor option. Then on the right side, you can find (probably, not true for all versions and all themes) Theme Functions (functions.php). You can exclude one or more categories from the main listing page adding the following lines to the end of the file.
function exclude_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '-33' ); } } add_action( 'pre_get_posts', 'exclude_category' );
You may ask “what the heck is happening here?”. Well, we declare a function called exclude_category which performs a check if the current query is the query for the blog homepage and if it is the main query. If both statements evaluate to true, then we exclude the memes category from the query. The only information you need is the category ID which you can find out from the URL when you are editing it (…&tag_ID=33… in my case). The minus sign before the category ID indicates that this is an exclusion. When WordPress is rendering our page after creating the query object, but before running it calls the hook called pre_get_posts which calls our function. The exclusion takes effect and from now on the posts from the memes category will not show up on the homepage.
Custom memes category page
Now when I checked the Memes listing page and what I saw is that the posts are still not showing up properly. It turned out that the theme uses the same setting for the category pages as the homepage to determine if it should show the whole posts or just the excerpts in the list. Because of the aforementioned reasons, we have to do something with this too. A little bit of research revealed that on rendering a category page, it looks for templates in the following order: category-slug.php → category-id.php → category.php → archive.php → index.php. In my theme, the archive.php template is the one which was responsible for rendering the Memes category page, so the only thing I had to do is to make a copy of it, rename it to category-memes.php and make some changes.
The original archives.php look like:
'.__( 'Browsed by', 'nisarg' ).'
', '' ); the_archive_description( '', '' ) ?>
After some decluttering the result is:
As you may see I have removed the unnecessary “Browsed by” part and removed the excerpt possibility.
So good so far. There is a separate menu point for the “Memes”, the posts do not show up on the homepage and their category page displays them properly.
There is only one thing remaining. Jetpack does good work in the comments section, auto-sharing on twitter and sending notification emails on new posts. However, when published Dory I realized that maybe my readers would not want to be notified by each and every new meme.
Exclusion from the notifications
Searching for such an option proved ineffectual so I turned again to good ol’ Google. Shortly after I added this couple of lines of code to the end of my functions.php.
add_filter( 'jetpack_subscriptions_exclude_these_categories', 'exclude_these' ); function exclude_these( $categories ) { $categories = array( 'memes' ); return $categories; }
I won’t waste many words on this, the code is self-explanatory and does the trick.
That’s all. Not a big deal from a technical perspective but it was fun. Depending on your technical savviness the things in this post can range from “black magic” to “piece of cake”. Whichever is the case thanks for reading and please share your thoughts with me in the comments about WordPress. Oh, and please let me know if I should go on this geeky road further or get back to the stories of the good ol’ days (I have both on my list).
Thank you and enjoy the brand new MEMES section.
One thought on “Memes Reloaded”
Nice work, man! I like to dabble in code, but it often comes back to bite me in the hiney. I’ll have to check into this snippet you’ve got here if I ever add a meme section myself.
As frustrating as it can be sometimes, coding is about as fun if not more so than actual writing.
Comments are closed.