1
votes

I've set up a WordPress Custom Post Type using code. I've managed to get the Custom posts to display in an 'archive' page but, if I click on a link in the 'archive' page WP can't find the single post page.

I think maybe I've got a problem with the custom post permalinks. Hoping someone can help me out

functions.php

add_action('init', 'create_portfolio');

function create_portfolio(){

    $portfolio_args = array(
        'label' => _('Portfolio'),
        'singular_label' => _('Portfolio Item'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => true,
        'supports' => array('title', 'editor', 'thumbnail')
    );

    register_post_type('portfolio', $portfolio_args);

}

Page Template

To display the custom posts I set up a new page template with a WP Query:

<?php

/*
Template Name: The Portfolio
*/

get_header(); ?>


<?php
$args = array(
    'post_type'   => 'portfolio',
    'post_status' => 'publish'
 );

$portfolios = new WP_Query( $args );


if( $portfolios->have_posts() ) :
  while( $portfolios->have_posts() ) : $portfolios->the_post();
    ?>

  <h1><a href="<?php echo get_post_permalink() ?>"><?php echo 
  the_title();?></a></h1>

     <?php the_post_thumbnail('medium'); ?>

      <?php //the_content(); ?>
      <?php the_excerpt(); ?>

    <?php

 // Stop the loop when all posts are displayed
 endwhile;

// If no posts were found
else :
?>
<p>Sorry no posts matched your criteria.</p>
<?php
endif;
?>   
<?php
get_footer();
?>

The page template code seems to work and and all custom posts display as expected BUT when I click on the 'title' link I get a 'Page Not Found' error.

I notice that when I mouseover a 'title' link, the following displays in browser status bar:

someplace.com/wp/portfolio/christmas-cake

However, the error page I'm taken to has the URL:

someplace.com/wp/portfolio/christmas-cake/christmas-cake/

What's happening??

1
Have you tested re-saving your permalinks? I'd also add this after your endwhile: wp_reset_postdata()David.J

1 Answers

2
votes

I think you just need to create single-{posttype}.php file in your theme.

Try to create single-portfolio.php file and do some code there.

Please save permalink setting again from the settings -> permalink option from the back-end.

One more thing you need to pass post id inside your code & replace it like below:

<a href="<?php echo get_permalink($post->ID); ?>">

Try it & let me know if facing any issue.