• Resolved mattregister

    (@mattregister)


    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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; ?>

    Thread Starter mattregister

    (@mattregister)

    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.

    Thread Starter mattregister

    (@mattregister)

    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.

    Hello Matt,

    Glad to see that you figured that out.

    Any chance that you could explain your solution to me? I would really, really appreciate it.

    – Joe

    Thread Starter mattregister

    (@mattregister)

    Joe,

    I used Songdogtech’s solution.

    I basically styled my drop down menu as an unordered list in my style sheets, I put in my
    ul tag, then used songdogtech’s code.

    This is what I have for the news dropdown:

    <ul> 
    
    <?php $my_query = new WP_Query('category_name=news&showposts=5'); ?><?php while ($my_query->have_posts()) : $my_query->the_post(); ?><li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li><?php endwhile; ?>
    
    <li><a href="https://buyandsellwithshannon.com/news/2">MORE...</a></li>
    </ul>

    I did the same thing for the blog drop down, changing the name of the category in the query.

    This is brilliant – I was looking to do something very similar and already had the drop-down styled, just needed the proper query code.

    Worked like a charm!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Latest Posts in a drop down menu’ is closed to new replies.