Hi.
I want to set read more for my cms posts.
The code i found was <?php the_content( $more_link_text , $strip_teaser ); ?> but still don't know how and where to use it.
Any suggestion?

Recommended Answers

All 18 Replies

<?php the_content( $more_link_text , $strip_teaser ); ?> is for wordpress usage.

<?php the_content( $more_link_text , $strip_teaser ); ?>

Codes like this is from wordpress. it has its own codes for it CMS.

The Usage of that code is Like a movie trailer. in order to see the full description you need to click the $more_link_text + $strip_teaser to see the full content of that link

I wonder you 'read more' button will behave like one of the following?
1. link to actual post
2. it will trigger ajax to get rest of the post and append to it
3. show the rest of the post(The rest of post already loaded in div which hidden with css).

So what can i do for my cms posts? I want it to link to actual post like what we see it wordpress.

Well i think you should trim some of the words. then add the read more

well ill do it this way though

// strip tags to avoid breaking any html
$string = strip_tags($string);

if (strlen($string) > 500) {

    // truncate string
    $stringCut = substr($string, 0, 500);

    // make sure it ends in a word so assassinate doesn't become ass...
    $string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="/this/story">Read More</a>'; 
}
echo $string;

this is just an example you can modify or make your own codes this is just a template of what you want

If it is link back to original post, then what you should do is
1. select all post from database.
2. crop the content based on character count(substr()) or what so ever filter way you need.
3. generate a link to the actually single post view.

@joshuajames.delacruz, would you please explain strrpos() for me?
I have read about it.
strrpos(string,find,start) please give me an example that contains the start part.

strrpos($stringCut, ' ') means that variable $stringcut holds a text, and says come backwards from the end of that text untill to find space, then cut the text exactly right there to be sure no words will be cut, and cutting will happen from the last space in the text, correct? Did i understand it correctly?
So just want an example of strrpos(string,find,start) to understand what the start parameter says.

What is the usage of strip_tags()?
We can use this:
$string = "full text";
instead of this:
$string = strip_tags($string);
What do you mean by:

to avoid breaking any html

Should not be using strrpos(), as from http://php.net/manual/en/function.strrpos.php, strrpos only search the character from string and return the index. we should probably do: $short = substr($content_from_db, 0, 10)."..." read the http://php.net/manual/en/function.substr.php for substr api.

The code provided will crop the string of $content_from_db from 0-th position for 10 characters. Means if $content_from_db = "I am here to help", then the $short will having "I am here ..." as its value.

But @lps, what about breaking words?
Without using $string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="/this/story">Read More</a>'; the text breaks in the middle of words.

This is what i tested:

    // strip tags to avoid breaking any html
    $string = strip_tags($row['Content']);
    if (strlen($string) > 60) {
    // truncate string
    $stringCut = substr($string, 0, 60).'... <a href="/this/story">Read More</a>';
    // make sure it ends in a word so assassinate doesn't become ###...
    //$string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="/this/story">Read More</a>';
    }
    echo $stringCut;

As you see i didn't use the last line of code and the result was:

ONE DOLLAR AND EIGHTY-SEVEN CENTS. THAT WAS ALL. AND SIXTY C... Read More

So without using strrpos() and strrpos($stringCut, ' ') here, how can i cut the text without breaking words? Is there any better way to use?

What i did finally was:

    foreach($result as $key => $row) {
                   echo "
                        <tr>
                            <td>$row[ID]
                            <td><a href='single-post-page.php?post=" . $row['ID'] . "'>" . $row['Title'] . "</a>
                            <td>$row[Author]
                            <td>" . substr($row['Content'], 0, 60) . "... <a href='/this/story'>Read More</a>
                         ";
    } 

But then i changed it into this:

    foreach($result as $key => $row) {
                   echo "
                        <tr>
                            <td>$row[ID]
                            <td><a href='single-post-page.php?post=" . $row['ID'] . "'>" . $row['Title'] . "</a>
                            <td>$row[Author]
                            <td>" . substr($row['Content'], 0, (strrpos(substr($row['Content'], 0, 60), ' '))) . "... <a href='/this/story'>Read More</a>
                         ";
    } 

Is it ok @lps or is better to use the first model?
With the first model, the text will be broken in the middel of words but with the second model, no word breaking happen.

I think that depends on what you want @Niloofar24

@lps covered all the work here thou

Yes, it is totally depends on what you want to do. For me, I will use the first methold as for me, it is hard to mantain the wordings while matching the excerpt with the stylings. For example: If you match 4 words, "I am here to..." will having 1 line, while "Andrew plays american football..." will probably taken 2 or more rows which cause me more trouble to style it.
So, in the end, still depends on what you need.

I understood, but @lps, if you use the first model, it may breaks a word, am i right?! How would you solve it for yourself?
I mean if you want to set Read more for your posts, then what would you do for that? It's correct that it depends on what we like to have but even with any style you want to use, if you use the first model you may have problem with word breaking.

I'm not sure if i could point into the question that is in my mind.
What is your own solution for avoiding word breaking With using substr($row['Content'], 0, a number)?

Well Lets see

It could be done with a regex, something like this will get up to 260 characters from the start of string up to a word boundary:

$line = $body; //$row['Content'] 
//from min boundery 1st char up to max boundery 
//   260 char you may change the max boundery
if (preg_match('/^.{1,260}\b/s', $body, $match))
{
    $line=$match[0];
}

Alternatively, you could maybe use the wordwrap function to break your $body into lines, then just extract the first line.

Hope this helps

For me, I don't mind if the words are cut off as long as users knows you cut off the word and how to read the entire post. This depends on how you style your page to look nicer. Complete words or not, doesn't really matter.

I understood. Before marking this discussion as solved, just want to ask one more question.

@joshuajames.delacruz, would you please explaine this part of code for me? Specially this part ('/^.{1,260}\b/s', $body, $match).
I read about preg_match() but didn't understand it clearly.

    $line = $body; //$row['Content']
    //from min boundery 1st char up to max boundery
    // 260 char you may change the max boundery
    if (preg_match('/^.{1,260}\b/s', $body, $match))
    {
    $line=$match[0];
    }

preg_match('/^.{1,260}\b/s', $body, $match);

ill start piece by piece

/^ this is the start of your variable whitch is the $body
. the terminator or the ; in php to start another statement
{1,260}\b this is the boundery form the very beggining of your $body up to the end the
\s - Whitespace e.g (space, tab) it will also count it on the $body

hope this is the information you needed

Thank you @joshuajames.delacruz.

Thank you @lps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.