WordPress: hierarchical order taxonomies on single post

WordPress: Taxonomy hierarchy on a single post

In WordPress, you can easily get the custom taxonomy terms that belong to the current post. Just use get_the_term_list. That gives you links to all taxonomy terms of that particular post. However, they’re not hierarchical, so if you have a parent category, that will be just displayed within the list (in alphabetical order). Probably not how you want it.

How to get your single post taxonomies in a hierarchical order in WordPress? You have to use wp_get_post_terms().

Even then, it’s not so obvious how to get your current post’s terms in hierarchical order, including links to the term. But it is possible, see the code below.


function taxonomy_hierarchy() {
	global $post;
	$taxonomy = 'your_taxonomy'; //Put your custom taxonomy term here
	$terms = wp_get_post_terms( $post->ID, $taxonomy );
	foreach ( $terms as $term )
        {
	if ($term->parent == 0) // this gets the parent of the current post taxonomy
	{$myparent = $term;}
        }
	echo '<a href="'.get_term_link($myparent).'" title="'.$myparent->name.'">'.$myparent->name.'</a>';
	// Right, the parent is set, now let's get the children
	foreach ( $terms as $term ) {
		if ($term->parent != 0) // this ignores the parent of the current post taxonomy
		{ 
		$child_term = $term; // this gets the children of the current post taxonomy
		echo '<a href="'.get_term_link($child_term).'" title="'.$child_term->name.'">'.$child_term->name.'</a>';
		}
    }	
}

Put this code in your theme's (preferably a child theme's) 'functions.php' file and call it within a theme template or hook it into an action. To explain how to do that is beyond the scope of this WordPress tip, sorry.

Of course you can add other details in the HTML and style the output in CSS. In the example image, I've divided the output in a 'Main Category'(the parent) and 'Business Type'(the children).

Just to be clear: this is about getting hierarchical taxonomy terms in use by the current post.
Getting all taxonomy terms (independent of what terms are in use by the current post) in hierarchical order is quite easy,
just use _get_term_hierarchy.

WP Plugin Content Views: Manual Excerpt

So, you want to use the WordPress plugin Content Views (full title is: “Content Views Query and Display Post Page”, don’t get me started on this ridiculous plugin name…). Anyway, it’s a very nice plugin, we use it on this Web Design Tips & Tricks page.

The problem is, in the free version, on your overview page you can only get the “full content” of a blog or news post or the first X number of words of the post, not the manual excerpt you can set in a post edit.

Well, that’s very easy to customize. Get the file “html.php” (full path: /content-views-query-and-display-post-page/includes/html.php) and find this code (around line 456):

case 'full':
	ob_start();
	the_content();
	$content = ob_get_clean();

Change "the_content();" to "the_excerpt();" and save your changes.

Et voila! There's your manual excerpt on the posts overview page!

(Be sure to set the Content Settings to "Show Full Content" under Display settings.)

The downside is: you have to remember this change, because every time you update this plugin it's overwritten and you have to do it again. Just make a note of it to apply this change when the plugin is updated.

WordPress emoticons (or emoji) and how to remove them

WordPress introduced emoticons (well, they call them emoji’s, don’t ask me why) in version 4.2 of the popular Content Management System (CMS). To be honest, we don’t understand why they did that. WordPress has evolved from a simple blogging platform to a full-blown CMS. Introducing emoticons as a standard feature to us seems like going back to their roots: a blogging platform, where emoticons (or ’emojicons’) would have a role.

But NOT in serious websites serving businesses, artists portfolio’s, online shopping, and so on. So, why WordPress has not introduced it as an option (“Would you like to enable emojicons? YES/NO”) is beyond us.

Anyway, to completely disable emojicons in WordPress (including in the editor screen) is simple. Add the following code to the ‘functions.php’ file of your WordPress theme:


function disable_wp_emojicons() {
  // all actions related to emojis
  remove_action( 'admin_print_styles', 'print_emoji_styles' );
  remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
  remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  remove_action( 'wp_print_styles', 'print_emoji_styles' );
  remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
  remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
  remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
  // filter to remove TinyMCE emojis
  add_filter( 'tiny_mce_plugins', 'disable_emojicons_tinymce' );
}
add_action( 'init', 'disable_wp_emojicons' );

function disable_emojicons_tinymce( $plugins ) {
  if ( is_array( $plugins ) ) {
    return array_diff( $plugins, array( 'wpemoji' ) );
  } else {
    return array();
  }
}
Geografische Targeting met Google Webmaster Tools

Geotargeting with Google Webmaster Tools

We assume you are familiar with Google Webmaster Tools. You probably have already submitted your sitemaps and regularly check if these are indexed or whether Google reports problems with your sitemaps and/or website.

Did you know you can target (parts of) your website to a specific country? You don’t need to do that when you have a top-level country domain, like .au, .uk, .fr, or .nl. Google then already automatically assumes your website is targeted at that specific country. This tip only applies to generic top level domains (gTLDs), like .com, .edu, or .org.

If you have a multi-lingual website, it may be beneficial to target different languages at specific countries. Let’s say you are based in Europe, have English as the default language, and two additional languages: Dutch and French. These two additional languages are primarily for website visitors in The Netherlands and France (yes, we know Dutch is also spoken in northern Belgium, and French also in parts of Switzerland, we’ll come to that later).

Your main website is already listed in Webmaster Tools. Add your language specific sites separately, so with the above mentioned languages you add mywebsite.com/nl/ and mywebsite.com/fr/ (or, if you have subdomains, nl.mywebsite.com and fr.mywebsite.com).

Once you’ve done that, you can select a GeoTarget under ‘Improve the search presence of http://mywebsite.com/nl/’. In this case, you would select the target country as The Netherlands. It will take some time, but after a while your website in that specific language should rank higher in google.nl.

Of course you should submit sitemaps for each language separately. How you can do that, we have covered in a previous post: Yoast WordPress SEO: Sitemap for each language.

Yoast WordPress SEO: Sitemap for each language

The WordPress SEO plugin by Yoast is very popular (more than 1 million active installs), including the functionality to add XML sitemaps to your website.

If you have a multi-lingual website, it’s not so clear how to get separate sitemaps for each language. If you use WPML, out of the box it is only possible if you have different subdomains (e.g. en.mywebsite.com and fr.mywebsite.com) and in the WPML language settings select ‘a different domain for each language’.

But what if you have different languages in subfolders (e.g. mywebsite.com/en/ and mywebsite.com/fr/)?

Well, it’s not so difficult at all. Add the following code into your functions.php file of your WordPress theme:


if (isset($sitepress)) add_filter('wpseo_posts_join', 'sitemap_per_language', 10, 2);
function sitemap_per_language($join, $type) {
    global $wpdb, $sitepress;
    $lang = $sitepress->get_current_language();
    return " JOIN " . $wpdb->prefix . "icl_translations ON element_id = ID AND element_type = 'post_$type' AND language_code = '$lang'";
}

The XML sitemap for your default language is still in the same place (mywebsite.com/sitemap_index.xml). Your other languages don’t have an ‘index.xml’, but separate sitemaps, like ‘mywebsite.com/fr/post-sitemap.xml‘ and ‘mywebsite.com/fr/page-sitemap.xml‘.

So, if you want to submit your sitemaps in Google Webmaster Tools you have to add ‘sitemap_index.xml’ for your default language, but for your other languages you have to add multiple XML sitemaps as described above.

This comes in handy when you want to set different geotargets for your languages.

Gravity Forms Datepicker: restrict dates

Have you ever felt the need to disable certain dates/times within the date-picker of Gravity Forms? You cannot do that in the back-end. There are some plugins promising that, but this plugin only allows you to restrict the range to fixed dates. That’s not very useful when you want to restrict e.g. to today and 1 year ahead.

You can insert a code in the header of the page, like this:

jQuery(document).ready(function($) {
    $(".datepicker").datepicker({ minDate: 0 });
});

But hey, did you notice? Your calendar button (the small icon next to the input field) disappears! So, we also have to add the path to the button image again (make sure the path is correct)

jQuery(document).ready(function($) {
		$( "#input_6_3-0" ).datepicker({  buttonImage: 'images/calendar.gif', minDate: '+1d' });
  });

Another Simple Solution

Even with the above code, sometimes the calendar button just will not show up, although you made absolutely sure the path to the image file is correct. I haven't yet found out why that is. In that case you have to resort to editing a core file in Gravity Forms:
wp-content/plugins/gravityforms/js/datepicker.js

Don't worry, it's very simple, change this (on line 9 and 10):

yearRange: '-100:+20',
showOn: 'focus',

to this:

yearRange: '0:+1',
minDate: '+1w',
maxDate: '+1y',
showOn: 'focus',

It's pretty self explanatory: a user can only select the current year and the next, only a date 1 week from today, and the maximum date a user is able to select is 1 year from today.

Of course this file will be overwritten every time you update Gravity Forms, but because the change is so simple (less than 1 minute work), you'll just have to make a note that you should apply the above change after an update.