0

I Registered a New CPT , and it did works but , after working on something different , when i refreshed the page , i realized that my CPT turn to work with index.php . i think all is fine with my code . here it is .

function mn_portfolio_post_type_init() {

$labels = array(

    // Labels .

);

$portfolioargs = array(

    'labels'                => $labels,
    'public'                => true,
    'has_archive'           => true,
    'query_var'             => true,
    'publicly_queryable'    => true,
    'rewrite'               => true,
    'supports'              => array(
        'title',
        'editor',
        'thumbnail',
        'author',
        'excerpt',
        'custom-fields',
        'revisions'
    ),
    'hierarchical'          => false,
    'menu_position'         => 5,
    'menu_icon'             => 'dashicons-media-document',
    'capability_type'       => 'post'

);

  register_post_type('portfolio', $portfolioargs);

}

add_action('init', 'mn_portfolio_post_type_init');

After That I Used a new page-name.php to work with , and all was fine , but for now i'm out of ideas for help me out of that .

1

It looks like your theme is missing certain key files that are needed in order to keep WordPress from defaulting to index.php per the WordPress Template Hierarchy.

Since you have defined your portfolio post type to act like posts by setting 'hierarchical' to false, you should probably be using the post custom template files.

WordPress Theme Template Hierarchy

In reading the chart from left to right, you want to create one, possible two files depending if you want the index view to be custom as well as the single post view. For the single view, WordPress uses single-$posttype.php where $posttype is replaced by the slug you gave your custom post type. In your case this file would be single-portfolio.php. The archive page is a similar naming convention with archive-$posttype.php so you would make archive-portfolio.php

Pages do not have support for this. With a page you can pretty much only use page-$id.php or page-$slug.php.

  • thanks man for that amazing informations , if can i ask you Please , for that CPT i Used a page called page-portfolio.php and then i gave it a template name which is => Portfolio template , and then i went to the pages and picked a page called portfolio and gave the template with the name . so now is this can work or not , actually what i didn't know is all that worked for a while and then turned to work with index.php – Simo Patrek Jan 14 '18 at 0:09
  • page-portfolio.php will only work on a single page called "Portfolio" because its slug will be "portfolio". Otherwise WordPress will default back to index.php for everything else. – Cedon Jan 14 '18 at 0:28
  • Remember, when WordPress processes a request, it will go from left to right on that graphic. If it finds a template file that matches what kind of content it's looking for, such as single-portfolio.php, it will stop and load that file. Otherwise it will keep going until only index.php is left and use that. – Cedon Jan 14 '18 at 0:30
  • Did you know there's an archive at yoursite.com/portfolio/ already? You don't need to construct a page and assign it a template to list your portfolio items. WP already created an archive for you and tries to load a special portfolio archive template. You just haven't created it, that's why everybody is pointing you towards the template hierarchy diagram – Tom J Nowell Jan 14 '18 at 2:15
  • Thanks a Lot A lot alot for your Support , i learned so much informations from this little problem , now i get it , everytime i want to create a CPT , i should give it an archive page e.g archive-movies.php , and give it a single page which is e.g single-movires , such as it explained in the graphic that you gaved me . thanks so much . – Simo Patrek Jan 14 '18 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.