Sticky Posts, Posts only in subcategories, popup comments – WP2.1
-
I have just started using WordPress, and the only version I have worked with is 2.1 – and the fact that it is free is amazing, I wish I had time to put into the project.
In trying to achieve various things I have seen how folks have done them in earlier versions and realised that it needs to be done differently now, partly to avoid plugins and partly to keep it simple.
So I thought I would write up the various things I have discovered, for other folks who are starting to use 2.1.
Sticky posts and posts showing only in categories that are ticked
These are useful and much requested, but they are easy to add yourself until it is standard in WP.
Looking at how adhesive would have worked (it didn’t work for me in 2.1), something more was needed anyway. Basically, sticky posts are associated with a particular section of your website, whether it is the home page, or a specific category, and you would not necessarily want them to appear in both. But if you did make a new category, you may want it to appear on your home page as a normal post (ie only be sticky in the category).
So what I did was create a sticky flag that can either be H, C or CH, depending on whether it should appear as
H – sticky, only on shows on home page
C – sticky, only shows in categories is it in
C+ – sticky in its categories, and normal post on home pageThe other thing I have addressed is making posts only appear in the ticked categories for that post, and not automatically appear in the parent categories (when looking at a category view).
I think there may be a bug in WP 2.1 actually – or at least an unintentional effect. When you specify the categories and subcategories of a post, you can choose to only put it in a subcategory, or put it in a category and subcategory.
If the engine honoured this, all would be fine, as you could just put a post in a subcategory and forget about it, or put it in the overall category, or both. But WP doesn’t do this, and I didn’t fancy going into the main engine to try to fix it, so for now I did a simple workaround. [Aside – I think WP must have extra code in specifically to pull in post in subcategories when viewing the parent category, so the ‘fix’ might be just to remove that functionality, as it really doesn’t help]
So, to implement the above functionality:
– Add a custom field, called sticky, to a post. In WP2.1 when you add a custom field to any post, it then appears as a dropdown option for all other posts. I simply set the field to H or C or HC to make any post sticky.
– In your theme, copy index.php to home.php and category.php and modify them as follows. This is the tricky bit if you are not a programmer, but basically what you start with in home.php is a loop that goes through all the posts and displays them. What I did was duplicate the loop, and the first one picks out posts with sticky set to H or HC, and the second with it set to blank. Then, only in the category.php, I further extended it to only show posts that are in the category being viewed.The loop starts with:
<?php while (have_posts()) : the_post(); ?>
and ends with:
<?php endwhile; ?>
What I did was:
– duplicate the whole block of code (in both home.php and category.php)
– For home.php, first loop, insert these lines after the start:
<?php if ( get_post_meta($post->ID, "sticky", true) != "H") continue ; ?>
– For home.php, second loop, insert these lines after the start:
<?php if ( get_post_meta($post->ID, "sticky", true) != "" &&
get_post_meta($post->ID , "sticky", true) != "C+" ) continue ; ?>– For category.php, first loop, insert these lines after the start:
<?php
if ( get_post_meta($post->ID, "sticky", true) != "C" &&
get_post_meta($post->ID , "sticky", true) != "C+") continue ;
$jez_post_cats = &get_the_category($post_id);
$jez_current_cat = intval( get_query_var('cat') );
$jez_ignore_this_post = true ;
foreach ( $jez_post_cats as $jez_check_cat )
if ( $jez_current_cat == (int) $jez_check_cat->cat_ID
$jez_ignore_this_post = false ;
if ($jez_ignore_this_post) continue ;
?>– For category.php, second loop, insert these lines after the start:
<?php
if ( get_post_meta($post->ID, "sticky", true) != "") continue ;
$jez_post_cats = &get_the_category($post_id);
$jez_current_cat = intval( get_query_var('cat') );
$jez_ignore_this_post = true ;
foreach ( $jez_post_cats as $jez_check_cat )
if ( $jez_current_cat == (int) $jez_check_cat->cat_ID )
$jez_ignore_this_post = false ;
if ($jez_ignore_this_post) continue ;
?>Comments to open in new resizable window
When clicking on the comments (where it says 27 comments etc), I wanted it to open in a new window instead of navigating there. However I also wanted the window to be resizable. WP2.1 does support popup comment windows, but it is disabled by default, and there appears to be no admin option to turn it on, or set the size of window or whether it is resizable (would be nice to have them all in admin).
So what I did was, in my theme, edit header.php and added the following line:
<?php comments_popup_script(750,500); // off by default ?>
I added it just before the<title>
tag, very near the top. 750 and 500 are the width and height of the window that will pop up.This still opened a fixed size window though, and I wanted it resizeable, so I changed the WP engine very trivially. Edit wp-includes/comment.template.php and find the window opening line that contains
scrollbars=yes,status=yes
and change this toscrollbars=yes,resizable=yes,status=yes
Single Post to include sidebar
The next thing I wanted was the sidebar to always be there (I’d suggest this should be the default behaviour for WP – losing navigation in a web site is imho always a bad thing – at least an admin option to control it would be good).
I read various posts and people had various awkward ways of doing it, and ways that resulted in incomplete sidebars etc.
There are only two very minor changes that need making to single.php (in your theme) to achieve this (at least in WP 2.1):
– Very near the top, change the class of the “content” div from widecolumn to narrowcolumn.
o Old:<div id="content" class="widecolumn">
o New:<div id="content" class="narrowcolumn">
– Very near the end, just before the get_footer() line, add the following line:
o<?php get_sidebar(); ?>
Categories to show empty categories
It would be nice if this was a switch in the admin pages. However, you can modify your sidebar.php in your theme. Find the following line:
<?php wp_list_categories('show_count=1&title_li=<h2>Categories</h2>'); ?>
and change it to
<?php wp_list_categories('hide_empty=0&show_count=1&title_li=<h2>Categories</h2>'); ?>
- The topic ‘Sticky Posts, Posts only in subcategories, popup comments – WP2.1’ is closed to new replies.