WordPress.org

Ready to get started?Download WordPress

Forums

some loop and $GET magic (1 post)

  1. sepehr125
    Member
    Posted 5 years ago #

    Alright, design goal:
    User goes to News section, sees latest post; there is a sidebar on the right with other news titles. Clicking on items in sidebar brings up just the one post. I'm not using themes or templates, just loop. I've got a good thing going right now, but feel like it can be optimized. As you can see below, links are generated in div#wpnav, with their slug in the URL. A $GET command picks up the slug from the URL and queries the exact post. I think it's brilliant :) Thing is, it would be so awesome if the root, "all_homilies.php" showed the latest post. Right now I have another page that pulls up the latest post and has the exact same navbar, but I get the feeling, one of those feelings you get after not sleeping for some 30 hours, that there simply must be a more parsimonious solution to this problem. As you'll note below, in the $GET phrase, I have it so that if there is no slug name put in (i.e. the user didn't use the navbar but decided to get clever and see where "all_homilies.php" leads), it puts in $$$ by default. Why? because otherwise it would go to a page with all posts from all categories on it, and we can't have that. At least this way I can get a "no post" message in there.

    <div class="fr" id="wpnav">
    <ul>
    <?php query_posts('category_name=homily'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <li><a href="http://mysite.com/all_homilies.php?name=<?php echo($post->post_name) ?>"><?php the_title(); ?>, <?php the_time('Y'); ?></a></li>
    
    <?php endwhile; else: ?>
    <p>Error</p>
    <?php endif; ?>
    
    </ul>
    </div>
    
    <?php $name = (isset($_GET['name']))? $_GET['name'] : '$$$'; ?>
    
    <?php rewind_posts(); ?>
    <?php query_posts("name=$name"); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <?php the_content(); ?>
    <div class="center"><a href="#top">back to top</a></div>
    
    <?php endwhile; else: ?>
    <h2> class="style1">Curious? That's okay, we're a curious bunch, too. </h2>
    <?php endif; ?>

    I tried doing a query in the beginning for the latest post
    <?php query_posts(category_name=homily&order_by=date&show_posts=1) ?>
    taking its slug name and saving it to a variable, then putting that instead of $$$, but for some reason the latest post was always whatever page you're on.

    Thanks in advance for all your help, I'll appreciate it a ton after getting some sleep.

    "?"

    Sepehr

Topic Closed

This topic has been closed to new replies.

About this Topic