"How to Build a WordPress Plugin" was retired on July 6, 2019.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Theme Development Custom Post Type Templates in WordPress The Portfolio Single Page

Joe Li
Joe Li
8,104 Points

404 after linking from custom post type

Hey Guys,

I'm hoping someone can help. I'm basically building out a recipes page. 'page-recipes.php' will hold and loop through all the recipes and 'single-recipes.php' will be a single recipe page. I've anchor tagged the images and button on the 'page-recipes.php' page and it takes me to a 404 error message?

Recipes Page

<?php /* Template Name: Recipes Page*/ ?> <?php get_header(); ?>

<div class="page-content" id="page-container">

    <section id="recipes">
        <div class="container">
            <div class="row">
                <?php 
                $args = array(
                    'post_type' => 'recipes'
                );
                $query = new WP_Query($args);
                ?>
                <?php if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?>
                    <div class="col-md-4 text-center">
                        <h3><?php the_field('title'); ?></h3>
                        <a href="<?php the_permalink(); ?>">
                        <img src="<?php the_field('image'); ?>" alt="<?php the_field('title'); ?>">
                        </a>
                        <p><?php the_field('short_description'); ?></p>
                        <a href="<?php the_permalink(); ?>"></a><button>Read Now</button></a>

                    </div>
                <?php endwhile; endif;  wp_reset_postdata();?>
            </div>
        </div>
    </section>

</div>

<?php get_footer(); ?>

Single Recipe Page <?php get_header(); ?>

<div class="page-content" id="page-container">


            <section id="recipes">
        <div class="container">
            <div class="row">
                <?php 
                $args = array(
                    'post_type' => 'recipes',
                    'order' => 'DESC'
                );
                $query = new WP_Query($args);
                ?>
                <?php if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?>
                <div class="col-md-4 text-center">
                    <h3><?php the_field('title'); ?></h3>
                    <img src="<?php the_field('image'); ?>" alt="<?php the_field('title'); ?>">
                    <p><?php the_field('full_description'); ?></p>

                </div>
                <?php endwhile; endif;  wp_reset_postdata();?>
            </div>
        </div>
    </section>




</div>

<?php get_footer(); ?>

The thing is it links to the correct URL (http://localhost:8888/CreativeNature/recipes/recipe-2/) But nothing hows up?

Any advice would be very welcome!!

Thanks!

Joe

2 Answers

I had this same error!

If you haven't already fixed it, I had to visit the permalinks page to reset the .htaccess. This apparently needs to be reset after adding custom post types.

Go to Settings > Permalinks. This should be enough but you could also hit update / or save to make sure.

This fixed it for me!

Make sure you do not have a Page that uses the same name/slug as your custom content type. If you have a landing page called Portfolio (/portfolio) and you have a custom post type of Portfolio (/portfolio/portfolio-item) then the custom post type will be 404. Changing the slug of the page or custom post type should resolve it.