2011-08-30 22 views
5

में कस्टम पोस्ट प्रकारों के लिए टेम्पलेट बनाएं मुझे पता है कि किसी विशिष्ट पृष्ठ के लिए कस्टम टेम्पलेट कैसे बनाएं। हालांकि मैं एक विशिष्ट कस्टम पोस्ट प्रकार के लिए एक टेम्पलेट बनाना चाहता हूं। क्या यह संभव है और यदि सच है तो मैं यह कैसे कर सकता हूं?वर्डप्रेस

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

समस्या हल हो:

/* 
Show the list of available custom templates templates in the Custom Post Type admin section 
*/ 

/** 
* Post_type 
*/ 
define('MY_THEME_POST_TYPE', 'cases'); 
/** 
* Load the page template for any post object 
* having the appropriate meta key set. 
*/ 
add_action('template_redirect', 'mytheme_template_redirect'); 
function mytheme_template_redirect() { 
    global $wp_query; 
    $id = (int) $wp_query->get_queried_object_id(); 
    $template = get_post_meta($id, '_wp_page_template', true); 
    if ($template && 'default' !== $template) { 
     $file = STYLESHEETPATH . '/' . $template; 
     if(is_file($file)) { 
      require_once $file; 
      exit; 
     } 
    } 

} 
/** 
* Process the Meta Box 
* @todo Permissions check. 
* @todo Filter input. 
* @todo Nonces. 
*/ 
add_action('save_post', 'mytheme_process_resource_template'); 
function mytheme_process_resource_template() { 
    global $post; 

    /* Sanitize $_POST array. */ 
    $clean_id = (isset($_POST['ID'])) ? intval($_POST['ID']) : 0; 

    if (!empty($_POST['page_template']) && MY_THEME_POST_TYPE == $post->post_type) { 
     $page_templates = get_page_templates(); 
     if ('default' != $page_template && !in_array($_POST['page_template'], $page_templates)) { 
      if ($wp_error) 
       return new WP_Error('invalid_page_template', __('The page template is invalid.')); 
      else 
       return 0; 
     } 
     update_post_meta($clean_id, '_wp_page_template', $_POST['page_template']); 
    } 
} 
/** 
* Registers the Meta Box 
* @uses mytheme_page_attributes_meta_box() 
*/ 
add_action('admin_init', 'mytheme_register_meta_boxes', 10); 
function mytheme_register_meta_boxes() { 
    add_meta_box(
     'mytheme_post_type_template', 
     'Template', 
     'mytheme_page_attributes_meta_box', 
     MY_THEME_POST_TYPE, 
     'side', 
     'low' 
     ); 
} 
/** 
* Creates the Meta Box 
*/ 
function mytheme_page_attributes_meta_box() { 
    global $post; 
    $post_type_object = get_post_type_object($post->post_type);  
    if (0 != count(get_page_templates())) { 
     $template = get_post_meta($post->ID, '_wp_page_template', true); 
     ?> 
<p><strong><?php _e('Template') ?></strong></p> 
<label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label><select name="page_template" id="page_template"> 
<option value='default'><?php _e('Default Template'); ?></option> 
<?php page_template_dropdown($template); ?> 
</select> 
<?php 
    } 
} 
+0

पर अभी तक परीक्षण नहीं किया गया है, लेकिन कस्टम प्लग_टाइप के लिए टेम्पलेट को सक्षम करने के लिए कुछ प्लगइन्स हैं: http://wordpress.org/plugins/custom-post-template/ http: // wordpress .org/plugins/custom-post-type-page-template/ –

उत्तर

17

बनाएं पेज कि कहा जाता है:

एकल {CPT-स्लग} .php उदा सिंगल-product.php

कस्टम पोस्ट प्रकार का एक पृष्ठ दिखाते समय इसका उपयोग किया जाएगा। यानी जब कोई http://example.com/product/awesome-shoes/

+0

किस फ़ोल्डर में मुझे यह टेम्पलेट फ़ाइल रखना है? –

+2

थीम रूट। यदि आपकी थीम 'वर्डप्रेस/डब्ल्यूपी-कंटेंट/थीम/कमाल-थीम/सिंगल-product.php' से' कमाल-थीम 'है। –