Archive for the ‘Wordpress’ 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.
WordPress XML Sitemap Plugin Review
I have been building quite a few sites with WordPress lately. WordPress has quite a long list of 3rd party plugins developed for various tasks and functions.
One downside to this is that there are several redundant WordPress plugins for common tasks, and their plugin search mechanism which is located at http://wordpress.org/extend/plugins/ is somewhat difficult to use.
I tend to search for the most installed or popular plugins and use the hive mind’s collective research to choose which plugin is the best, most robust, and best supported. Installing the most installed plugin does not always ensure that it is the best plugin, so I tend to test different WordPress plugins from time to time.
Recently, I installed Khang Minh’s XML Sitemap plugin called “Better WordPress Google XML Sitemaps (with sitemapindex and Multi-site support).” This creates a sitemapindex.xml file which references several other sub-sitemaps. This is the new trend in XML sitemaps because there is a 50,000 entry limit for a single sitemap file. By splitting the sitemaps into sub-sitemaps, you can broaden the number of items that you can index.
You can get the plugin from the WordPress plugin project page here. You can also visit Khang Minh’s website at http://betterwp.net/ which also showcases his several other WordPress plugins.
I also want to note that I had a problem with the plugin. The category sitemap wouldn’t generate. I posted a bug report to the issue queue, and he contacted me via Skype and did some testing on my WordPress installation. He found a bug with the code, fixed it, and created a new patch. The problem was with my database, there were several posts had been deleted, but were still categorized as “Uncategorized” which is the default WordPress category for new posts.
This is absolutely the best support I have ever received from a WordPress plugin or Drupal module developer. I have to say that he was a very nice guy, fixed the problem, and devoted his time to not only resolving my individual issue, but creating a patch to fix the particular scenario for the greater community as a whole.
I have not found any problems with any of the other WordPress XML sitemap plugins, but I will use the Better WordPress Google XML Sitemaps (with sitemapindex and Multi-site support) due to the high level of support that Khang Minh provides. His plugins seem solid and he has written and maintains several, so he has a high familiarity with the WordPress framework.
Other XML Sitemap Plugins of note are as follows:
- Google XML Sitemaps – this is the most popular XML Sitemap plugin, but it creates a single sitemap.xml file.
- Yoast’s SEO Ultimate also includes an XML sitemap generator, with very few options.
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.
How to dynamically set Facebook Opengraph Meta tags in WordPress headers
I recently developed a WordPress website where it was important that some pages could be liked and show up on a user’s Facebook status. The front page also had a Facebook fan page like box. This is getting to be pretty much the standard for most websites.
Setting up the placement of the buttons is pretty trivial. This guide shows you how to generate the widget code for the fan page like box, and this guide shows you how to generate the widget code for the like/recommend button.
Embedded the widget code into the WordPress or Drupal theme is pretty straightforward, and placing it so that it looks right is a matter of simple CSS.
Getting the like/recommend buttons to function properly so that the like action shows up on the user’s Facebook status, which then leads that user’s friends to your website is the difficult part. Additionally, what shows up in that link might not always be what you want.
Ideally, you want the proper page title, a short description under the page title, and a little image to the left of the title and description which is indicative of what the page is about.
You must add Facebook Opengraph meta tags to the pages header for this to work right. This is done by adding the meta tags to the header.php file in your WordPress theme.
That’s easy to do if you just want to set a static image, title, and description for all pages on your site. I’m going to show you how to do that dynamically.
First, let’s assume that we want to have something for the front page, something for pages that have featured images, and something for pages/posts that do not have a featured image. This is done with a PHP if statement and few WordPress short codes.
The first thing that happens is that we set two tags that are always going to be the same no matter what – the fb:admin tag, which is the profile ID of one of the admins of the Facebook Fan Page. The second tag is the URL of the link.
Next, if the front page is liked, then specify the title, default description, and logo.
The next part of the statement says that if the post is a page, and the page has a featured image attached, then use the post title, and featured image. The description is taken from the body field.
The else statement says that for all other pages, use the post title, but use the site’s default logo and description.
You may want to play with these a little bit, but for my project this is what I needed. I’m sure this will get you started and you can figure out the rest in getting the proper look you want on the user’s Facebook status.
See this example below: