2010-06-25 10 views
13

मैं डिफ़ॉल्ट टेम्पलेट पदानुक्रम व्यवहार को बदलना चाहता हूं, और सभी उपश्रेणी स्तर पृष्ठों को बल देना चाहता हूं जिनके पास अपनी मूल श्रेणी टेम्पलेट फ़ाइल नहीं है, ताकि वे अपनी मूल श्रेणी टेम्पलेट फ़ाइल को संदर्भित कर सकें। मेरी दूसरी पोस्ट में, Richard M. gave an excellent answer जिसने एक व्यक्तिगत उपश्रेणी के लिए समस्या हल की। क्या कोई जानता है कि इसे कैसे अमूर्त करना है?* सभी * वर्डप्रेस श्रेणियां अपने अभिभावक श्रेणी टेम्पलेट का उपयोग करें

function myTemplateSelect() 
{ 
    if (is_category()) { 
     if (is_category(get_cat_id('projects')) || cat_is_ancestor_of(get_cat_id('projects'), get_query_var('cat'))) { 
      load_template(TEMPLATEPATH . '/category-projects.php'); 
      exit; 
     } 
    } 
} 

add_action('template_redirect', 'myTemplateSelect'); 

अग्रिम धन्यवाद।

उत्तर

20
/** 
* Iterate up current category hierarchy until a template is found. 
* 
* @link http://stackoverflow.com/a/3120150/247223 
*/ 
function so_3119961_load_cat_parent_template($template) { 
    if (basename($template) === 'category.php') { // No custom template for this specific term, let's find it's parent 
     $term = get_queried_object(); 

     while ($term->parent) { 
      $term = get_category($term->parent); 

      if (! $term || is_wp_error($term)) 
       break; // No valid parent 

      if ($_template = locate_template("category-{$term->slug}.php")) { 
       // Found ya! Let's override $template and get outta here 
       $template = $_template; 
       break; 
      } 
     } 
    } 

    return $template; 
} 

add_filter('category_template', 'so_3119961_load_cat_parent_template'); 

यह तत्काल टेम्पलेट मिलने तक माता-पिता पदानुक्रम को रोक देता है।

+0

मैंने अभी कोशिश की और इसे काम नहीं कर सका। क्या आप इसे दोबारा जांचना चाहते हैं? – Matrym

+2

'TEMPLATEPATH'' TEMPLATE_PATH' –

+0

की जगह अच्छी जगह - अपडेट किया गया :) – TheDeadMedic

2

TEMPLATEPATH चर बाल विषयों के लिए काम नहीं कर सकता है - मूल थीम फ़ोल्डर में दिखता है। इसके बजाय स्टाइलशिपेट का उपयोग करें। जैसे

$template = STYLESHEETPATH . "/category-{$cat->slug}.php"; 
3

मैं सोच रहा था कि हेरार्किकल टैक्सोनोमीज़ के लिए एक ही चीज़ कैसे करें। TheDeadMedic का जवाब उस मामले में काम करता है, कुछ tweaks:

function load_tax_parent_template() { 
    global $wp_query; 

    if (!$wp_query->is_tax) 
     return true; // saves a bit of nesting 

    // get current category object 
    $tax = $wp_query->get_queried_object(); 

    // trace back the parent hierarchy and locate a template 
    while ($tax && !is_wp_error($tax)) { 
     $template = STYLESHEETPATH . "/taxonomy-{$tax->slug}.php"; 

     if (file_exists($template)) { 
      load_template($template); 
      exit; 
     } 

     $tax = $tax->parent ? get_term($tax->parent, $tax->taxonomy) : false; 
    } 
} 
add_action('template_redirect', 'load_tax_parent_template'); 
संबंधित मुद्दे