WordPress Theme: Just Write

Just Write - Free WordPress Theme Just Write is a WordPress theme that I released back in March. This simple theme is geared towards writers and bloggers who want the focus of their site to be on their written content. It includes a primary menu, a custom site logo, and a sidebar that can be toggled.

I just released an update that introduces custom color options and minor bug fixes. The update still needs to be approved before it appears in the directory. But in the meantime, the latest version can be found on GitHub:

A demo can be found here. And the previous version can be found in the official directory here:

Just Write
by ryancowles

Just Write is a simple theme, geared towards writers and bloggers who want the focus of their site to be on their written content. It includes a primary menu, and a sidebar that can be toggled.

Stats:

  • Current version: 1.1
  • Rating: 90(2 ratings)
  • Downloaded 0 times

Set WordPress Featured Image as Facebook Thumbnail

If you have a WordPress blog, odds are that you are sharing your posts on Facebook and other social media websites. Some of these sites allow you to include a thumbnail image and description when you or your visitors share posts. For example, when you paste in a URL on Facebook or LinkedIn they will pull an image (if available) and description from the page. At first glance, it may appear that you don’t have much control over this.

However, both Facebook and LinkedIn utilize the Open Graph Meta tags (For more info about the Open Graph API in general, check out David Walsh’s Facebook Open Graph META Tags article.). If you take advantage of this, you can control which image and description is pulled by default.

How can I use Open Graph Meta Tags with WordPress?

It is a pretty straightforward process to add the required meta tags. First, we need to allow for Facebook markup in our opening HTML tags. In this example we are going to add a few filters in our functions.php  file to accomplish this (Note: you could also add the appropriate code to your header.php):

add_filter('language_attributes', 'add_og_xml_ns');
function add_og_xml_ns($content) {
  return ' xmlns:og="http://ogp.me/ns#" ' . $content;
}

add_filter('language_attributes', 'add_fb_xml_ns');
function add_fb_xml_ns($content) {
  return ' xmlns:fb="https://www.facebook.com/2008/fbml" ' . $content;
}

Now we can add the Open Graph meta tags just like regular meta tags and use WordPress’ Template Tags to fill them in. Here’s an example:

<meta property="og:title" content="<?php the_title(); ?>"/>
<meta property="og:description" content="<?php echo strip_tags(get_the_excerpt($post->ID)); ?>" />
<meta property="og:url" content="<?php the_permalink(); ?>"/>
<?php $fb_image = wp_get_attachment_image_src(get_post_thumbnail_id( get_the_ID() ), 'thumbnail'); ?>
<?php if ($fb_image) : ?>
	<meta property="og:image" content="<?php echo $fb_image[0]; ?>" />
<?php endif; ?>
<meta property="og:type" content="<?php 
	if (is_single() || is_page()) { echo "article"; } else { echo "website";} ?>"
/>
<meta property="og:site_name" content="<?php bloginfo('name'); ?>"/>

Most of the tags are pretty self explanatory, but feel free to ask questions if anything is unclear.  If you wanted to avoid touching your theme files, I’ve packaged this into a simple plugin that you can use as a starting point.  You can see the full code for the plugin here (the zip also contains a screenshot and a readme):

<?php
/*
Plugin Name: Facebook Featured Image and Open Graph Meta Tags
Version: 1.0
Plugin URI: http://www.ryanscowles.com
Description: Automatically set the posts' Featured Image as the thumbnail and set appropriate Open Graph meta tags for sharing on Facebook.
Author: Ryan S. Cowles
Author URI: http://www.ryanscowles.com
*/

define ('pluginDirName', 'fb-featured-image');

// Allow for Facebooks's markup language
add_filter('language_attributes', 'add_og_xml_ns');
function add_og_xml_ns($content) {
  return ' xmlns:og="http://ogp.me/ns#" ' . $content;
}

add_filter('language_attributes', 'add_fb_xml_ns');
function add_fb_xml_ns($content) {
  return ' xmlns:fb="https://www.facebook.com/2008/fbml" ' . $content;
}

