• Resolved dbhynds

    (@dbhynds)


    I’m creating a website using WordPress, http://www.dougberkytheatre.com.

    I’m new to php (really new, as in learning it for this project) and I’m having some trouble getting it to do what I want.

    I have several posts labeled under Category 3. I want to display the titles of each page in the navigation. I can get it to display correctly, but any time you click the link to load a page, always loads the same post.

    Here is the code I have for the navigation:

    <table border=0 cellpadding=0 cellspacing=0>
        <tr>
        <?php query_posts('cat=3'); ?>
        <?php while (have_posts()) : the_post(); ?>
        <td class="nav" nowrap><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></td>
        <td><img src="/images/navdivider.gif"></td>
        <?php endwhile; ?>
        <td class="nav" width="100%"></td>
        </tr>
        </table>

    Here is my code for the main body of my page:

    <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>

    So here’s the problem, the site automatically loads the page entitled “Test Post.” However, when you click “Test post 2” it will load “Test Post” instead. Basically, any link you click will always display “Test Post.”

    I think the <?php query_posts('cat=3'); ?> code is somehow causing it, because when I delete that, everything works fine. The problem is that I don’t want posts in Category 4 to be displayed in the navigation, and deleting this code causes Category 4 posts to be displayed. Oh, the other wierd thing that tells me it’s somehow connected to that line of code is that when I move the Navigation code below the Main Body, everything works fine. So it’s like the query_post function is somehow getting hung up on the last post in the category (which is “Test Post”), but only does so if the function occurs before the main body code.

    Can someone help me out here? I’m completely new to php.

Viewing 6 replies - 1 through 6 (of 6 total)
  • I’m guessing you’ve found your way into the Codexquery_posts, which is good. Without see your more of your source code, I can only take a guess at what’s wrong.

    As you may know, the query_posts function, when before a loop, overrides the default query that WP runs for a particular page (e.g. the most recent posts for a front page). If a query_posts() call is put before one loop, and then another loop is used, that second loop will use the same query as the first query_posts() call.

    I think that’s what happening in your case. The Loop, especially the multiple loop example #2. You’ll probably want to do something like that – save the temp query and then call it before your second loop.

    If I misunderstood or you’re still having problems, then you’ll need to post the full code of your index.php and the sidebar.php (or whereever this navigation is going) so we can understand what’s all going on. Paste those into the WP pastebin at this address: http://wordpress.pastebin.ca/. Then, link to the files.

    Also, I’m getting database connection errors when I try to visit your site.

    Thread Starter dbhynds

    (@dbhynds)

    That sounds like my problem! (Good job on figuring it out).

    I thought it might be something like that, but like I said, I’m REALLY new to php.

    So how would I save the temp query and then call it again?

    Oh, sorry, I had a fragment in my original post.

    Anyway, take a look at the Codex page for The Loop, especially second example for a multiple loop. That example (#2) has the code you need.

    Thread Starter dbhynds

    (@dbhynds)

    Alright… here’s the pastebin:
    http://wordpress.pastebin.ca/1038862

    I tried using the information from Example (#2) in The Loop, but nothing changed. It behaves exactly the way it did before.

    Help?

    When you add the template tags for your the main content, that needs to be inside a “Loop” as well, because, to get the template tags to work, the_post() function is necessary.

    From your code, starting at line 55:

    <?php $wp_query = $my_query; ?>
    <h1><?php the_title(); ?></h1>
    <?php the_content(); ?>

    The last two lines need to be inside a Loop.

    Based on your current file, replace the above code with the following, and that should get everything working.

    <?php $wp_query = $my_query; ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h1><?php the_title(); ?></h1>
    <?php the_content(); ?>
    <?php endwhile; else: ?>
    <?php _e('Sorry, no posts matched your criteria.'); ?>
    <?php endif; ?>
    Thread Starter dbhynds

    (@dbhynds)

    That got it… or at least, close enough to where I could figure it out. Thanks!

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

The topic ‘Displaying posts in category as navigation’ is closed to new replies.