Welcome to WebmasterWorld Guest from 54.167.58.159

Forum Moderators: rogerd & travelin cat

Message Too Old, No Replies

Altering get template part

wordpress 'basic' template

     

digic

12:32 pm on Nov 16, 2012 (gmt 0)

5+ Year Member



Hi there.

In my single.php file, I have this code to call the contents of a post.

<?php get_template_part( 'includes/loop' , 'single'); ?>


Can I alter it to show different contents/templates based on the given TAGS for posts?

Say, I want to get the template loop-cat (includes/loop-cat.php) if post has 'cat' Tag. Same goes for the other template. Most propably I will have at least 10 templates to show. Plus, the default post or without Tag will use the default template.

I dont want to merge them in a single loop file because I am worried that the file may get heavy.

Please help me. If there are other better ways to call different templates for different tags, please let me know.

In my previous theme, things are different. I can call each template by replacing the single.php contents with this:

<?php
$post = $wp_query->post;

if (has_tag('dog')) {
include(TEMPLATEPATH.'/dog.php');

} elseif (has_tag('cat')) {
include(TEMPLATEPATH.'/cat.php');

} else {
include(TEMPLATEPATH.'/single-def.php');
}
?>


However, I find that it is not applicable to the Basic theme.

Thanks a lot.

lorax

1:33 pm on Nov 16, 2012 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Hi digic,
I don't have a clear answer for you but technically I don't see a reason WP can't do what you're asking it to do. Especially since you were able to do it with the previous theme.

I am curious why you're using tags to drive this. I assume there's some need for the flexibility the tags offer over evaluating for categories and delivering a specific theme.

digic

2:17 pm on Nov 16, 2012 (gmt 0)

5+ Year Member



Hi Lorax,

Thanks for the reply. Technically, my previous theme is calling the content from single.php file. While the new one is not.

I know that there are some coding works to achieve this in WP, however I am not a capable of doing so. This is the reason why I am seeking help for those who can understand how WP templating works. I have tried reading and applying some techniques from WP codex, but I cannot get this thing to work.

I am using this simply to apply different themes/layout for specific post. Afterall I am not using TAG function of WP. Category is already set for each articles so I may not be able to use the 'Category Template' function. Besides I have posts in the same Category that needs to be shown in different layout.


My question is basically on editing or using the right code for the get_template_part function to call different file as per Tag of the post.

lorax

8:11 pm on Nov 16, 2012 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Okay. I assume you've looked at the Template Hierarchy page -> [codex.wordpress.org...] which seems to have the load order of the templates and such?

digic

1:06 am on Nov 18, 2012 (gmt 0)

5+ Year Member



Yes, I already looked at that. As Ive said, I am not PHP guy so nothing syncs in. Also, template_part is not discussed on that either.

lorax

12:56 pm on Nov 19, 2012 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



I'd recommend you find someone that is WordPress coder to help you.