Category Archives: Wordpress

Latex for WordPress

I’ve just installed ‘Jetpack‘ which comes with \LaTeX. This post is to provide a quick reference. \LaTeX parses the text in the WordPress post, and lays out the display as a graphic for the viewer. Though someone reading this will see images, there are no images referred to in the edited post.

\LaTeX is a generalised document layout language, but as far as I am concerned, the main use of \LaTeX in WordPress is mathematical.

Note that as the images are generated on the fly, the page will be slower to load than if the page were pre-rendered. Therefore, I think it may still be best for the web to generate the image, save it and then display the result via an <img> tag (keeping the \LaTeX hidden in a comment so that the image can be regenerated if need be). As already mentioned, I have not pre-generated the images for this post, it is all done in code.

It’s a pity that MathML isn’t well supported at the moment.
Continue reading Latex for WordPress

Series Linking – A solution

A while ago, I posted about a problem I was having with series links. I usually would post the solution soon after, but it seems I forgot.

I can’t recall the process of how I got to this solution, so apologies for not linking anywhere I should to give credit, but this is what I have in my functions.php now.

Things I’d like to work out are:
Can I have a post in more than one series (with current code, no, I suspect a rewrite may be in order for that)
Can I have a second type of series link, one to link to another series? Ideally, this would be ‘previous series’ and ‘next series’. I suspect I wouldn’t want to attach these relations to posts, as I’d want the links to be on the last and first posts in a series, and that may change with new posts being added.
Can I list the names of series which I have defined on an admin page somewhere? Ideally, with the post titles listed right there alongside.

[php]function mrk_theme_article_footer($content)
{
$newcontent = $content;
$psttitle=get_the_title();

global $id, $post;

// Check to see if this post is part of a series, if it isn’t skip
$series = get_post_meta($post->ID, ‘Series’, true);
if ($series) :
$args = array(
‘numberposts’ => -1,
‘meta_key’ => ‘Series’,
‘meta_value’ => $series,
‘orderby’ => ‘post_date’,
‘order’ => ‘asc’,
);
$series_posts = get_posts($args);

// Check how many posts have been grabbed – if only 1, then move on
$seriescount = count($series_posts);
if ($seriescount > 1)
{
// Now to throw away all but the first, previous, current, next and last base this on date.
// First let’s initialise all the variables – these values will change soon!
$currpostdate = $post->post_date;
$currpostID = $post->ID;

$lastpostdate = $currpostdate;
$lastpostID=$currpostID;

$firstpostdate = $currpostdate;
$firstpostID = $currpostID;

$nextpostdate = $currpostdate;
$nextpostID = $currpostID;

$prevpostdate = $currpostdate;
$prevpostID = $currpostID;

$counter = 0;

foreach ($series_posts as $serial) :
// Check for two key posts earlier than this – first look if I have an earlier post than the known earliest
// currpost refers to the post currently selected in the loop
// firstpost refers to the first post in a series – lastpost similar
// prev and next refer to the next and prev post in a series
// serial refer to the current series post being examined to see if it may be one of the above
$counter=$counter +1;
if ($serial->post_date < $firstpostdate)
{
$firstpostdate = $serial->post_date;
$firstpostID=$serial->ID;
if ($prevpostdate == $currpostdate)
{
// I have an earlier post than the current one, so if the previous post is set to point at the current post, change this
$prevpostdate = $firstpostdate;
$prevpostID = $firstpostID;
}
}

// Is the post found dated between the current post and the current date for previous post?
if ($serial->post_date < $currpostdate)
{
if ($serial->post_date > $prevpostdate )
{
$prevpostdate = $serial->post_date;
$prevpostID = $serial->ID;
}
}
if ($serial->post_date > $lastpostdate)
{
$lastpostdate = $serial->post_date;
$lastpostID=$serial->ID;
if ($nextpostdate == $currpostdate)
{
$nextpostdate = $lastpostdate;
$nextpostID = $lastpostID;
}
}
if ($serial->post_date > $currpostdate )
{
if ($serial->post_date < $nextpostdate )
{
$nextpostdate = $serial->post_date;
$nextpostID = $serial->ID;
}
}

if ($serial->post_date == $currpostdate)
{
$postnumber = ordinal_suffix($counter,1);
}
endforeach; // I now know the first, prev, next and last in series

// First, we’ll do some database lookups to get the info needed. I know it’s already in an array, but I’m being lazy
$firstpostarray=get_post($firstpostID);
$firsttitle = $firstpostarray->post_title;
$firstlink = get_permalink($firstpostID);

$prevpostarray=get_post($prevpostID);
$prevtitle = $prevpostarray->post_title;
$prevlink = get_permalink($prevpostID);

$lastpostarray=get_post($lastpostID);
$lasttitle = $lastpostarray->post_title;
$lastlink = get_permalink($lastpostID);

$nextpostarray=get_post($nextpostID);
$nexttitle = $nextpostarray->post_title;
$nextlink = get_permalink($nextpostID);

$seriesclass = preg_replace("/[^a-z0-9\\040\\.\\-\\_\\\\]/i", "", $series);
$seriesclass = ‘series-‘ . strtolower(str_replace(array(‘ ‘, ‘ ‘), ‘-‘, $seriesclass));

// Single posts are handled differently to summary posts
if (is_single())
{
// Create a div to hold the links
$extra = ‘<div class="series seriessingle ‘ . $seriesclass . ‘"><h4 class="series-title">Series Information</h4>’;
$extra = $extra . ‘<ul>’;

// This bit of code is for first and last pages
if ($firstpostID == $currpostID || $lastpostID == $currpostID)
{
// If the first or last post, show the whole list of links
$extra = $extra . ‘<p>&quot;’ . $psttitle . ‘&quot; is the ‘;
if ($firstpostID == $currpostID)
{
$extra = $extra . ‘first’;
}
else
{
$extra = $extra . ‘last’;
}
$extra = $extra . ‘ in a sequence of ‘ . $seriescount . ‘ posts</p>’;

// For the first and last post only show the full list
foreach ($series_posts as $serial) : //Output list of posts
$extra = $extra . ‘<li>’;
if ($serial->ID == $currpostID)
{
$extra = $extra . ‘<strong>This post: </strong> ‘ . $psttitle;
}
else
{
$extra = $extra . ‘<a href="’ . get_permalink($serial->ID) . ‘">’ . $serial->post_title . ‘</a>’;
}
$extra = $extra . ‘</li>’;
endforeach; // Finish outputting list of posts
$extra = $extra .'</ul>’;
$extra = $extra . ‘</div> <!– end of series stuff –>’;
} // The above bit was for first and last pages
else
{ // Output a display for posts other than the first and last.
$extra = $extra . ‘<p>&quot;’ . $psttitle . ‘&quot; is ‘. $postnumber .’ in a larger sequence of ‘ . $seriescount .’ posts</p>’;
$extra = $extra . ‘<ul>’;

// Is the previous post the first post?
if ($firstpostID == $prevpostID)
{
$extra = $extra . ‘<li><a href="’. $firstlink .’">’. $firsttitle. ‘</a> – First in Series (and Previous Post)</li>’;
}
else
{
$extra = $extra . ‘<li><a href="’. $firstlink .’">’. $firsttitle. ‘</a> – First in Series</li>’;
$extra = $extra . ‘<li><a href="’. $prevlink .’">’. $prevtitle. ‘</a> – Previous Post</li>’;
} // End output first and previous
// Is the next post the last post?
if ($nextpostID == $lastpostID)
{
$extra = $extra . ‘<li><a href="’. $nextlink .’">’. $nexttitle. ‘</a> – Next in Series (and Last Post)</li>’;
}
else
{
//End next post is last post
$extra = $extra . ‘<li><a href="’. $nextlink .’">’. $nexttitle. ‘</a> – Next Post</li>’;
$extra = $extra . ‘<li><a href="’. $lastlink .’">’. $lasttitle. ‘</a> – Last in Series</li>’;
} // End output next and last
$extra = $extra .'</ul>’;
$extra = $extra . ‘</div> <!– end of series stuff –>’;
}
} // End of code if it is a single post

else
{ //Now do summary posts
$extra = ‘<div class="series seriessummary "’ . $seriesclass .’"><p>’;
if ($currpostID == $firstpostID)
{
// For the first post in the series, link to the latest post
$extra = $extra . ‘This post is the first in a series of ‘ . $seriescount .’ posts, the latest is ‘;
$extra = $extra . ‘<a href="’ . $lastlink . ‘">’ . $lasttitle . ‘</a></p>’;
}
else
{
// For any post in the series, link to the first post

$extra = $extra . ‘This post is ‘;

// The latest post is a special case
if ($currpostID == $lastpostID)
{
$extra = $extra . ‘the latest in’;
}
else
{
$extra = $extra . ‘part of’;
}
$extra = $extra . ‘ a series of ‘ . $seriescount .’ posts which begins with <a href="’;
$extra = $extra . $firstlink;
$extra = $extra . ‘">’ . $firsttitle . ‘</a>’;

// If not the latest post, link to that.
if ($currpostID == $lastpostID)
{
$extra_stuff_post = $extra_stuff_post . ‘. ‘;
}
else
{
$extra_stuff_post = $extra_stuff_post . ‘, the latest post in the series is <a href="’;
$extra_stuff_post = $extra_stuff_post . $lastlink . ‘">’ . $lasttitle . ‘</a>.’;
}
$extra_stuff_post = $extra_stuff_post . ‘</p>’;
}
$extra = $extra . ‘</div> <!– end of series stuff –>’;
} // End of Summary posts series code
} // End of what to do if series has several links

$newcontent = $content . $extra;
endif; // End of series code

return $newcontent;
}
// ——————————————————

add_filter(‘the_content’, ‘mrk_theme_article_footer’);[/php]

WordPress Problem – Series Posts

NOTE: Problem fixed – code unedited from the bugged version

———-
On my private ‘test-bed’ installation, I thought I’d update my Series post function for compatibility with TwentyEleven WordPress Theme and standard hooks, as opposed to Thematic.

The idea is that I add a custom field to my posts called ‘Series’ with a value of my choosing – and then the script adds links to other posts in the same series.

It works, in that single posts and some summary posts are formatted as required – but when a summary has NON series link posts, it breaks. I don’t see why, as far as I can see it should simply return the original content unaffected if the post being processed isn’t a series link. Worse, it doesn’t simply break that post on a summary page, but it stops processing other posts.

As I type, I have an earlier version working on this site – and it is this which was modified – however, I can see nothing that would prevent posts appearing on summary pages.

I can’t see the error – I would appreciate any insights readers may have.

On my test-bed site, in functions.php I have the following:

[php]function mrk_theme_article_footer($content)
{
$original_content = $content;
$psttitle = get_the_title();
$pstbody = get_the_excerpt();
$siteurl = get_bloginfo(‘wpurl’);
$themeurl = get_bloginfo(‘stylesheet_directory’);
$pageurl = get_permalink($post->ID);

$extra = ”;

global $id, $post;

// Check to see if this post is part of a series, if it isn’t skip
$series = get_post_meta($post->ID, ‘Series’, true);
if ($series) :
$args = array(
‘numberposts’ => -1,
‘meta_key’ => ‘Series’,
‘meta_value’ => $series,
‘orderby’ => ‘post_date’,
‘order’ => ‘asc’,
);
$series_posts = get_posts($args);

// Check how many posts have been grabbed – if only 1, then move on
$seriescount = count($series_posts);
if ($seriescount > 1)
{
// Now to throw away all but the first, previous, current, next and last base this on date.
// First let’s initialise all the variables – these values will change soon!
$currpostdate = $post->post_date;
$currpostID = $post->ID;

$lastpostdate = $currpostdate;
$lastpostID=$currpostID;

$firstpostdate = $currpostdate;
$firstpostID = $currpostID;

$nextpostdate = $currpostdate;
$nextpostID = $currpostID;

$prevpostdate = $currpostdate;
$prevpostID = $currpostID;

$counter = 0;

foreach ($series_posts as $serial) :
// Check for two key posts earlier than this – first look if I have an earlier post than the known earliest
// currpost refers to the post currently selected in the loop
// firstpost refers to the first post in a series – lastpost similar
// prev and next refer to the next and prev post in a series
// serial refer to the current series post being examined to see if it may be one of the above
$counter=$counter +1;
if ($serial->post_date < $firstpostdate)
{
$firstpostdate = $serial->post_date;
$firstpostID=$serial->ID;
if ($prevpostdate == $currpostdate)
{
// I have an earlier post than the current one, so if the previous post is set to point at the current post, change this
$prevpostdate = $firstpostdate;
$prevpostID = $firstpostID;
}
}

// Is the post found dated between the current post and the current date for previous post?
if ($serial->post_date < $currpostdate)
{
if ($serial->post_date > $prevpostdate )
{
$prevpostdate = $serial->post_date;
$prevpostID = $serial->ID;
}
}
if ($serial->post_date > $lastpostdate)
{
$lastpostdate = $serial->post_date;
$lastpostID=$serial->ID;
if ($nextpostdate == $currpostdate)
{
$nextpostdate = $lastpostdate;
$nextpostID = $lastpostID;
}
}
if ($serial->post_date > $currpostdate )
{
if ($serial->post_date < $nextpostdate )
{
$nextpostdate = $serial->post_date;
$nextpostID = $serial->ID;
}
}

if ($serial->post_date == $currpostdate)
{
$postnumber = ordinal_suffix($counter,1);
}
endforeach; // I now know the first, prev, next and last in series

// First, we’ll do some database lookups to get the info needed. I know it’s already in an array, but I’m being lazy
$firstpostarray=get_post($firstpostID);
$firsttitle = $firstpostarray->post_title;
$firstlink = get_permalink($firstpostID);

$prevpostarray=get_post($prevpostID);
$prevtitle = $prevpostarray->post_title;
$prevlink = get_permalink($prevpostID);

$lastpostarray=get_post($lastpostID);
$lasttitle = $lastpostarray->post_title;
$lastlink = get_permalink($lastpostID);

$nextpostarray=get_post($nextpostID);
$nexttitle = $nextpostarray->post_title;
$nextlink = get_permalink($nextpostID);

$seriesclass = preg_replace("/[^a-z0-9\\040\\.\\-\\_\\\\]/i", "", $series);
$seriesclass = ‘series-‘ . strtolower(str_replace(array(‘ ‘, ‘ ‘), ‘-‘, $seriesclass));

// Single posts are handled differently to summary posts
if (is_single())
{
// Create a div to hold the links
$extra = ‘<div class="series seriessingle ‘ . $seriesclass . ‘"><h4 class="series-title">Series Information</h4>’;
$extra = $extra . ‘<ul>’;

// This bit of code is for first and last pages
if ($firstpostID == $currpostID || $lastpostID == $currpostID)
{
// If the first or last post, show the whole list of links
$extra = $extra . ‘<p>&quot;’ . $psttitle . ‘&quot; is the ‘;
if ($firstpostID == $currpostID)
{
$extra = $extra . ‘first’;
}
else
{
$extra = $extra . ‘last’;
}
$extra = $extra . ‘ in a sequence of ‘ . $seriescount . ‘ posts</p>’;

// For the first and last post only show the full list
foreach ($series_posts as $serial) : //Output list of posts
$extra = $extra . ‘<li>’;
if ($serial->ID == $currpostID)
{
$extra = $extra . ‘<strong>This post: </strong> ‘ . $psttitle;
}
else
{
$extra = $extra . ‘<a href="’ . get_permalink($serial->ID) . ‘">’ . $serial->post_title . ‘</a>’;
}
$extra = $extra . ‘</li>’;
endforeach; // Finish outputting list of posts
$extra = $extra .'</ul>’;
$extra = $extra . ‘</div> <!– end of series stuff –>’;
} // The above bit was for first and last pages
else
{ // Output a display for posts other than the first and last.
$extra = $extra . ‘<p>&quot;’ . $psttitle . ‘&quot; is ‘. $postnumber .’ in a larger sequence of ‘ . $seriescount .’ posts</p>’;
$extra = $extra . ‘<ul>’;

// Is the previous post the first post?
if ($firstpostID == $prevpostID)
{
$extra = $extra . ‘<li><a href="’. $firstlink .’">’. $firsttitle. ‘</a> – First in Series (and Previous Post)</li>’;
}
else
{
$extra = $extra . ‘<li><a href="’. $firstlink .’">’. $firsttitle. ‘</a> – First in Series</li>’;
$extra = $extra . ‘<li><a href="’. $prevlink .’">’. $prevtitle. ‘</a> – Previous Post</li>’;
} // End output first and previous
// Is the next post the last post?
if ($nextpostID == $lastpostID)
{
$extra = $extra . ‘<li><a href="’. $nextlink .’">’. $nexttitle. ‘</a> – Next in Series (and Last Post)</li>’;
}
else
{
//End next post is last post
$extra = $extra . ‘<li><a href="’. $nextlink .’">’. $nexttitle. ‘</a> – Next Post</li>’;
$extra = $extra . ‘<li><a href="’. $lastlink .’">’. $lasttitle. ‘</a> – Last in Series</li>’;
} // End output next and last
$extra = $extra .'</ul>’;
$extra = $extra . ‘</div> <!– end of series stuff –>’;
} // End of code if it is a single post
}
else
{ //Now do summary posts
$extra = ‘<div class="series seriessummary "’ . $seriesclass .’"><p>’;
if ($currpostID == $firstpostID)
{
// For the first post in the series, link to the latest post
$extra = $extra . ‘This post is the first in a series of ‘ . $seriescount .’ posts, the latest is ‘;
$extra = $extra . ‘<a href="’ . $lastlink . ‘">’ . $lasttitle . ‘</a></p>’;
}
else
{
// For any post in the series, link to the first post
$extra = $extra . ‘This post is ‘;

// The latest post is a special case
if ($currpostID == $lastpostID)
{
$extra = $extra . ‘the latest in’;
}
else
{
$extra = $extra . $postnumber . ‘ in’;
}
$extra = $extra . ‘ a series of ‘ . $seriescount .’ posts which begins with <a href="’;
$extra = $extra . $firstlink;
$extra = $extra . ‘">’ . $firsttitle . ‘</a>’;
$extra = $extra . ‘the latest in’;
}
else
{
$extra = $extra . $postnumber . ‘ in’;
}
$extra = $extra . ‘ a series of ‘ . $seriescount .’ posts which begins with <a href="’;
$extra = $extra . $firstlink;
$extra = $extra . ‘">’ . $firsttitle . ‘</a>’;

// If not the latest post, link to that.
if ($currpostID == $lastpostID)
{
$extra = $extra . ‘. ‘;
}
else
{
$extra = $extra . ‘, the latest post in the series is <a href="’;
$extra = $extra . $lastlink . ‘">’ . $lasttitle . ‘</a>.’;
}
$extra = $extra . ‘</p>’;
}
$extra = $extra . ‘</div> <!– end of series stuff –>’;
} // End of Summary posts series code
} // End of what to do if series has several links

$content = $original_content . $extra;
endif; // End of series code

return $content;
}

add_filter(‘the_content’, ‘mrk_theme_article_footer’,0);[/php]

Running WordPress on Mac OS X

For a while, I’ve been wanting to install wordpress on Mac OS X, so that when I modify themes I can do this offline for testing. I also want to have the theme modifications automatically upload to the servers and to mirror the site database on my local copy.

The information how to do this is ‘out there’ – but I thought it would be good to collect it all in one place. Much of this could be adapted for other platforms such as windows or Unix. Pick and mix what you need, this is what I’ve done.

As always with this sort of thing, it’s your own responsibility if you mess up – understand each command before you type it. If in doubt, type man command, e.g. man rf )

Continue reading Running WordPress on Mac OS X

Updated Code for Series Posts

I just posted about a problem with series posts. Almost immediately I solved it.

I’m not really ready for this to go public – as all it does is tag the posts (I really want to only show the HTML of those posts), but I wanted to check that my manual remedy for ensuring that posting code doesn’t do funny things to my friend’s RSS reader works (see comment on previous). I’ve tried to prevent this by using a ‘read more’ tag as that should stop the RSS at that point.

Ultimately, I’d want to have the software automatically trim down the lines so I don’t have to rely on doing this manually.

This code tags the first/last/prev and next posts, and then uses CSS to clean it up.
Continue reading Updated Code for Series Posts