0
votes

I have created a custom post type named as product. After that I have created a template file to show all products on that page and write the below code :

<?php $loop = new WP_Query( array( 'post_type' => 'acme_product',
                                   'posts_per_page' => 14 ) );
      while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
<div>
<?php the_post_thumbnail('thumbnail');
?>
</div>
<div>
<?php the_title( '<h2 class="entry-title"><a href="'.get_permalink().'" title="'.
                 the_title_attribute( 'echo=0' ).
                 '" rel="bookmark">', '</a></h2>' );
?>
</div>
</div>
<?php endwhile;
?>

But when I click on the product title link it will not show my product details page. Can anyone help me?

2
could you show the code to create your custom post type? And is the above code the exact code you are using, coz in the above code you are fetching the 'acme_product' type post not 'product'Yamu

2 Answers

1
votes

You need to save your template as single-{post_type}.php so in your case it would be single-acme_product.php and make sure that permalink is enable if not than

  • Login to your WordPress admin.
  • Go to Settings -> Permalinks. And under Common Settings, let’s use Post name.
  • Then click ‘Save Changes’

as per your code you don't need to define the post_type here. just use with simple code

<?php while ( have_posts() ) : the_post(); ?>
0
votes

It sounds as though you've already figured out how to loop through your product type. If you're new to custom post types and are not already aware of it, I would suggest investigating the "archive" template for custom post types (archive-{post_type}.php). I saw another answer that referenced single-{post_type}.php as well; both of these can be identified in the Template Hierarchy documentation in the WP Codex. By default, if you don't provide a customized single template for your post type, it will fall back to single.php (which may require customization if you want it to perform double-duty for multiple post types).

With that in mind, and assuming you're already using single-product.php in your theme (or have otherwise coaxed Wordpress into using your template), it would be helpful to know the symptoms you're experiencing:

  • Are you getting a 404? This could be a permalink issue, and if you haven't already attempted it you might consider flushing your rewrite rules. This can be accomplished by simply visiting the Settings -> Permalinks panel in the Dashboard.
  • Is your template being used? Consider adding some HTML comments to your template, and view source on the rendered version to ensure that you're even using the correct file. Failure to load the appropriate template could be due to a filename typo or some other logic that interferes with the normal template selection logic.
  • If you're getting the correct template, but you're not able to display the correct details, are you doing anything particularly wonky with your single template's loop?

Without clear details on what symptoms you're actually experiencing, it's hard to give you better direction. Hopefully some of this information points you to the correct answer, but if not please share additional details as to what is happening on your end.