Hook into loop templates examples

This is a follow up how to on this topic: Hook into loop templates

How do I get the template name?Copy and paste the following php call to get the template name: 

tk_loop_designer_get_template_slug(); // will return the current loop template slug.

What means slug?

Example: If your Template name is "template 1" the slug will be "template-1".

Some examples:

I will make some general examples at this place.This should be enough to get you into the thing. There are countless of template tags available, like get_the_title($post->ID); You can find out easily what template tag fits your needs by googling for it. ;-)

Display post content from the post:Example for Post Creation Date: 

<?php<br><p>function your_function(){</p><p>global $post;</p><p>echo get_the_time('Y-m-d', $post->ID);  // echo the date in Y-m-d format.</p><p>}</p><p>?></p>

Display custom post meta:  your_custom_meta should be replaced by the name of the custom field you want to display!

<p><?php</p><p>function your_function(){</p><p>global $post</p><p>$your_custom_meta = get_post_meta( $post->ID,'your_custom_meta', true );</p><p>if ( isset ( $your_custom_meta ) )</p><p>echo'<div>'.$your_custom_meta.'</div>';</p><p>}</p><p>?></p>

Add content only to a special template: If you want to hook into only one particular template you can make a check like this:

<p>if( tk_loop_designer_get_template_slug() != 'your-tempale-slug' )</p><p>return;</p>

Add content only to a custom post type:If you want to fire the action hook if a particular custom post type is displayed, use this: 

<p>if(get_post_type( $post ) != 'your-tempale-slug')</p><p>return;</p>

* of course you can combine all the statements. ;-) * 

Deregister HOOKSIn some situation you might want to deregister a hook, then just add this:

remove_action( 'tk_loop_designer_after_loop_title', 'the-function-name-you-want-to-deregister', 20 );
In this example you deregister the excerpt: 
remove_action( 'tk_loop_designer_after_loop_title', 'tk_loop_designer_list_posts_content', 20 );

Still need help? Contact Us Contact Us