Showing multiple wordpress posts within a php page

Discussion in 'PHP' started by hostedweb, Jan 30, 2010.

  1. #1
    Hi,

    I have a bit of PHP code within my sites homepage that's connecting to my Wordpress DB and and pulling a table, i then have another script that echo's the last Wordpress post into my homepage content.

    I have a few issues though

    1. I would like to show a few of the latest posts rather than one
    2. I would like the excerpt to be a number of words (its currently 200 characters, which cuts the last word in half)
    3. The script currently echos the last post, even if its not published, how can i only show the last published post?

    If anyone could assist me with any of these issues i would be very grateful, as you can tell i don't actually know how the script works, I've just 'bodged' it through research!

    My current echo code is ..

    <?php
    // Retrieve all the data from the "WP" table
    $result = mysql_query("SELECT * FROM wp_posts order by id desc limit 1")
    or die(mysql_error());

    // store the record of the "WP" table into $row
    $row = mysql_fetch_array( $result );

    // Print out the contents of the entry

    echo "<h2><a href='$row[guid]'>$row[post_title]</a></h2>"; ?>
    <p>
    <?php
    if (strlen($row['post_content'])>230)
    echo substr($row['post_content'], 0, 200) . '…';
    else
    echo $row['post_content'];

    ?>
     
    hostedweb, Jan 30, 2010 IP
  2. USWGO

    USWGO Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think I can help you with this. I recently made both my wordpress websites excerpt only which limits the amount of words, even if you have an empty excerpt and it brings out the full post it won't show it all. I also added a read more link to my tag and category sections.

    If this is what you need help with then I will help you since I use a special theme and know what to do with it.
     
    USWGO, Feb 2, 2010 IP
  3. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    
    <?php
    // Retrieve all the data from the "WP" table
    $result = mysql_query("SELECT * FROM `wp_posts` order by post_date desc Limit 0, 5")
    or die(mysql_error()); 
    
    // store the record of the "WP" table into $row
    $row = mysql_fetch_array( $result );
    
    // Print out the contents of the entry 
    
    echo "<h2><a href='$row[guid]'>$row[post_title]</a></h2>"; ?>
    <p>
    <?php
    if (strlen($row['post_content'])>230)
    echo substr($row['post_content'], 0, 200) . '…';
    else
    echo $row['post_content'];
    
    ?>
    Code (markup):
     
    javaongsan, Feb 2, 2010 IP
  4. hostedweb

    hostedweb Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
     
    hostedweb, Feb 3, 2010 IP
  5. hostedweb

    hostedweb Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Would be interested in seeing your code?
     
    hostedweb, Feb 3, 2010 IP
  6. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #6
    Use space as a delimiter for each word
    
    $words = explode(' ', $row['post_content'], 200);
    foreach($words as $word)
    {
    	echo $word;
    }	
    	
    echo '…';
    
    Code (markup):
     
    javaongsan, Feb 3, 2010 IP
  7. hostedweb

    hostedweb Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for this, but when implemented the words have no spacing and they break out of my div, any ideas why?

    Thanks.
     
    hostedweb, Feb 8, 2010 IP
  8. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #8
    you can add space using ." " behind $word but I not sure about the breaking of div.

    
    $words = explode(' ', $row['post_content'], 200);
    foreach($words as $word)
    {
    	echo $word." ";
    }	
    	
    echo '…';
    
    Code (markup):
     
    javaongsan, Feb 8, 2010 IP
  9. hostedweb

    hostedweb Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Excellent! Thanks, that corrected the div problem and it now shows full words.

    Do you know how i can restrict the words as its now showing the full post as opposed to an excerpt?

    Also any ideas how i can show a few of the latest posts instead of just one?

    Thanks again for your help.

     
    hostedweb, Feb 9, 2010 IP
  10. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #10
    Try this
    <?php
    // Retrieve all the data from the "WP" table
    $result = mysql_query("SELECT * FROM `wp_posts` order by post_date desc Limit 0, 5")
    or die(mysql_error()); 
    
    // store the record of the "WP" table into $row
    while ($row = mysql_fetch_array($result)) {
    	// Print out the contents of the entry 
    	echo "<h2><a href='$row[guid]'>$row[post_title]</a></h2><p>";
    	
    	$words = explode(' ', $row['post_content'], 200);
    	if (count($words) > $excerpt_length) {
    			array_pop($words);
    			array_push($words, '[...]');
    			$content = implode(' ', $words);
    	}
    	echo $content."</p><br><br>";
    }
    ?>
    Code (markup):
     
    javaongsan, Feb 9, 2010 IP
  11. hostedweb

    hostedweb Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hi,

    Thanks again, this worked a charm. One last query, its pulling drafts from the database, is there any way to restrict it to published posts only?

    Thanks a lot for your help, its much appreciated.

    Tony.

     
    hostedweb, Feb 10, 2010 IP
  12. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #12
    Change your sql to
    "SELECT * FROM `wp_posts` where post_status = 'publish' order by post_date desc Limit 0, 5"
     
    javaongsan, Feb 10, 2010 IP
  13. hostedweb

    hostedweb Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Finished! Your a very patient guy and a credit to this forum. Thanks for your help. If you ever need any gratis hosting let me know :)
     
    hostedweb, Feb 11, 2010 IP
  14. hostedweb

    hostedweb Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Hi,

    This code seems to be picking up the permilink for some posts and the standard link for others, any ideas why it would be doing this?

    For example
    Some links are http://www.mysite.co.uk/blog/?p=1544
    Others are like http://www.mysite.co.uk/blog/post-name-permilink/

    Thanks,
    Tony.
     
    hostedweb, Feb 18, 2010 IP
  15. hostedweb

    hostedweb Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    hostedweb, Mar 2, 2010 IP
  16. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #16
    you can use this code in mysql to fixed the data
    update  `wp_posts` set `guid` = concat( ' http://www.mysite.co.uk/blog/', `post_name` )
    Code (markup):
     
    javaongsan, Mar 3, 2010 IP
  17. hostedweb

    hostedweb Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Brilliant! Cheers for helping me again.
     
    hostedweb, Mar 4, 2010 IP