Forums

Recent Posts Code (34 posts)

  1. Mithrustt
    Member
    Posted 2 years ago #

    I want to get some code going so I can have recent posts show up in the sidebar. I'd use the sidebar widget, but when I go to use it, it messes the formatting of everything up. So, I just wanted to add the coding directly to the main index page, but I don't know what the code is.

  2. drmike
    Member
    Posted 2 years ago #

    need a link please....

    The one in your profile isn't a wp blog it appears.

  3. MichaelH
    moderator
    Posted 2 years ago #

    The template tag, wp_get_archives(), and the postbypost parameter can get the job done.

  4. Mithrustt
    Member
    Posted 2 years ago #

  5. Mithrustt
    Member
    Posted 2 years ago #

    I forgot to mention that I only wanted to have only one category of posts show up in the recent posts section. Is there a way to add code to the wp_get_archives() template tag?

  6. Mithrustt
    Member
    Posted 2 years ago #

    I got the recent post code up and working for the most part. The one thing is that it isn't formatted like the rest of the stuff in the sidebar. Did I miss something?

    And, is it possible to not only have it show one category, but show the most popular posts in that category? Like, say I want to show no more than 5 posts, but their not actually the most recent posts in that category, but the ones with the most comments. Possible? Not possible?

  7. drmike
    Member
    Posted 2 years ago #

    check out the link that MichaelH gave you as it talks about listing only certain categories and modify the outputted code.

  8. Mithrustt
    Member
    Posted 2 years ago #

    I checked it, but I didn't find anything that would show me how to get only one category of posts to show.

  9. Mithrustt
    Member
    Posted 2 years ago #

    I've tried changing stuff around, but it hasn't really helped. The formatting is still messed up. I changed my mind on the one category idea, though. Now I'd like to do it so it's from a few categories, but not all categories.

    Any ideas?

  10. Sivar
    Member
    Posted 2 years ago #

    You might be interested in this.

    You should modify that code with "<?php $myquery = $wp_query; ?>" before the query_posts and "<?php $wp_query = $myquery; ?>" after the end of the loop to preserve the original query.

  11. Mithrustt
    Member
    Posted 2 years ago #

    OK, so I had this before:

    <h2>Recent Reviews</h2>

      <?php wp_get_archives('type=postbypost&limit=5&format=custom'); ?>

    <br/>

    Then I changed it to this:

    <h2>Recent Reviews</h2>

      <?php $myquery = $wp_query; ?>

      <?php
      query_posts('cat=5&showposts=5');
      while (have_posts()) : the_post() ?>

      <?php $wp_query = $myquery; ?>

      <?php endwhile; ?>

    <br/>

    But it doesn't show anything now, just the header.

  12. Sivar
    Member
    Posted 2 years ago #

    First of all... the line "<?php $wp_query = $myquery; ?>" needs to be BELOW the endwhile. Otherwise you get a real mess.

    And second... of course it doesn't show anything, because there is nothing inside your loop. You need to put template tags inside it like the_title() and the_content().

    I'll give you an example of a very simple loop:

    <?php while (have_posts()) : the_post(); // watch the semicolon at the end of the line, I forgot that before! ?>
    <h2><?php the_title() ?></h2>
    by <?php the_author() ?> · <?php the_time('F js, Y') ?>
    <?php endwhile; ?>

    I hope, it got it all right and could help (I'm pretty new to wordpress, too)... and maybe this gives you a better understanding of The Loop itself.

  13. Mithrustt
    Member
    Posted 2 years ago #

    All right, I put this in, doing what I think you meant:

    <h2>Recent Reviews</h2>

      <?php $myquery = $wp_query; ?>

      <?php
      query_posts('cat=5&showposts=5');
      while (have_posts()) : the_post() ?>

      <?php while (have_posts()) : the_post(); // watch the semicolon at the end of the line, I forgot that before! ?>
      <h2><?php the_title() ?></h2>
      by <?php the_author() ?> · <?php the_time('F js, Y') ?>

      <?php endwhile; ?>

      <?php $wp_query = $myquery; ?>

    <br/>

    But I get this error when I load the page:

    Parse error: syntax error, unexpected T_ENDIF in /home/thinkth1/public_html/savingprogress/launchprep/wp-content/themes/blueblog-10/sidebar.php on line 78

  14. Sivar
    Member
    Posted 2 years ago #

    Delete the following line:
    <?php while (have_posts()) : the_post(); // watch the semicolon at the end of the line, I forgot that before! ?>
    You got that "while..." twice, when you copy-pasted my loop example.

    After that, see if the error message still shows up. If it does, there must be some "endif;" where it does not belong (I would guess, below the code you posted). Try commenting that out, unless it belongs to another loop, which starts with "if (have_posts())".

    Good luck! ;)

  15. Mithrustt
    Member
    Posted 2 years ago #

    It kinda works now. The only problem is I just want it to be a link, like the other links in the sidebar, and not a header, like the headers in the sidebar.

    Also, the text doesn't space out properly. Like, it overlaps.

  16. Sivar
    Member
    Posted 2 years ago #

    Ok, try something else as content of your loop. Content means, between:
    <?php while (have_posts()) : the_post() ?>

    and

    <?php endwhile; ?>

    Here's the content:
    <li><a href="<?php the_permalink() ?>" title="<?php _e('Permanent link to'); ?> <?php the_title(); ?>"><?php the_title() ?></a></li>

  17. Mithrustt
    Member
    Posted 2 years ago #

    So close! It looks great now, but there is one more problem. I have three posts, all of which are in the same caetegory, which is the category I have set up to show under recent posts.

    However, it only shows the two in the sidebar and on the main page. When I enter the URL for the third post, it still doesn't show up. But when I take the recent posts code out, it shows up fine.

    What could be causing this?

  18. Mithrustt
    Member
    Posted 2 years ago #

    Actually, I did have it in the wrong category, so I switched it and it shows up. However, when I made a test post in a different category, it doesn't show up at all.

  19. Sivar
    Member
    Posted 2 years ago #

    Great, so far :).

    Well, look at the line with query_posts... see the "cat=5" in there as an option? That means, that only posts from category 5 (= reviews?) are being displayed. If you want to include other categories to the query (let's say, category 1), you would modify your query_posts like this:

    query_posts('cat=1,5&showposts=5');

    Now it says: "Ask for the 5 latest posts from categories 1 and 5."
    You can add as many categories as you like. Example:

    query_posts('cat=1,4,5,10,12&showposts=5');

  20. Mithrustt
    Member
    Posted 2 years ago #

    I understand that, but the problem is that it doesn't display a post on the main page if it isn't included in the query_posts.

  21. Mithrustt
    Member
    Posted 2 years ago #

    And it also won't display pages. I'll click on a page link, and it says it goes there, but it just shows the home page.

  22. Sivar
    Member
    Posted 2 years ago #

    That's odd... and shouldn't be. Are you sure, you didn't forget this line before our "while..."...
    <?php $myquery = $wp_query; ?>
    ...and this one below our "endwhile"...?
    <?php $wp_query = $myquery; ?>

    If they are both there, I don't think this loop has something to do with the not appearing post on your main page.

  23. Mithrustt
    Member
    Posted 2 years ago #

    This is what I have:

    <h2>Recent Reviews</h2>

    <br/>

  24. Sivar
    Member
    Posted 2 years ago #

    Seems fine to me. If you delete the whole thing (the code you just posted), does the rest of your page work as it should?

    I'm using something very similar on my page, and it doesn't give me any problems. Maybe someone else has an idea?

    Oh, and please use the backticks ("code"-button) for code you post (explanation below the text area) :).

  25. Mithrustt
    Member
    Posted 2 years ago #

    Yeah, everything works perfectly normal when the new code is gone.

    I don't understand.

  26. Sivar
    Member
    Posted 2 years ago #

    In the end, immediately after ´<?php $wp_query = $myquery; ?>` try inserting the following line:

    <?php rewind_posts() ?>

  27. Mithrustt
    Member
    Posted 2 years ago #

    Nope, that didn't work.

  28. Sivar
    Member
    Posted 2 years ago #

    Ok, delete the rewind_posts() line. Then modify:

    <?php $myquery = $wp_query; ?>
    to -> <?php $myquery = clone $wp_query; ?>

    and

    <?php $wp_query = $myquery; ?>
    to -> <?php $wp_query = clone $myquery; ?>

    Now pray, that it works with wordpress 2.2! ;)

  29. Mithrustt
    Member
    Posted 2 years ago #

    No, it gives me this error:

    arse error: syntax error, unexpected T_VARIABLE in /home/thinkth1/public_html/savingprogress/launchprep/wp-content/themes/blueblog-10/sidebar.php on line 50

  30. Sivar
    Member
    Posted 2 years ago #

    Ok, one last thing before I give up...

    Delete:
    <?php $myquery = $wp_query; ?> (or the modified one with clone)
    and...
    <?php $wp_query = $myquery; ?> (or the modified one)

    Replace:
    <?php query_posts('cat=5&showposts=5'); ?> with...
    <?php $myquery = new WP_Query('cat=5&showposts=5'); ?>
    and...
    <?php while (have_posts()) : the_post() ?> with...
    <?php while($myquery->have_posts()) : $myquery->the_post(); ?>

    But I have no idea, if the loop shows the correct titles/links, regardless of what else is currently displayed on your page.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags