0

I made a custom post type , and an archive page for the post type. I don't know if there is a url where I can access this archive page? I would like to have a page where all the "posts" (products) for this archive is listed. But they don't need to be organized into post dates (what month and day).

Here is the code

function register_products_post_type() {
    $labels_array = [
        'name'  =>  __( 'Produkter', 'Divi' ),
        'singular_name'      => _x( 'Produkt', 'post type singular name', 'Divi' ),
        'menu_name'          => _x( 'Products', 'admin menu', 'Divi' ),
        'name_admin_bar'     => _x( 'Product', 'add new on admin bar', 'Divi' ),
        'add_new'            => _x( 'Add New', 'product', 'Divi' ),
        'add_new_item'       => __( 'Add New Product', 'Divi' ),
        'new_item'           => __( 'New Product', 'Divi' ),
        'edit_item'          => __( 'Edit Product', 'Divi' ),
        'view_item'          => __( 'View Product', 'Divi' ),
        'all_items'          => __( 'All Products', 'Divi' ),
        'search_items'       => __( 'Search Products', 'Divi' ),
        'parent_item_colon'  => __( 'Parent Products:', 'Divi' ),
        'not_found'          => __( 'No products found.', 'Divi' ),
        'not_found_in_trash' => __( 'No products found in Trash.', 'Divi' )

    ];

    $args = array(
        'public' => true,
        'labels'  => $labels_array,
        'description'        => __( 'beskrivning.', 'Divi' ),
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'products', $args );
}
add_action( 'init', 'register_products_post_type' );

I have an archive.php and an archive-products.php. So the main thing I need is a url to the archive page for the post type. Then I need to edit that archive to display the products.

edit: when I try to access the page "/products" I reach a 404 page.

Another way I suppose would be to create a normal custom page where I would list the products, and use it as an "archive". I don't know if that would be the better alternative.

1 Answer 1

2

First of all, before you do anything, make sure that you have flushed your permalinks, a 404 usually is a hint that you did not flush your permalinks after you created your custom post type the first time around.

get_post_type_archive_link() will return the URL to the post type archive page, so you would do the following

echo get_post_type_archive_link( 'products' );

to get the correct URL

As for a custom template, you can create an archive template called archive-products.php to display your products on. WordPress will automatically pick that template up and use it when you visit the products archive page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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