• Resolved stevechatterton

    (@stevechatterton)


    I’ve been sneaking Loop-style structures into pages on my site with not much trouble except that the ‘Edit this entry’ link becomes a link to the edit post page for the last item displayed in the loop.

    Here’s an example page:
    http://www.thelitreview.com/the-lit-reviews-top-10-list

    When I try to put something like this in a post it looks like everything below the loop, including meta data, comments and some sidebar items that depend on the post ID, thinks the post ID is the ID of the last link displayed in the loop.

    Here’s the function:

    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    
    // using tables resolved some display issues we were having
    
    echo("<tr valign=\"top\">
    <td class=\"bookthumb\"><a href=\"");
    echo(get_permalink());
    echo("\" rel=\"bookmark\" title=\"Permanent Link to Book Reviews &amp Literary Criticism for ");
    echo(the_title_attribute());
    echo("\">");
    echo(the_excerpt());
    
    // the excerpt is the image (a real bad solution, I know)
    // for some reason, there's a link being thrown before
    // the image as well as around it. Could this be the
    // source of the problem
    
    echo("</a></td>
    <td class=\"bookinfo\"><div ");
    echo(post_class());
    echo("id=\"post-");
    echo(get_the_ID());
    echo("\"><strong><a href=\"");
    echo(get_permalink());
    echo("\" rel=\"bookmark\" title=\"Permanent Link to Book Reviews &amp Literary Criticism for ");
    echo(the_title_attribute());
    echo("\">");
    echo(the_title());
    echo("</a></strong></div>
    <div class=\"avgScore\"><strong>Average Score:</strong> ");
    
    $thisBook = get_the_ID();
    
    listAvgScore($thisBook);
    
    echo("</div>
    
    <p class=\"postmetadata\">Genres: ");
    echo(the_category(', '));
    echo(" | ");
    echo(edit_post_link('Edit', '', ' | '));
    echo(" ");
    echo(comments_popup_link('No&nbsp;Comments&nbsp;»', '1&nbsp;Comment&nbsp;»', '%&nbsp;Comments&nbsp;»'));
    echo("</p>
    </div>
    <div class=\"clear\">&nbsp;</div>
    </td>
    </tr>"); 
    
    endwhile;

    Any idea what I’m doing wrong and how I can go about fixing it? Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You have a stray closing DIV element in there, not sure if that could be a problem, but thought i’d point it out..

    Furthermore, i’m not sure if you write your own code, or whether someone writes it for you, but whoever encourages you or whomever else to write your code that way really shouldn’t, your code is highly unreadbale.

    Try this version instead.
    http://wordpress.pastebin.ca/1863314
    NOTE: You might need to remove the opening PHP tag i placed at the top, when inserting into existing code (incase you havn’t spotted that), placed their simply so the highlighting works on the pastebin page.

    Even if it doesn’t fix the issue, it should be much easier to read and modify.

    NOTE: I’m not meaning to just insult whoever writes the code, it’s simply that when it comes to seeking help, a good wealth of people won’t even bother looking when they see code formed like above, because like me, they’ll have to reformat the whole lot, just to get an idea of what’s going on with the code. So again i’m not saying it to be insulting, but more to educate you as to why others may be put off helping you with your code.

    Thread Starter stevechatterton

    (@stevechatterton)

    Thx t31os_ – it didn’t fix the problem, but I see how it’s easier to read.

    I had a problem once with display issues when I called a variable in the middle of an echo statement and the only way to resolve it was to bust everything into its own isolated echo. I don’t know why, but it did work, and since that day I’ve been overly cautious.

    Well it depends, but i think you’re referring to function calls, for example get_permalink(), and not variables, ie. $var (denoted by the dollar).

    Some functions return a value, others echo, when you try to store a value from a function that echoes it’s result, then the results aren’t as you expect. It’s all a matter of getting some perspective of how things work. When you need a value to be stored in a variable, for example $myvar = somefunction();, then that function must give a return value.

    However, something to bear in mind, is that when you call a function that returns, instead of echoing, it’s sometimes minus sanitization, so there’s potential for code injection or invalid characters being present(no filters have been run on the data, you have the raw data), etc…

    I’ll give you a brief example of return vs echo functions..

    <?php the_permalink(); ?>

    Echoes the value (so no good for storage).

    <?php get_permalink(); ?>

    Return value, so fine for storage.

    So the following..

    echo get_permalink();

    ..would be equivalent to..

    the_permalink();

    You can usually find whether a value returns or echoes simply by checking the corresponding codex entry for the function(or look in the source – codex being easier of course).

    So an incorrect usage example..

    echo 'hello, please check out my post '. the_permalink().';

    ..because in short, you’ve nested an echo inside another..

    Thread Starter stevechatterton

    (@stevechatterton)

    I started commenting out WordPress functions one by one to isolate the one that may or may not be causing my problem. I got it down to the point where every function was commented out and the page still displays meta data & comments from the last post called by the while loop.

    So I’m guessing the very nature of the while loop is flawed in this case. I’m off to to some of that web-based research, but I’ll check back to see if anyone has any advice.

    Thanks.

    Try placing a reset before or after your loop…

    wp_reset_query();

    Just a theory, but it could be another query throwing things off..

    Thread Starter stevechatterton

    (@stevechatterton)

    Your hunch paid off. That’s awesome. Thank you so much!

    Happy to help.. πŸ˜‰

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

The topic ‘Looping structures in posts confusing Post ID’ is closed to new replies.