// Set your Open Graph Meta Tags
function fbogmeta_header() {
  if (is_single()) {
    ?>
    	<!-- Open Graph Meta Tags for Facebook and LinkedIn Sharing !-->
		<meta property="og:title" content="<?php the_title(); ?>"/>
		<meta property="og:description" content="<?php echo strip_tags(get_the_excerpt($post->ID)); ?>" />
		<meta property="og:url" content="<?php the_permalink(); ?>"/>
		<?php $fb_image = wp_get_attachment_image_src(get_post_thumbnail_id( get_the_ID() ), 'thumbnail'); ?>
		<?php if ($fb_image) : ?>
			<meta property="og:image" content="<?php echo $fb_image[0]; ?>" />
			<?php endif; ?>
		<meta property="og:type" content="<?php
			if (is_single() || is_page()) { echo "article"; } else { echo "website";} ?>"
		/>
		<meta property="og:site_name" content="<?php bloginfo('name'); ?>"/>
		<!-- End Open Graph Meta Tags !-->

    <?php
  }
}
add_action('wp_head', 'fbogmeta_header');

Things to consider:

A lot of plugins and themes already include some or all of this functionality. For example, WordPress SEO by Yoast takes care of this for you. A lot of themes will also do this by default. To avoid redundancy you should check to see if the Open Graph tags already exist on your site. All code contained in this post is to be used on an “As-is” basis, and may contain errors. If you notice any, let me know!

Add a category image to a WordPress loop

I recently wrote an article on how to set a fallback image for featured images. Shortly after writing that I ran into a situation on my own site that required a slightly different solution. Instead of grabbing the first image from the post as a fallback, I wanted to set a fallback based on category. For example, if I have a post in my WordPress category that doesn’t have a featured image set, the WordPress logo will accompany the excerpt in the loop by default. I was able to accomplish in just a few short steps. I’ll show you how to add a category image to a WordPress loop.

Category Images

First, I had to create the images I wanted to use for my categories. I saved them all as JPG‘s with the category slug as the filename (e.g., wordpress.jpg for my WordPress category and web-development.jpg for the Web Development category). I then uploaded these to a new directory called cat-images in my theme’s images folder.

Calling Category Images in a Theme

Now the images are uploaded, but we still need to call them in our loop. If you are trying to insert them in your main blog feed you will probably find the loop in loop.php, home.php, front-page.php, or index.php.

First, let’s see if our post has a featured image set for it. If it does, let’s return it and be done with it:

<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?> //Check to see if a featured image is set
		<?php the_post_thumbnail('blog-thumb', array('class' => 'blog-thumb')); ?> //If so, let's return it

Then, if the post doesn’t have a post thumbnail associated, let’s grab the corresponding image we uploaded:

<?php else : //If there is no featured image set for the post do the following
?>
		<img class="cat-thumb" src="<?php echo get_template_directory_uri(); ?>/images/cat-images/<?php $category = get_the_category(); echo $category[0]->slug; ?>.jpg" /> <!-- Pull the corresponding image in your theme's /cat-images/ folder !-->

After that, we just have to close our if statement:

<?php endif; ?>

Now, let’s put it all together:

<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?> 

		<?php the_post_thumbnail('blog-thumb', array('class' => 'blog-thumb')); ?>

	<?php else :?>

		<img class="blog-thumb" src="<?php echo get_template_directory_uri(); ?>/images/cat-images/<?php $category = get_the_category(); echo $category[0]->slug; ?>.jpg" /> 

<?php endif; ?>

Variations

If you don’t want to use featured images in your loop, and instead just want to return the category image for all posts, you can strip out most of the code and just use the following:

<img class="blog-thumb" src="<?php echo get_template_directory_uri(); ?>/images/cat-images/<?php $category = get_the_category(); echo $category[0]->slug; ?>.jpg" />

Things to Watch For

If you see broken images shown in your loop, there is probably an incorrect path or filename somewhere. Make sure that the folder name in your theme’s directory matches the exact name and path as in your loop. Also, make sure the filenames of pictures are the exact category slugs (Pay attention to capitalization and file extensions).

Conclusion

Now, whenever you have a post with no featured image set, it will return the corresponding category image. Want an example? Just check out my main blog page. Have you done something similar, or have another way of doing this? Let me know!

References

WordPress Codex: has_post_thumbail
WordPress Codex: the_post_thumbnail