custom post type archive page url to point to Page permalink

admin2025-01-07  6

I wish that my 'custom post type archive page' would point to Page.

The situations is as follows:

I have Page in Wordpress with permalink . This page shows list of posts with custom post type = vmgallery. I have custom logic for this page that is working fine.

From another view, this page works like "custom post type archive page", because it displays all posts for given custom post type vmgallery. Currently, if user goes to / wordpress is loading archive page (archive.php), instead, I wish page is loaded.

How to achieve this?

I wish that my 'custom post type archive page' would point to Page.

The situations is as follows:

I have Page in Wordpress with permalink http://myurl.com/galleries. This page shows list of posts with custom post type = vmgallery. I have custom logic for this page that is working fine.

From another view, this page works like "custom post type archive page", because it displays all posts for given custom post type vmgallery. Currently, if user goes to http://myurl.com/vmgallery/ wordpress is loading archive page (archive.php), instead, I wish http://myurl.com/galleries page is loaded.

How to achieve this?

Share Improve this question edited Mar 1, 2016 at 11:49 renathy asked Mar 1, 2016 at 11:43 renathyrenathy 2131 gold badge4 silver badges16 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 12

You have multiple options here.

1. Define post type archive slug on post type registration

By setting 'has_archive' => 'galleries' you can define custom post type archive slug. Check documentation. Then you can delete your page "galleries" then add & customize the archive-post_type.php

2. Disable default archive for post type

Disable the archive by setting 'has_archive' => false then keep the page for the post type archive.

3. Redirect archive requests to your page

You can permanent redirect default archive request to your page.

function archive_to_custom_archive() {
    if( is_post_type_archive( 'post_slug' ) ) {
        wp_redirect( home_url( '/galleries/' ), 301 );
        exit();
    }
}
add_action( 'template_redirect', 'archive_to_custom_archive' );

I will say the first method is good!

Here is below code to remove archive page link with not affecting in single post url.and you can create page with archive slug.

function dg_custom_post_type_args( $args, $post_type ) {
if ( $post_type === "custom post type" ) {
    $args['rewrite'] = array(
    'with_front' => false,
    'slug' => 'slug here'
  );
}

return $args;
}
add_filter( 'register_post_type_args', 'dg_custom_post_type_args', 20, 2 );

This code solved my problem.

function redirect_cpt_archive() {
    if( is_post_type_archive( 'galleries' ) ) {
        wp_redirect( home_url( '/galleries/post-url/' ), 301 );
        exit();
    }
}
add_action( 'template_redirect', 'redirect_cpt_archive' );

In your custom post type in functions.php you must update 'has_archive' => true.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736252640a61.html

最新回复(0)