0

disclaimer: this is my first foray into wordpress

I'm creating a custom theme and in the single.php file, i have the main loop, however if i navigate to different posts, all that shows up is the latest post. I could go to the different permalinks, but the only post that shows up is the latest one. How do I make it so that the correct post shows up?

I tried changing the permalinks and deleting and adding pages, but I get the same result.
Here is single.php

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

    <section class="main-section">
        <header>
            <h1 class="section-header"><?php the_category('/');?></h1>
            <ul class="section-nav">
                <?php
                    $cat = get_the_category()[0];
                    $categoryPosts = get_posts(array('category' => $cat->term_id));

                    foreach($categoryPosts as $post): setup_postdata($post);
                ?>

                    <li><a href="<?php the_permalink();?>"><?php the_title(); ?></a></li>

                <?php endforeach; ?>
                <?php wp_reset_postdata(); ?>
            </ul>
        </header>
        <h2 class="section-blurb"><?php the_title();?></h2>
        <div class="info">
            <?php the_content(); ?>
        </div>
    </section>

<?php endwhile;?>

Everything works..but only for the latest post.

Edit:
I reinstalled wordpress fresh and made new pages, but the result is the same. I even stripped the code down to just

<?php the_title(); ?>

in the single.php file, but the result is the same. No matter which post you're supposed to be viewing, it only shows the latest post.

6
  • what do the contents of your single.php file look like? edit your question to add the code.
    – Milo
    Dec 28, 2014 at 17:28
  • @Milo I added the code Dec 28, 2014 at 17:33
  • so for each post you are also outputting posts from the same category above the post's content?
    – Milo
    Dec 28, 2014 at 17:37
  • @Milo Yes, that's correct. As a sort of navigation because this is actually a website instead of a blog. Dec 28, 2014 at 17:43
  • @PieterGoosen setup_postdata won't work unless you use $post
    – Milo
    Dec 28, 2014 at 17:48

2 Answers 2

0

I think that your structure here is wrong. I would completely remove your custom query from inside your loop and move it outside. Another option is to make use of other variable names in your custom query and making direct use of the WP_Post objects

As @Milo already explained, your custom query is most probably affecting the value of your $post global

OPTION 1

Move the custom query outside your loop (CAVEAT: Untested)

<section class="main-section">

    <header>
        <h1 class="section-header"><?php the_category('/', '', get_queried_object_id() );?></h1>
        <ul class="section-nav">

        <?php
            $cat = get_the_category( get_queried_object_id() )[0];
            $categoryPosts = get_posts( array( 'category' => $cat->term_id ) );

            foreach($categoryPosts as $post): setup_postdata($post);
        ?>

               <li><a href="<?php the_permalink();?>"><?php the_title(); ?></a></li>

               <?php unset( $post ); ?>

         <?php endforeach; ?>

       </ul>
    </header>

Then run your loop as normal. the_post() in your loop will reset your $post global to the main query

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

        <h2 class="section-blurb"><?php the_title();?></h2>
        <div class="info">
            <?php the_content(); ?>
        </div>
    </section>

<?php endwhile;?>

OPTION 2

Use different variable names (CAVEAT: Untested)

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

    <section class="main-section">
        <header>
            <h1 class="section-header"><?php the_category('/');?></h1>
            <ul class="section-nav">
                <?php
                    $cat = get_the_category()[0];
                    $categoryPosts = get_posts(array('category' => $cat->term_id));

                    foreach($categoryPosts as $categoryPost):

                       $title = apply_filters( 'the_title', $categoryPost->post_title ); ?>
                        <li>
                             <a href="<?php the_permalink();?>">
                                  <?php echo $title; ?>
                             </a>
                        </li>

                <?php endforeach; ?>
                <?php unset( $categoryPost ); ?>
            </ul>
        </header>
        <h2 class="section-blurb"><?php the_title();?></h2>
        <div class="info">
            <?php the_content(); ?>
        </div>
    </section>

<?php endwhile;?>
8
  • I tried both your advice, but it still doesn't seem to work. Please see my edit Dec 29, 2014 at 17:56
  • Do you have any plugins installed Dec 29, 2014 at 17:57
  • No, i disabled all the plug-ins Dec 29, 2014 at 17:58
  • OK, so you have wordpress reinstalled, disabled all plugins. Have you switched to a bundled theme, what is the result Dec 29, 2014 at 18:00
  • the twentyfifteen theme works fine. Dec 29, 2014 at 18:03
1

When you call setup_postdata($post);, that populates the global $post with the current post in your $categoryPosts result set.

After the foreach terminates and you try to output the regular post data with template tags:

<h2 class="section-blurb"><?php the_title();?></h2>
<div class="info">
    <?php the_content(); ?>
</div>

Those template tags are getting their data from $post, which still contains whatever was the last post in your $categoryPosts.

To fix that, you need to reset the value of $post to what it was before you ran the secondary query, with wp_reset_postdata:

foreach($categoryPosts as $post): setup_postdata($post);
    // output post
endforeach;

wp_reset_postdata();

// now you can return to outputting the post in the main loop
the_title();
6
  • nope, added the code and it's still only showing the latest post. I removed the whole setup_postdata() function and it still did the same thing. Dec 28, 2014 at 18:01
  • update your question to show your current code.
    – Milo
    Dec 28, 2014 at 18:08
  • i updated the question Dec 28, 2014 at 18:13
  • when I copy and paste your code into a single.php template it works as expected. I see a list of the latest 10 posts in the same category above the selected single post. I see 10 because that's the value of Blog pages show at most under Settings > Reading.
    – Milo
    Dec 28, 2014 at 18:18
  • Yeah, the category navigation parts work fine, but it seems to only work with that last post that was no matter what post you're supposed to be on. All the content is there, but if you go to see another post, you just keep getting the same one. Dec 28, 2014 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.