homepage Welcome to WebmasterWorld Guest from 54.237.249.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / WordPress
Forum Library, Charter, Moderators: lorax & rogerd

WordPress Forum

    
Altering get template part
wordpress 'basic' template
digic




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

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




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

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




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

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




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

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

digic




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

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




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / WordPress
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved