कस्टम पोस्ट प्रकार एकल- {कस्टम} .php काम नहीं कर रहा है


19

मैंने मशीन नाम special_media_post के साथ एक कस्टम पोस्ट प्रकार बनाया है और वर्डप्रेस को केवल single-special_media_post.php नहीं देखा जा रहा है। मैं पूरी तरह से हार गया हूं। यह index.php को डिफ़ॉल्ट करता रहता है

यहाँ मेरे कस्टम पोस्ट प्रकार और इसके वर्गीकरण के लिए मेरा कोड है:

//Post and Taxonomy stuff
//Register Custom Post Type
function special_media_post() {
$labels = array(
    'name'                => _x( 'Media Posts', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Media Post', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Media Post', 'text_domain' ),
    'parent_item_colon'   => __( 'Media Post:', 'text_domain' ),
    'all_items'           => __( 'All Media Posts', 'text_domain' ),
    'view_item'           => __( 'View Media Post', 'text_domain' ),
    'add_new_item'        => __( 'Add New Media Post', 'text_domain' ),
    'add_new'             => __( 'New Media Post', 'text_domain' ),
    'edit_item'           => __( 'Edit Media Post', 'text_domain' ),
    'update_item'         => __( 'Update Media Post', 'text_domain' ),
    'search_items'        => __( 'Search Media Posts', 'text_domain' ),
    'not_found'           => __( 'No media posts found', 'text_domain' ),
    'not_found_in_trash'  => __( 'No media posts found in Trash', 'text_domain' ),
);

$rewrite = array(
    'slug'                => 'mediapost',
    'with_front'          => true,
    'pages'               => true,
    'feeds'               => true,
);

$args = array(
    'label'               => __( 'mediapost', 'text_domain' ),
    'description'         => __( 'Post Type for Media', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'custom-fields', ),
    'taxonomies'          => array( 'year', 'type' ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'query_var'           => 'mediapost',
    'rewrite'             => $rewrite,
    'capability_type'     => 'page',
);

register_post_type( 'special_media_post', $args );
}

// Register Custom Taxonomy
function media_year()  {
$labels = array(
    'name'                       => _x( 'Years', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Year', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Year', 'text_domain' ),
    'all_items'                  => __( 'All Years', 'text_domain' ),
    'parent_item'                => __( 'Parent Year', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Year:', 'text_domain' ),
    'new_item_name'              => __( 'New Year Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Year', 'text_domain' ),
    'edit_item'                  => __( 'Edit Year', 'text_domain' ),
    'update_item'                => __( 'Update Year', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate years with commas', 'text_domain' ),
    'search_items'               => __( 'Search years', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove years', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used yearss', 'text_domain' ),
);

$rewrite = array(
    'slug'                       => 'year',
    'with_front'                 => true,
    'hierarchical'               => true,
);

$capabilities = array(
    'manage_terms'               => 'manage_categories',
    'edit_terms'                 => 'manage_categories',
    'delete_terms'               => 'manage_categories',
    'assign_terms'               => 'edit_posts',
);

$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'query_var'                  => 'year',
    'rewrite'                    => $rewrite,
    'capabilities'               => $capabilities,
);

register_taxonomy( 'year', 'special_media_post', $args );
}

// Register Custom Taxonomy
function media_type()  {
$labels = array(
    'name'                       => _x( 'Types', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Type', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Type', 'text_domain' ),
    'all_items'                  => __( 'All Types', 'text_domain' ),
    'parent_item'                => __( 'Parent Type', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Type:', 'text_domain' ),
    'new_item_name'              => __( 'New Type Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Type', 'text_domain' ),
    'edit_item'                  => __( 'Edit Type', 'text_domain' ),
    'update_item'                => __( 'Update Type', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate types with commas', 'text_domain' ),
    'search_items'               => __( 'Search types', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove types', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used types', 'text_domain' ),
);

$rewrite = array(
    'slug'                       => 'type',
    'with_front'                 => true,
    'hierarchical'               => true,
);

$capabilities = array(
    'manage_terms'               => 'manage_categories',
    'edit_terms'                 => 'manage_categories',
    'delete_terms'               => 'manage_categories',
    'assign_terms'               => 'edit_posts',
);

$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'query_var'                  => 'media_type',
    'rewrite'                    => $rewrite,
    'capabilities'               => $capabilities,
);

register_taxonomy( 'type', 'special_media_post', $args );
}

// Hook into the 'init' action
add_action( 'init', 'special_media_post', 0 );

// Hook into the 'init' action
add_action( 'init', 'media_year', 0 );

// Hook into the 'init' action
add_action( 'init', 'media_type', 0 );

अगर आपको कुछ और देखना है, तो मैं इसे लगा सकता हूं, लेकिन अगर मैंने वहां पर 'हैलो वर्ल्ड' की गूंज लगाई, तो यह भी दिखाई नहीं देता। तो यह केवल एकल-special_media_post.php या संग्रह-special_media_post.php नहीं देख रहा है

जवाबों:


57

पर्मलिंक्स पेज पर जाएं (जो इसे फ्लश करेगा) और फिर से जांचें। वर्डप्रेस को शायद पदानुक्रम के अलावा अपने पहचान को पहचानने की ज़रूरत है।


2
वाह! आकर्षण की तरह काम करता है, 1 घंटे बर्बाद :(
मोहम्मद सूफियान

5

कोड बदलें

से:

 'has_archive'         => true,

सेवा :

 'has_archive'         => false,

और फिर पर्मलिंक पृष्ठ पर जाएं, डिफ़ॉल्ट में बदलें और अपने "सुंदर पर्मलिंक" पर वापस जाएं

%पोस्ट नाम%/

अब यह काम करना चाहिए।

इसका एकल-{custom_post_type} .php पृष्ठ पर न जाने का कारण has_archive के कारण है। जब has_archive सही पर सेट हो जाता है, तो यह एकल पेज के बजाय आर्काइव- {custom_post_type} .php के लिए दिखेगा।

आशा है कि यह काम किया।


1
मुझे यकीन नहीं है कि यह सच है। एकल पोस्ट दृश्य एकल पोस्ट दृश्य है, चाहे पोस्ट-प्रकार संग्रह अनुक्रमणिका दृश्य का समर्थन करता है या नहीं।
चिप बेनेट

यह समाधान मैंने पोस्ट किया है कि मैंने अपने स्वयं के विकसित विषय पर अपनी समस्या को कैसे हल किया। जब मेरा सिंगल पोस्ट टाइप पेज अब मुझे दिखाएगा कि मैं क्या देखना चाहता था।
वेस्ले चेउंग

3
यह संभव है कि फिक्स पर्मलिंक संरचना को रीसेट करने से है, जो बदले में फिर से लिखे गए नियमों को फ्लश करता है।
चिप बेनेट

2
'has_archive' सेट करने से घरघराहट पर कोई प्रभाव नहीं पड़ता है या एकल- {post_type} .php टेम्पलेट का उपयोग एकल अनुरोध के लिए नहीं किया जाएगा।
जूल्स १

एक जादू की तरह काम करता है। धन्यवाद दोस्त!
user1202416

1

यह एक अच्छा अभ्यास है register_activation_hook()और register_deactivation_hook()नई सामग्री प्रकार बनाते समय भी इसका उपयोग करता है ।

ऐसा लगता है कि नई नई सामग्री के प्रकार हमेशा नए सिरे से लिखे जाने में विफल रहेंगे। इससे बचने के लिए, register_activation_hook () पर कॉलबैक करें flush_rewrite_rules()और अपना पंजीकरण नया सामग्री फ़ंक्शन डालें । मुझे पता नहीं क्यों, लेकिन ऐसा करने से इस समस्या से बचने लगता है। देखो:

register_activation_hook( __FILE__, 'your_active_hook' );

function your_active_hook() {
    special_media_post();
    flush_rewrite_rules();
}

0

मैंने आपके कोड की प्रतिलिपि बनाई, व्यवस्थापक के माध्यम से फिर से लिखे गए नियमों को प्रवाहित किया और अब मीडिया पोस्ट पर जाने पर थीम सही टेम्प्लेट का उपयोग करता है।

After_switch_theme हुक का उपयोग करके आपको एक बार फिर से लिखना होगा । इससे यह सुनिश्चित हो जाएगा कि उपयोगकर्ता द्वारा थीम को सक्रिय करने के बाद पुन: लिखने के नियम स्वतः ही समाप्त हो जाएंगे।

आप इस कोड का उपयोग कर सकते हैं (कोडेक्स से सीधे):

add_action( 'init', 'theme_prefix_cpt_init' );
function theme_prefix_cpt_init() {
    register_post_type( ... );
}

function theme_prefix_rewrite_flush() {
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'theme_prefix_rewrite_flush' );

अधिक जानकारी के लिए कृपया वर्डप्रेस कोडेक्स से परामर्श करें: http://codex.wordpress.org/Function_Reference/register_pest_pype

EDIT: ऐसे मामले में, इंस्पेक्ट रिवीट रूल्स प्लग इन बहुत काम आता है, क्योंकि यह आपको अपने कस्टम पोस्ट प्रकार से जुड़े नियमों को देखने की अनुमति देता है: http://wordpress.org/extend/plugins/rewrite-rules-ppector/

कृपया ध्यान दें कि कस्टम पोस्ट प्रकार डालने की अनुशंसित जगह एक प्लगइन है, न कि कोई थीम।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.