Archive for May, 2010

Long/short headings (or title tags) using WordPress Custom Fields

[ This post is now largely if not entirely redundant now thanks to WordPress 3 menu options ]

Problem: WP titles are too long to fit in your navigation.

Solution:

Make the title short enough to fit in the navigation and then add a Custom Field into your post (in the example bellow it is set as “heading”) then edit your template file to render the longer version (“heading”) on the page.

Example:

In the Loop of  your template swap out

<h1><?php the_title(); ?></h1>
<?php the_content(); ?>

for:

<h1><?php// If has a custom field of heading ; then use that:
$customField = get_post_custom_values("heading");
if (isset($customField[0])) {
echo $customField[0];
} else {
the_title();
} ; ?></h1>
<?php the_content(); ?>

Hat-tip: Smashing Magazine

,

No Comments

Mac’s Apache web server crashing out

Spent a few hours yesterday try to fix my localhost web sharing. After a restart none of my local sites were responding, neither from my user account nor the back-up admin account.

This error message kept showing up in my Console:
com.apple.launchd[1] (org.apache.httpd[790]) Exited with exit code: 1

Followed a lengthy process to discount disk errors – (finding and fixing some serious ones) then a clean install and update of the system. Yeuch.

In the end it turns out to have been an error in the latest addition to my virtualhosts in /etc/apache2/virtualhosts. No error in the contents (created by the excellent virtualhost.sh script) – but something wrong with the file or its permissions or gawd knows what.

Obscure, strange and particularly annoying when a deadline is looming. Nevermind – next time I know to temporarily disable any virtualhosts when trying to find out what has killed my Apache server.

, , ,

No Comments

How to show just sticky posts in a WordPress template

Want to just show just your sticky posts in a WordPress template? This guide tells you how (and much more) – but you’re hacking a theme and are stuck when it says “add this code before the loop” – well the loop is the bit that starts  if (have_posts()). In the below example – how to when hackingh the soon-to-be-defunct default Kubric theme.

Add the following

< ?php

// this line just get the sticky posts:
query_posts(array('post__in'=>get_option('sticky_posts')));

// The Loop:
if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

No Comments

Include an HTML file in a PHP variable

I couldn’t get PHP to accept an HTML file include as the definition of a variable without throwing my templated page into disarray.  An answer came from the venerable desilva then checked with the php folk; use output  buffering (see example six in that last link):

<?php
$string = get_include_contents('somefile.php');

function get_include_contents($filename) {
 if (is_file($filename)) {
 ob_start();
 include $filename;
 $contents = ob_get_contents();
 ob_end_clean();
 return $contents;
 }
 return false;
}

?>

Also worth a look: file_get_contents

Why? Because Coda, my favourite Macintosh text editor+,  doesn’t do syntax highlighting of HTML for a PHP string. The site I’ve just finished for a high-end luxury villa in Marbella I had a big block of HTML to include (and work on) for the images page…

,

No Comments