Forums

[resolved] Latest Posts in a drop down menu (4 posts)

  1. mattregister
    Member
    Posted 2 months ago #

    BuyAndSellWIthShannon.com

    I am trying to do something that is on the very edge (arguably just beyond) my technical ability. I built the theme for my wife's real estate page. It features a bottom portion that list the latest posts in a couple of categories (news and blog). I have been playing with adding a drop down menu at the top of the page. What I would like is for you to mouse over "news" and have a drop down menu of the last 5 posts, and a 6th option of "MORE..." which would take you to page 2 of the news archive. I would like the same for the "blog" menu item.

    I have gotten this to work utilizing the same code as I used in the bottom portion of the page. However, it displays the latest post in every post, page, and archive on the site. Clearly, this is some kind of loop problem. I have tried the "rewind post" solution with no luck.

    Here is the code I am using to display the post titles at the bottom of the page. Ignore the extra code getting the time, this just displays a "new" tag if it is less than a week old.

    <?php
    $posts = get_posts('category=1&numberposts=4');
    foreach($posts as $post) :
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?><?php
    // get the time of the current post
    $u_time = get_the_time('U');
    // reset my selector
    $tooOld = 0;
    // set the time selector
    if ( ( time('U') - 432000 ) >= $u_time ) { $tooOld = 1; } // 60*60*24*7 = 604800 = 7 days
    
    ?> <?php if($tooOld < 1 ) { ?>
    <span class="new"><strong>NEW</strong></span>
    <?php } ?>
    </a></li>
    				<?php endforeach; ?>

    So essentially I am trying to use this code twice in the same page, once in the header as a drop down menu, and once in the footer where it exists peacefully now. Extra gold star for anyone who can help me figure this out.

  2. songdogtech
    Member
    Posted 2 months ago #

    I think you need a new query. This should give you some ideas, though I can't see how to combine the two to get what you want.

    But this is what I use for a new query that will work inside the loop and multiple times on a single page/post, if needed. It shows the permalinks of the last 5 posts. I'm not sure there's a need to use time loop to determine the last number of posts.

    <?php $my_query = new WP_Query('category_name=mycategory&showposts=5'); ?><?php while ($my_query->have_posts()) : $my_query->the_post(); ?><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><?php endwhile; ?>

  3. mattregister
    Member
    Posted 2 months ago #

    I will try that. Thanks.

    The time loop is only to add the word "new" in red to the title if the post were less than a week old. that was a display thing, not a function thing.

  4. mattregister
    Member
    Posted 2 months ago #

    songdogtech,

    thank you very much for the perfect solution. That is why I love this board. less than 20 minutes for a perfect solution. see it here:
    BuyAndSellWithShannon.com

    Gold star for you. Many thanks.

Reply

You must log in to post.

About this Topic