最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Change archive page template using pre_get_post

programmeradmin1浏览0评论

I've used pre_get_post to modify and display my custom post type in an archive page with pagination which uses my custom created archive template, see here. It it displaying the posts exactly what I need, however, it seems like it is not using my custom template. It is using the default template even though I already set a custom page template in my page settings. It is like this when in default template, and should be like this when using custom template.

Does everyone know how I can change the page template to my custom one when using pre_get_post?

I have tried the archive_videos_template() custom function that I found on this link, which you can see in this image but it is not working.

Please let me know if you have any idea how to do this because I wanted to modify the template of my custom post type in a page.

Thanks

Here's my initial code in function.php to display custom post type in archive page with pagination and the other function that I tried to change page template.

function post_type_videos( $query )
    {
      if ( ! is_admin() && is_post_type_archive( 'videos_cpt' ) && $query->is_main_query() )
      {
        $query->set( 'post_type', 'videos' ); //set query arg ( key, value )
        $query->set( 'posts_per_page', 2 ); //set query arg ( key, value )
        return $query;

        // custom page template
        add_filter( 'template_include', 'archive_videos_template', 99 );
      }
    }

    add_action( 'pre_get_posts' ,'post_type_videos', 1, 1 );

    function archive_videos_template( $template )
    {
        $target_tpl = 'archive-videos_cpt.php'; // EDIT to your needs

        remove_filter( 'template_include', 'archive_videos_template', 99 );

        $new_template = locate_template( array( $target_tpl ) );

        if ( ! empty( $new_template ) )
            $template = $new_template;;
    }

        return $template;
    }

I've used pre_get_post to modify and display my custom post type in an archive page with pagination which uses my custom created archive template, see here. It it displaying the posts exactly what I need, however, it seems like it is not using my custom template. It is using the default template even though I already set a custom page template in my page settings. It is like this when in default template, and should be like this when using custom template.

Does everyone know how I can change the page template to my custom one when using pre_get_post?

I have tried the archive_videos_template() custom function that I found on this link, which you can see in this image but it is not working.

Please let me know if you have any idea how to do this because I wanted to modify the template of my custom post type in a page.

Thanks

Here's my initial code in function.php to display custom post type in archive page with pagination and the other function that I tried to change page template.

function post_type_videos( $query )
    {
      if ( ! is_admin() && is_post_type_archive( 'videos_cpt' ) && $query->is_main_query() )
      {
        $query->set( 'post_type', 'videos' ); //set query arg ( key, value )
        $query->set( 'posts_per_page', 2 ); //set query arg ( key, value )
        return $query;

        // custom page template
        add_filter( 'template_include', 'archive_videos_template', 99 );
      }
    }

    add_action( 'pre_get_posts' ,'post_type_videos', 1, 1 );

    function archive_videos_template( $template )
    {
        $target_tpl = 'archive-videos_cpt.php'; // EDIT to your needs

        remove_filter( 'template_include', 'archive_videos_template', 99 );

        $new_template = locate_template( array( $target_tpl ) );

        if ( ! empty( $new_template ) )
            $template = $new_template;;
    }

        return $template;
    }
Share Improve this question edited Jun 23, 2020 at 12:54 Jim asked Jun 23, 2020 at 11:01 JimJim 134 bronze badges 6
  • 1 Hello! Your code is in an image, please edit your question using the edit link to put your code in a code block, not a screenshot. In order to debug this I would have to type it all out manually, and I don't have the time to do that. Also can you clarify what you're trying to do that this solves? Why couldn't you use archive-video_cpt.php? There's a screenshot of the block editor in your question but I don't understand why it's there. – Tom J Nowell Commented Jun 23, 2020 at 12:12
  • oh sorry. I edited my post and pasted the code. thank you for the reply – Jim Commented Jun 23, 2020 at 12:20
  • the screenshot of block editor shows the template that i used in that page which is the archive-video_cpt.php that I custom created, however, it is not being used when I try to display posts using pre_get_post. It is using the default template. I am not sure why – Jim Commented Jun 23, 2020 at 12:32
  • You have in code add_filter( 'archive-videos', 'archive_video_template', 99 ) but there is no such filter. To filter the path of template use template_include filter hook, so change this line to add_filter( 'template_include', 'archive_video_template', 99 ). You can also try with the archive_template filter. – nmr Commented Jun 23, 2020 at 12:37
  • oh, thanks, I'll try an get back to you – Jim Commented Jun 23, 2020 at 12:45
 |  Show 1 more comment

1 Answer 1

Reset to default 1

You corrected the filter hook name from archive-videos to template_include, but in the first function return $query; statement comes before add_filter(), so the filter is not added.

add_action( 'pre_get_posts' ,'post_type_videos' );

function post_type_videos( $query )
{
    if ( ! is_admin() && $query->is_post_type_archive( 'videos_cpt' ) && $query->is_main_query() )
    {
        $query->set( 'post_type', 'videos' ); //set query arg ( key, value )
        $query->set( 'posts_per_page', 2 ); //set query arg ( key, value )

        // custom page template
        add_filter( 'template_include', 'archive_videos_template', 99 );

        return $query;
    }
}

function archive_videos_template( $template )
{
    remove_filter( 'template_include', 'archive_videos_template', 99 );

    $target_tpl = 'archive-videos_cpt.php';
    $new_template = locate_template( array( $target_tpl ) );
    if ( ! empty( $new_template ) )
        $template = $new_template;;
    }

    return $template;
}
发布评论

评论列表(0)

  1. 暂无评论