Why is my custom category template duplicating the entries?

I’ve created a custom category page for a store I’m working on which you can see here:

As you can see, the product listing is duplicated on the page, it doesn’t seem to matter how I configure the page template, this gets duplicated no matter what. I’d appreciate any help I can get.

Here is the code I have in my custom category template named mp_category.php:

<?php
/*
Template Name: Marketpress Category
*/
?>
<?php get_header(); ?>
<div id="mid">
<div id="content" class="grid_8">
<?php if (have_posts()) : while (have_posts()) : the_post();
if( has_term( 'Members Only', 'product_category' ) ) {
$url = $_SERVER[REQUEST_URI];
if (strpos($url, "members-only")) {
if ( !is_user_logged_in() ) {
header("Location: http://essentialsboutique.thinkbigwebsites.com/vip-area");
die();
}
} elseif (!strpos($url, "category")) {
if ( !is_user_logged_in() ) {
header("Location: http://essentialsboutique.thinkbigwebsites.com/vip-area");
die();
}
}
}
?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="entry">
<h2><?php single_cat_title('Product Category: '); ?></h2>
<div class="entry single-category">
<?php mp_list_products(); ?>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
</div>
<?php endwhile; endif; ?>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
</div>
</div>
<?php get_footer(); ?>

  • Ash
    • WordPress Hacker

    Hello @dgulledge

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    Please replace the line 9

    <?php if (have_posts()) : while (have_posts()) : the_post();

    with

    <?php wp_reset_query() if (have_posts()) : while (have_posts()) : the_post();

    or

    <?php wp_reset_postdata() if (have_posts()) : while (have_posts()) : the_post();

    Try both and check which one works.

    Cheers

    Ash

  • dgulledge
    • New Recruit

    Hi Ash, thanks for the warm and quick response! Unfortunately, neither of those worked. Aside from getting an error for not having a semicolon after the initial wp_reset calls :slight_smile: I’m still getting the duplicated post info. And I just realized that I didn’t close the tag on my hyperlink to the page earlier so the whole post was linked to my example page (doh!).

    Any other ideas on what might be wrong here?

  • dgulledge
    • New Recruit

    Yes, the duplication occurs when using the same page template on the WP 2012 theme. Here are some additional observations I’ve made while continuing to troubleshoot this on my own:

    — It’s actually not just duplication. It’s replicating the content the same number of times as the number of items that are in the category. When I view a different category with 4 items, the content is duplicated 4 times. When I view a category with 1 item, the content is only there once.

    — I’m using a similar template for mp_product.php which works perfectly.

    — I have a home page template that calls the exact same function as this one (“mp_list_products()”:wink:, on the category with 4 items mentioned above, which also works as intended.

    — If I remove the mp_category.php file and let Marketpress default to the page.php template, it works like it should.

  • dgulledge
    • New Recruit

    Dear Kevin,

    I’m not really sure who your post is directed to exactly, but I don’t appreciate your assumption that I am a “new free member” as I have had an annual paid subscription to WPMUDev for over a year now. I have just never needed to ask a question in the forums until now. I also don’t appreciate you junking up my email inbox with snarky replies to a resolved issue that had absolutely NOTHING to do with YOU. Further, perhaps your attitude is why you can’t seem to get the help you need. Just a thought. Have a great day.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.