Archive for the ‘Theming’ Category

Increasing the number of posts per page on category, tag, and archive pages in WordPress

In WordPress, you can set the number of pages to display per page in the Administration settings. This is set in Admin/Settings/Reading.

The issue with this is that this is a global setting. If you would like to set a different number of posts to display on the front page than on the category page, you can do it by adding this code to functions.php in your theme.

The following code displays 20 posts per page on all pages that are not the front page (index). This includes search results, categories, tag pages, archives, and author listings.

function change_number_of_posts($query) {
	if ( ! is_front_page()) // Make sure it is not the front page
	$query->query_vars['posts_per_page'] = 20; // Change 20 to the number of posts you would like to show
	return $query; // Return our modified query variables
}
add_filter('pre_get_posts', 'change_number_of_posts'); // Hook our custom function onto the request filter

You can change this only for category pages for example with this code.

function change_number_of_posts($query) {
	if ( $query->is_category ) // Make sure it is a category page
	$query->query_vars['posts_per_page'] = 20; // Change 20 to the number of posts you would like to show
	return $query; // Return our modified query variables
}
add_filter('pre_get_posts', 'change_number_of_posts'); // Hook our custom function onto the request filter

The result is that you can set the global setting in the WordPress admin page to 10 posts, which affects the front page (index). You can then insert this code into functions.php and all other pages will display 20 posts.

Note: You do not have to modify your theme other than functions.php for this to work.

Zero day vulnerability in many WordPress themes

Mark Maunder announced a zero day vulnerability in many WordPress themes yesterday here on his blog.

If you use any third party WordPress themes, you should check your server for the presence of timthumb.php or thumb.php. It is reported that several theme marketplace websites host common legitimate themes that utilize this library.

The fix is to remove any allowed sites from the thumb.php or timthumb.php files as recommended below.

BEFORE:

$allowedSites = array (
	'flickr.com',
	'picasa.com',
	'blogger.com',
	'wordpress.com',
	'img.youtube.com',
	'upload.wikimedia.org',
	'photobucket.com',
);

AFTER:

$allowedSites = array ();

Please read the full technical details at Mark’s blog in the link above.

How to display nicely formatted PHP code in a WordPress post

I’ve been struggling for a while about how to embed PHP, HTML, or CSS code into WordPress posts so that they look good and are easy to understand.

I found this list of 12 WordPress Plugins to Display and Highlight Code.

I researched a few of them and finally settled on SyntaxHighlighter Evolved, which was simple to install, easy to use with short codes, formats nicely, and seems to have a large existing user base.

In your posts, you can simply use [php] and [/php] shortcodes around your actual PHP code and it does it’s trick. You can review the plugin page for the many various types of languages/syntax that are available.

I will spare your eyes and use this for all embedded code from now on.

Updating Price PHP print statements in Ubercart

Recently, I updated an Ubercart shopping cart system on Drupal that was very out of date.

After the upgrade, the list price on the product pages had too many zeros. It looked like this.

MSRP $ 49.99000

After a bit of research, I learned that Ubercart has been updated to include more decimal places to accommodate more International currencies.

I took a look at the node-product.tpl.php template and found that the print statement for the list price was using the out of date print statement.

The old print statement looked like:

list_price ?>

The new print statement looks like this:

list_price); ?>

Now all is fine.

MSRP $ 49.99

Theming fields in Drupal 6

I needed to figure out how to theme a field in Drupal 6. The use case was that I wanted to field to be a URL. This is pretty simple and straightforward by using the CCK Link module, which is a sub-module of CCK.

I was able to input my URL, such as http://www.symetrikdesign.com with no problem, and the CCK display options are pretty good. However, I wanted to strip the “http://” part on the output display, but have the link remain the legitimate link of http://www.symetrikdesign.com

CCK Link Display field

Though, there are several options for outputting the display in the Admin/Content Type//Display fields, there is nothing that handles stripping some characters from the title.

So I figured that I needed to theme the custom field. I searched drupal.org and google.com far and wide and found that though the documentation was present, there wasn’t much of it and it wasn’t that clear.

Here’s what I did.

My field name was called artistwebsite, so I needed to make sure that there was a standard content-field.tpl.php in my theme’s directory and then copy/clone that and call the new file content-field-field_artistwebsite.tpl.php. This works like other types of Drupal entity template files, but the tricky part is that you need to find content-field.tpl.php into your theme’s directory, because it probably isn’t there by default, depending on your theme.

I now have the following in my theme directory, and a field called field_artistwebsite in my content type.
content-field.tpl.php
content-field-field_artistwebsite.tpl.php

I was able to re-write the output of a CCK Link field so that it removes the “http://” on the display title on the node.

I did this by adding the following lines of code in place of the commented out print statement below:

".$item['display_title']."";
         
/* Here is the original PHP print statement line in the stock/default content-field.tpl.php
*  file that I commented out and replaced with what is above.
* 
*/
?>
Consulting

I'm currently available
for Lotus Notes / Domino consulting engagements.

LinkedIn

Connect with me:

LinkedIn

Advertisement
Advertisement
Categories