WordPress: Wrap the published day, month, and year in span tags

Here is a quick and easy way to wrap the individual elements of your WordPress template’s published date output in tags:

<?php the_time('F <\s\p\a\n>jS</\s\p\a\n>, Y') ?>

Note: the key is to escape the each letter of the tag declaration with a back slash.

WordPress Tricks: How to create a WYSIWYG page template for an archive page

Recently, I was working on a WordPress site and needed the ability to have editable page content at the top of the archive page for a custom post type.

It took me a bit to figure out how to accomplish it but once I did, I am happy to report the solution was quite simple!

First, clone whichever page template in your theme that you want to use as the base template for your archive page. For example, let’s say you clone page.php and name your new file template-customposttype.php.

Make sure to change or add the template name at the very top of your cloned file:

<?php
/* Template Name: Custom Post Type Landing Page */
 get_header();
?>

Now, under the portion of the template which calls in the page content, let’s add a query for the custom post type we want to display on this page:

<?php query_posts(array('post_type' => 'customposttype')); ?>

Make sure you change ‘customposttype‘ to the actual name of the post type you want to display on this page!

Now, open your theme’s archive.php template file and copy the section between get_header() and get_footer(). Paste this into your template-customposttype.php file, just under the query_posts() statement.

Save your new template.

Now, in the WordPress dashboard, create a new page called My Custom Posts Archive Landing Page. In Page Attributes, make sure to select your new template (“Custom Post Type Landing Page”) to this page.

Add whatever content you want in the visual editor, then publish your new page and view it on your site.

You will now see your custom content at the top of the page and the list of blog posts for your custom post type underneath! Voila!

WordPress Tricks: A simple shortcode which will output the site title

Here is a very simple way to create a shortcode that will output your WordPress site’s title.

Copy and paste this snippet into your active theme’s functions.php file:

function site_title_shortcode() {
	 $blog_title = get_bloginfo('name');
	return $blog_title;
}
add_shortcode('site_title', 'site_title_shortcode');

Now you can easily output the current site title anywhere in your content using the shortcode [site_title]

WordPress SimplePress Theme Customization: Adding custom thumbnails to home page slider

I recently worked on a project where I needed the ability to have the navigation thumbnails on the home page slider of the SimplePress WordPress theme to be customizable, not automatically generated off of the featured image as they are by default. Here are the steps I took to accomplish that:

First, install and activate the Multiple Post Thumbnails plugin.

Next, copy and paste this code into your theme’s functions file (be sure to create a child theme of the SimplePress theme before making these customizaations!):

if (class_exists('MultiPostThumbnails')) {
	new MultiPostThumbnails(array(
	'label' => 'Secondary Image',
	'id' => 'secondary-image',
	'post_type' => 'post'
 ) );
 }

Third, open the “includes/featured.php” file of your child theme, and replace this code on lines 109-118:

<?php
	print_thumbnail( array(
	'thumbnail' 	=> $arr[$i]["thumb_small"],
	'use_timthumb' 	=> $arr[$i]["thumbnail"]["use_timthumb"],
	'alttext'		=> $arr[$i]["fulltitle"],
	'width'			=> (int) $width_small,
	'height'		=> (int) $height_small,
	'et_post_id'	=> $arr[$i]['post_id'],
) );
?>

With this:

<?php
if (class_exists('MultiPostThumbnails')) {                              
$t=0;
      while ($t<1) {
          $image_name = 'secondary-image';
          if (MultiPostThumbnails::has_post_thumbnail('post', $image_name)) { 
              $image_id = MultiPostThumbnails::get_post_thumbnail_id( 'post', $image_name, $arr[$i]['post_id'] ); 
          }   
          if ($image_id == "") {
              $image_id = get_post_thumbnail_id( $arr[$i]['post_id'] ); 
          }
              $image_attributes = wp_get_attachment_image_src( $image_id );  ?>
              <img src="<?php echo $image_attributes[0]; ?>" alt="<?php echo $arr[$i]["fulltitle"]; ?>" width="<?php echo $width_small; ?>" height="<?php echo $height_small; ?>">
          <?php
                $t++;
      }                                     
}; 
?>

And there you have it – now you have the ability to choose a secondary featured image for your posts which will be used for the navigation thumbnail in the home page slider when available, otherwise the thumbnail will default to the featured image as usual.

WordPress: How to Truncate the Length of Titles in Previous / Next Post Links

Here is the code I use to truncate the length of post titles in previous post / next post links on WordPress sites:

<?php
$prev_post = get_previous_post();
if($prev_post) {
   $prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
   $getlength = strlen($prev_title);
   $thelength = 25;
   echo '<a href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '">&lsaquo;&nbsp;';
   echo substr($prev_title, 0, $thelength);
   if ($getlength > $thelength) echo "...";
   echo '</a>&nbsp;';
}

