working ant
working ant

Reputation: 85

How to make a template for my custom type?

I have created an custom post type inside functions and added some stuff through ACF in to it. I tried first to create an archive-forfragningar.php but it didn't work. Then inside my index.php i added php if is but i can not target my custom post type with is_post_type_archive. When i go to my archive page i get instead code that is inside else index.php

my custome post type

function create_forfragningar_cpt() {
  register_post_type( 'forfragningar', 
    array(
      'public' => true,
      'has_archive' => true,
      'menu_position' => 5, // places menu item directly below Posts
      'menu_icon' => '',
      'labels' => array(
        'name' => __( 'Förfrågningar' ),
        'singular_name' => __( 'Förfrågning' ),
        'add_new' => __( 'Lägg till' ),
        'add_new_item' => __( 'Lägg till nytt Förfrågning' ),
        'edit' => __( 'Redigera' ),
        'edit_item' => __( 'Redigera Förfrågning' ),
        'new_item' => __( 'Nytt Förfrågning' ),
        'view' => __( 'Visa Förfrågning' ),
        'view_item' => __( 'Visa Förfrågning' ),
        'search_items' => __( 'Sök igenom förfrågningar' ),
        'not_found' => __( 'Inga förfrågningar hittade' ),
        'not_found_in_trash' => __( 'Inga förfrågningar hittade i Papperskorgen' ),
        'parent' => __( 'Föräldrer av Förfrågning' ),
        ),
    )
  );
}
add_action( 'init', 'create_forfragningar_cpt' );


index.php

<?php if(is_front_page()){ ?>

//code

<?php } if ( is_post_type_archive('forfragningar') ) { ?>

<h1>Test</h1>
//code

<?php } else { ?>

//code

<?php } ?>

Upvotes: 1

Views: 164

Answers (3)

Matej Đaković
Matej Đaković

Reputation: 880

You can do that easy on this way, just duplicate single.php and rename to single-custom-post-type-name.php like single-cars.php, same thing with archive or taxonomy, taxonomy-taxonomy-name.php or archive-taxonomy-name.php

Upvotes: 3

Pulkit Ambliya
Pulkit Ambliya

Reputation: 46

Try the following code. Make php file like single-{post_type}.php

Now finally your file name is single-forfragningar.php

Make file single-forfragningar.php to your root directory

Upvotes: 1

Thanassis
Thanassis

Reputation: 877

If you want to render a specific archive page for custom post type you need to understand Template Hierarchy works. I also use this image.

In your case you have to create archive-forfragningar.php or single-forfragningar.php and insert your code inside these files. How to make custom post type template. Take a look of this also.
You don't need if ( is_post_type_archive('forfragningar') ) inside your template because wordpress is already to this custom type. You can use is_post_type_archive('forfragningar') , in non forfragningar custom post type templates file, to check if you are in the query of your custom post type and make a specific work to your case.
For example the want to render <h1> tiltle </h1> to your header in your forfragningar page. What you will do is:

insert in your header.php

if ( is_post_type_archive('forfragningar') ) { ?>

<h1>Test</h1>
//code

<?php } else { ?>

//code

<?php } ?>

and then in your custom type template render your header with this get_header(); What this will do is create your header for your custom type and your header will run specific code when wordpress runs your forfragningar type.

Upvotes: 1

Related Questions