Keith Devon

Menu

Tag: WP

Beginner WordPress Theme Development

Last Thursday (28th July 2011) we held the third London WordPress meetup. The meetup is for WordPress users and professionals in London and aims to provide a networking and learning platform.

I gave a talk on Beginner WordPress Theme Development, which introduced the basic structure of a theme and some key concepts. Here are the presentation ‘slides’ and the video.

Spinning Update progress on WordPress pages

Spinning Progress Wheel

One of my clients recently informed me that a site that I had built had a problem; when some pages were published or updated, the progress wheel would spin until the browser eventually timed out.

It had been a month or so since I had finished the site development and there had been lots of new content and plugins added. I turned on the WordPress debugging and got to work.

Finding the problem

There were a lot of errors being generated, mostly from plugins but also from WP itself. I also noticed a few memory limit issues which I fixed using the .htaccess file.

I got to work isolating the various components. All plugins, themes and content were tested to no avail. Next up I installed the site on different servers. I looked at the database for anomalies, contacted the hosting provider, nothing.

Lying in bed last night I couldn’t sleep, I had to give it another go. I did some more tests on what I thought was the offending plugin and associated database table. Still no joy. In one last ditch attempt I did various Google searches of the symptoms. I jumped from forum to forum and followed links all over the place.

Got it!

Then I found it. The custom permalink structure of “/%category%/%postname%/” that I have used on nearly every site was the culprit. Now, someone please correct me if I’m wrong, but I’m sure the WordPress codex used to give that exact structure as an example of how to implement “Pretty Permalinks”. Not any longer. Apparently that structure causes performance issues on sites with lots of pages. For every page a whole new set of rewrite rules are created until the system just can’t process it any more.

The solution is to use a non-verbose entry at the start. For example %year% or %month% or both are ideal but %category%, %postname%, %tag% and %author% should be avoided.

Fortunately, the codex has been updated to advise against this practice and the new example “pretty permalink” is /%year%/%postname%/.

Lessons learned

For most site you can probably get away with the offending structure. In fact many SEO and WordPress ‘gurus’ recommend it. Just be warned that if your site grows in terms of pages then you may encounter this problem in the future.

Some of the lessons that I’ll take with me:

  • Keep up to date with changes to WordPress documentation and best practices.
  • Clearly define who takes responsibility for WordPress faults in your project contract.
  • Never give up!

Internal Link Layout Issue

I recently came across an unusual problem when using internal links on a WordPress site. The link in question linked from one page on the site to a specific section on another page of the same site. When this link was followed there was an error with the page layout. More accurately, the new page layout was technically correct and the rest of the site was wrong.

I struggled for a while trying to find the source of this problem. I looked at the ‘error’ page in firebug and compared it to a normal page on the site. On all other pages there was a mysterious magin (or padding) created at the top of a div but when this page was visited from the link the margin/padding disappeared.

Long story short, the div in question was set to overflow:hidden. When I commented out that line, the problem resolved itself. Why? I don’t know. Can anyone shed any light on this?

Custom Post Type Archive Pages – WordPress

Need to show your custom post types on your category archive pages?

Add this to your functions.php file:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if(is_category() || is_tag()) {
    $post_type = get_query_var('post_type');
	if($post_type)
	    $post_type = $post_type;
	else
	    $post_type = array('post','cpt'); // replace cpt to your custom post type
    $query->set('post_type',$post_type);
	return $query;
    }
}

Now change the variable ‘cpt’ to the registered name of your custom post type, and voila!

A big thanks to parandroid over at http://wordpress.org/support/topic/custom-post-type-tagscategories-archive-page?replies=16 for this one.

WordPress Custom Post Templates

Once you have built a Wordpres custom post, you will often want to style it using it’s own template.

I searhed the web for a solution and courtesy of twothirdsdesign, if found the answer.

The template used for a custom post view is decided by the ‘get_single_template()’ function in the wp-includes/theme.php file.  And it basically tells locate_template() to look for single-’post_type’.php or single.php.

So the simplest way to customise the way a custom post is displayed is to add a template file to your theme with the name single-xxxxxx.php

Simples!

Adding the excerpt function to WordPress pages

The excerpt function that is so useful in WordPress posts, does not come as standard on pages. Luckily, the fix is easy. Add the following code to your functions.php file and voila! Excerpts on wordpress pages.

// add excerpts to pages

function add_page_excerpt_meta_box() { add_meta_box( ‘postexcerpt’, __(‘Excerpt’), ‘post_excerpt_meta_box’, ‘page’, ‘normal’, ‘core’ ); }

add_action( ‘admin_menu’, ‘add_page_excerpt_meta_box’ );

Sorting WordPress Posts Using Surnames

A client recently requested that their ‘Members’ page be sorted by surname and not first name. The lists were generated using the follwing code:

<?php$cat_args = array(
'sort_column' => 'post_title',
'order' => 'ASC',
'parent' => 12);
$categories =   get_categories($cat_args);
foreach($categories as $category) {
echo '<ul id="member"><li><h3>' . $category->name.'</h3></li>';
$post_args = array(
'numberposts' => 50,
'orderby' => 'title',
'order' => 'ASC',
'category' => $category->term_id    );
$posts = get_posts($post_args);
foreach($posts as $post) {	?>
<li><a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a></li>	<?php	}
echo '</ul>';
}
?>

To sort by surname I created a custom field in the Post Edit screen. The name of the custom field I set to ‘surname’ and I entered the member’s surname in the value field.

I then had to change the PHP code to look for the custom field and sort the list accordingly. I changed the ‘orderby’ => ‘title’, to ‘orderby’ => ‘meta_value’,’meta_key’ => ‘surname’,.

Now the members are sorted by their surnames. Simple! View the code in action here.

Note: If the custom field of ‘surname’ is not given a value then the post title will not display in the list.