3 Replies - 742 Views - Last Post: 30 March 2013 - 05:49 PM Rate Topic: -----

#1 Herbvine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-March 13

Limiting wordpress blog post to 250 characters then add a Read More...

Posted 29 March 2013 - 01:02 PM

Hi

what im trying to do is to limit the characters to 250 then add a read more... that will link to my corressponding wordpress blog post.

<?php

//start a loop that starts $i at 0, and make increase until it's at the number of rows
for($i=0; $i< $num_rows; $i++){ 

//assign data to variables, $i is the row number, which increases with each run of the loop
$blog_date = mysql_result($query_result, $i, "post_date");
$blog_title = mysql_result($query_result, $i, "post_title");
$blog_content = mysql_result($query_result, $i, "post_content");
//$blog_permalink = mysql_result($query_result, $i, "guid"); //use this line for p=11 format.

$blog_permalink = $blog_url . mysql_result($query_result, $i, "post_name"); //combine blog url, with permalink title. Use this for title format

//format date
$blog_date = strtotime($blog_date);
$blog_date = strftime("%b %e", $blog_date);

//the following HTML content will be generated on the page as many times as the loop runs. In this case 5.
?>

<div class="post"></div>  
  
        <span class="date">  <?php echo $blog_date; ?>:</code></span><br /><hr />   
  
        <a href="http://www.herbvine.com/blog/index.php"><?php echo $blog_title; ?></a><br /><br />  
  
        <?php echo $blog_content; ?> <br /><br />   
        
        <?php
} //end the for loop
?>


Thanks Herbvine

Is This A Good Question/Topic? 0
  • +

Replies To: Limiting wordpress blog post to 250 characters then add a Read More...

#2 Herbvine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-March 13

Re: Limiting wordpress blog post to 250 characters then add a Read More...

Posted 29 March 2013 - 01:07 PM

This is all on my website (index.php)

Thanks Herbvine
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: Limiting wordpress blog post to 250 characters then add a Read More...

Posted 29 March 2013 - 03:01 PM

Hey.

I think what you are looking for is the substr function. It'll let you extract only a specific part of a string, like for example the first 250 characters.

A downside to that simple method is that it is more than likely to result in the last word of the snippet being cut short. There are several ways to handle that, but a very simple method would be to explode the whole content on spaces, and then loop through the words and add them to the output string until you reach 250 chars. The logic of that might go:
Explode the main content on spaces.
For each word:
    If the length of the output plus the
       length of the current word is less
       than 250 characters:
           Add the current word to the output.
    Else:
       Break out of the loop


Was This Post Helpful? 3
  • +
  • -

#4 Herbvine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-March 13

Re: Limiting wordpress blog post to 250 characters then add a Read More...

Posted 30 March 2013 - 05:49 PM

Hi

Thanks for the reply.

I'm quite new to PHP and i'm not sure that i understand the function explode. Will i have to add all of my strings into an array

like this
Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)



The posts are brought though from WordPress and displayed on my index.php will this matter?

Also am i able to add ...Read More that is a link to the WordPress blog post?

Thanks Jake.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1