• harshclimate

    (@harshclimate)


    Hi all,

    1) I have my archives page working pretty well, but I think I messed up my loop. When I click on a category, my page displays the category name but then it only displays one post under that category instead of all posts under that category. So that’s issue one.

    2) Ultimately what I what to have happen is that all of the posts that are under a category only displays the post_thumbnail() and no title or content. Just the featured image only. I did manage to have the featured image display with a link to the post itself, but again, and I think it’s related to the #1 issue, only the one post thumb is showing.

    If anyone wants to take a stab at this for me, I’d be willing to post any code that you need. I’m sure I messed it up somewhere.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Michael

    (@alchymyth)

    Thread Starter harshclimate

    (@harshclimate)

    Here’s the code in pastebin (thanks, Michael)

    https://pastebin.com/K7EYxQjS

    Moderator bcworkz

    (@bcworkz)

    Yup, you misplaced the loop code. Your thumbnail output code only works on the initial first pass and the loop code actually loops, but does nothing because there is no code within the loop. Try modifying the related portion of your template as follows. Any code before or after what’s shown remains the same.

                    <p>
                        These are the posts filed under the <?php single_cat_title(); ?> category. You will not be able
                        to identify the post content other than the photograph that associates with the post. I've
                        decided that adding text to each photo would just be too much.
                    </p>
                </div>
            <?php while ( have_posts() ) : the_post(); ?>
                <div class="archive-gallery">
                    <?php if ( has_post_thumbnail() ) : ?>
                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail(); ?></a>
                    <?php endif; ?>
                </div>
            <?php endwhile; ?> 
            </div>
        </main>
     
    <?php  else :
     
                esc_html_e( "Sorry. No posts match your criteria." );

    All I did was move the while and endwhile lines, coordinating the <?php ?> tags as needed.

    Thread Starter harshclimate

    (@harshclimate)

    Hi BC! Long time no see. Thanks for your expertise, bud. I’ll give what you added a shot in a bit. A comment, though… I didn’t know you could nest a loop inside of a loop? Is that what I’m seeing?

    Michael

    (@alchymyth)

    try and move the output of the featured image into the loop;

    example https://pastebin.com/nVvQdm3R

    I am not sure about your intended html structure, so you might need to adjust the location of the html tags in the code.

    Moderator bcworkz

    (@bcworkz)

    I’m sorry I didn’t recognize you at first, it has been a long time! I’m glad your topic is not being ignored this time ??

    Sure, you can nest loops, but it can get confusing with nesting the while(): endwhile; syntax. It’s best to keep that to one instance (I prefer the outermost instance) and use the while() {} syntax for the inner loops. I don’t see where we’re nesting loops here, but I’ve been known to suffer from selective blindness when it comes to seeing significant code elements ??

    Do ensure loops are fully nested and not overlapping. Don’t do this:

    while ( condition_a()):
      while ( condition_b()) {
        do_something();
      // the final } belongs here
    endwhile;
      }

    What you cannot nest is <?php ?> tags. Doing so is illogical, but it can happen with novices or the more experienced not paying attention. PHP throws a syntax error when you try, so it doesn’t get very far.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Two issues with archive.php’ is closed to new replies.