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']; ?>
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.
<?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):
Use space as a delimiter for each word $words = explode(' ', $row['post_content'], 200); foreach($words as $word) { echo $word; } echo '…'; Code (markup):
Thanks for this, but when implemented the words have no spacing and they break out of my div, any ideas why? Thanks.
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):
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.
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):
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.
Change your sql to "SELECT * FROM `wp_posts` where post_status = 'publish' order by post_date desc Limit 0, 5"
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
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.
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):