WordPress Custom Post Type Single View Not Working - 404 Error

I created a custom post type in WordPress and I can see all the posts on my archive page. But when I try to click on individual post links, I get a 404 error page. The URL seems to be getting duplicated somehow.

Here’s my custom post type code:

add_action('init', 'setup_gallery_type');

function setup_gallery_type(){
    $gallery_settings = array(
        'label' => _('Gallery'),
        'singular_label' => _('Gallery Item'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => true,
        'supports' => array('title', 'editor', 'thumbnail')
    );
    
    register_post_type('gallery', $gallery_settings);
}

My template file looks like this:

<?php
/*
Template Name: Gallery Display
*/
get_header(); ?>

<?php
$query_params = array(
    'post_type' => 'gallery',
    'post_status' => 'publish'
);

$gallery_query = new WP_Query($query_params);

if($gallery_query->have_posts()) :
    while($gallery_query->have_posts()) : $gallery_query->the_post();
?>
        <h2><a href="<?php echo get_post_permalink() ?>"><?php echo the_title();?></a></h2>
        <?php the_post_thumbnail('medium'); ?>
        <?php the_excerpt(); ?>
<?php
    endwhile;
else :
?>
    <p>No gallery items found.</p>
<?php
endif;
get_footer();
?>

When I hover over a post title link, it shows: mysite.com/wp/gallery/summer-photos

But clicking it takes me to: mysite.com/wp/gallery/summer-photos/summer-photos/

Why is the slug getting repeated twice in the URL? How can I fix this single post view issue?

This duplicate slug issue arises when your WordPress permalinks conflict with custom post type rewrite rules. I’ve faced this similar problem before; it often occurs if you have a page and a custom post type using the same slug. First, check if there’s a page named ‘gallery’ in your WordPress admin. If it exists, consider renaming it or changing your custom post type slug to ‘galleries’ or ‘photo-gallery.’ Also, modify your rewrite setting to: ‘rewrite’ => array(‘slug’ => ‘gallery’, ‘with_front’ => false). After this, navigate to Settings > Permalinks and click ‘Save Changes’ to flush the rewrite rules, which should resolve the 404 errors and eliminate the duplicate URL issue.

you’re missing flush_rewrite_rules() after registering your post type. add it right after the register_post_type line in your function. the url duplication happens when wordpress gets confused about rewrite rules and tries to match patterns twice.

This happens because WordPress can’t find the right template for your custom post type. You’re missing the single template file - WordPress needs either single-gallery.php or single.php to show individual posts, but you’ve only got the archive template. Create single-gallery.php in your active theme folder with standard single post template code. Also fix your register_post_type function - use 'rewrite' => array('slug' => 'gallery') instead of just 'rewrite' => true. Once you’ve done that, go to WordPress admin > permalinks and hit save to refresh the rewrite rules. That’ll fix both the 404s and duplicate URL issues.