404 error - An empty Custom Post Type Archive page returns 404 in WP 3.1

I have set up a Custom Post Type called "Portfolio". If I view the CPT's Archive page without adding any

I have set up a Custom Post Type called "Portfolio". If I view the CPT's Archive page without adding any posts to it (/portfolio or /?post_type=portfolio), it returns a 404 page. Is there a way to show the archive page but personalize the message? Ie "there are currently no portfolio items here right now".

As soon as I add 1 post into the portfolio custom post type, the archive page loads fine.

Here are other posts that match my issue

Custom Post Type Archives with 0 Posts Redirects as 404

I have set up a Custom Post Type called "Portfolio". If I view the CPT's Archive page without adding any posts to it (/portfolio or /?post_type=portfolio), it returns a 404 page. Is there a way to show the archive page but personalize the message? Ie "there are currently no portfolio items here right now".

As soon as I add 1 post into the portfolio custom post type, the archive page loads fine.

Here are other posts that match my issue

http://wordpress/support/topic/empty-archives-returning-404

Custom Post Type Archives with 0 Posts Redirects as 404

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Apr 30, 2011 at 0:24 AndrewAndrew 1,1633 gold badges24 silver badges47 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

what have you set the "has_archive" option to in your CPT? Also, make sure to reset your permalinks.

As for putting a customized message, you can create an archive template specific to that CPT. If the CPT is "portfolio", then you can create a template called "archive-portfolio.php" and customize it as you see fit.

I also had this issue when the filters on the archive page are getting empty results.

function empty_archive_404_template( $template = '' ){
    global $wp_query;
    if ( isset( $wp_query->query['YOUR_TAXONOMY_NAME'] ) ) {
        if ( isset( $wp_query->query['post_type'] ) ) {
            $located = locate_template( 'archive-' . $wp_query->query['post_type'] . '.php', false );
            $template = $located !== '' ? $located : locate_template( 'archive.php', false );
        } else {
            $template = locate_template( 'archive.php', false );
        }

    }
    return $template;
}
add_filter( '404_template', 'empty_archive_404_template' );

The function will check if the query is for a specific taxonomy YOUR_TAXONOMY_NAME replace this part with the slug of your taxonomy. Then we check the current post type to check if there is a specific template file. If there is none we will default back to the archive.php. If you add this function to your functions.php it will stay on the archive template when your taxonomy has no results.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745229322a4617607.html

相关推荐

  • 404 error - An empty Custom Post Type Archive page returns 404 in WP 3.1

    I have set up a Custom Post Type called "Portfolio". If I view the CPT's Archive page without adding any

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信