echo '&nbsp;|&nbsp;';

$next_post = get_next_post();
if($next_post) {
   $next_title = strip_tags(str_replace('"', '', $next_post->post_title));
   $getlength = strlen($next_title);
   $thelength = 25;
   echo '<a href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '">';
   echo substr($next_title, 0, $thelength);
   if ($getlength > $thelength) echo "...";
   echo '</a>&nbsp;&rsaquo;';
}
?>

The result looks something like this:

« This is the title of th… | This is the title of th… »

WordPress Tips: How to use “get_sidebar()” in a foreach loop

Today I was working on a one-page WordPress blog template and encountered a situation where I need to put a call to the WordPress function “get_sidebar” within my loop. The sidebar only showed up on the first post result in that loop on the website however.

A bit of online research turned up this solution finally – .

Rather than using the default function “get_sidebar“, I ended up using an “include” for the sidebar.php file in my loop, like so:

<?php include( TEMPLATEPATH . '/sidebar.php'); ?>

And now the sidebar is being displayed in each iteration of my loop. A simple and effective workaround!

WordPress: Conditional PHP code to check if page is set as the website home page

Here is the code I use in my WordPress template files to add a conditional statement which checks to see if a page is set in the dashboard as the site home page:

<?php 
$front_page = get_option('page_on_front');
if (!is_page($front_page) ) { ?>
Show this text on the front page of the website.
<?php } ?>

How to use Google analytics on a parked domain

I have found a way to use Google analytics to track traffic on a parked domain by using a PHP conditional statement to check which domain a visitor is accessing your site with and to render the correct Google analytics javascript code based on the domain.

First, make sure to set up a Google analytics profile for each of your domains – both the domain your site lives on and each of the parked domains that you want to track traffic on.

Then added this PHP statement in your website where you would typically place the analytics code itself:

<?php
$firstUrl = "yourparkeddomain.com";
$secondUrl = "yourwebsitedomain.com";
$currentUrl = $_SERVER['SERVER_NAME'];

if($currentUrl == $firstUrl) { ?>
<script>
  //insert Google analytics script for your parked domain here
</script>

<?php } else { ?>
<script>
  //insert Google analytics script for your main website domain here
</script>  

<?php } ?>

I think this is pretty self-explanatory – the PHP script is checking the current URL that the visitor is accessing your site at against the different URLs you have defined in the PHP script and is loading the correct Google analytics script based off of that information.

I have found that this gives me a way to at least track the data of visitors who are landing on my site through parked domains.

How to add an incrementing number counter inside the WordPress “while” loop

This morning I spent some time figuring out how to add an incrementing number counter inside a WordPress “while” loop and here is how I finally accomplished it:

I defined the first number (in this case, the variable $postnum) before the “while” loop started, like so:

<?php $the_query = new WP_Query( $query_string.'showposts=4' ); ?>
<?php $postnum = 0; ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

Then I added my div with the incrementing number class within the “while” loop, like so:

 
<?php $postnum++;
	echo "<div class='homepagepost-";
	echo $postnum;
	echo "'></div>";
?>

Now my output for each post includes my custom-numbered div:

<div class="homepagepost-1"></div>
<div class="homepagepost-2"></div>
<div class="homepagepost-3"></div>

Resolved: Updating the server PHP version to 5.4 broke my Genesis Simple Sidebars plugin

I had to resolve an issue recently where a client’s hosting company had updated the PHP version of the server to 5.4 and this “broke” the plugin Genesis Simple Sidebars that was being used on their site, therefore causing the whole site to be down. The PHP error message being displayed in a browser when you tried to visit a site is what told me it was the Genesis Simple Sidebars plugin that was causing the site to be down.

I found this extremely useful thread which ultimately helped me to fix the issue – http://wordpress.org/support/topic/error-after-updating-to-php-54 – but want to outline what I did here to save others from having to scroll through the entire thread.

Step 1: Log into PHPMyAdmin or whichever interface you use to edit the database.

Step 2: Navigate to the wp_options table on the database (note: this may not be the exact name of the table, your table could be named differently depending on the $table_prefix defined in your WordPress site’s wp-config.php file)

Step 3: Navigate to the “ss_settings” field in the options table and open it to edit it.

Step 4: Remove this line of code from the beginning of the “ss_settings” content:

i:0;s:20:"__return_empty_array";

Step 5: Find the number listed right after “a:” at the beginning of the “ss_settings” content and subtract 1 from that number. So, for example, if the beginning of your “ss_settings” field looked like this:

a:37:

You will want to change it to read:

a:36:

Step 6: Save your changes to the “ss_settings” field and navigate to your website in a browser. Hopefully your site will be back up and running now!