WordPress Help - What's Wrong With This "Latest Posts" Code?

Discussion in 'PHP' started by muncle, Oct 14, 2008.

  1. #1
    I have following code in a sidebar of my WP theme:

    <?php query_posts('category_id=all');?>
    <?php $posts = get_posts('category=all&offset=0');
    	foreach ($posts as $post) : start_wp(); ?>
    <li><a href="<?php echo get_permalink() ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    Code (markup):
    It only displays 5 latest posts. How do I make it so it displays unlimited posts? Or how do I make it so I can control how many latest posts are displayed?

    Somebody help, I'm a PHP newb :eek:
     
    muncle, Oct 14, 2008 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    shallowink, Oct 14, 2008 IP
    muncle likes this.
  3. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #3
    Try usiing <?php wp_get_archives('type=postbypost&limit=10'); ?> Instead of <?php $posts = get_posts('category=all&offset=0');
     
    Bohra, Oct 14, 2008 IP
    muncle likes this.
  4. muncle

    muncle Guest

    Messages:
    1,195
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks guys,

    MegaMania:
    tried that - it worked but also included the name of a blog hyperlinked to the homepage at the bottom of the list

    shallowink:
    great link, I may have fixed my problem with advice given there. This is what the code looks like and it seems to work:

    <?php
     $lastposts = get_posts('numberposts=20');
     foreach($lastposts as $post) :
        setup_postdata($post);
     ?>
     <li><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>
    Code (markup):
    Thanks again guys, green rep on the way for both of you ;)
     
    muncle, Oct 14, 2008 IP
  5. muncle

    muncle Guest

    Messages:
    1,195
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I need help one more time (I'm totally worthless with PHP and didn't want to start a separate thread)

    I'm trying to display random posts with hyperlinked title and excerpt of content. This is the code I could come up with but while hyperlinked titles are shown, no except is displayed. What am I doing wrong?

     <?php
     $rand_posts = get_posts('numberposts=5&orderby=rand');
     foreach( $rand_posts as $post ) :
     ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <div class="entry">
    <?php the_excerpt() ?>
    </div>
     <?php endforeach; ?>
    Code (markup):
     
    muncle, Oct 14, 2008 IP
  6. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #6
    think get_posts isn't accessing all of the post data. from the same link I sent earlier...

    <?php
    $lastposts = get_posts('numberposts=3');
    foreach($lastposts as $post) :
    setup_postdata($post);
    ?>

    setup_postdata is the addition , aint sure its the answer but good place to start...
     
    shallowink, Oct 14, 2008 IP