Create a dynamic 'Read More' link within custom content using php
Hope the title could explain a little of what i'm looking to accomplish. So i have a custom CMS system and basically have a normal WYSIWYG (tinymce) where users can write their blog posts. I have looked into the 'page break' piece but I could not find anything past 'you have to write that functionality yourself' so my first thoughts were to add two more textareas, but that wouldnt be too convinient.
then i thought well if there is something that could be added to the wysiwyg like:
<span id='break1"></span>
i could use php or jquery or whatever have you to turn that code into a link then take care of the rest of it using the htaccess file to deal with the URL's.
I am pretty much out of ideas on how to deal with this, so any information, thoughts to this would be greatly appreciated.
here is an example of what it 'kind of' should do:
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<a href="/seo-title/2/">read more</a>
<!-- people would not see this part until the 'read more' link was clicked. -->
<!-- will open a new page NOT just hidden content -->
<p>Duis scelerisque, in te开发者_JAVA百科mpus ante tortor eget tortor. Donec eu consequat augue.</p>
If i understand correctly, you want to generate this in php and handle the read more in javascript (specifically jquery). I wrote a basic snippet that you can build upon.
<p><?php echo substr(CONTENT,0,255); ?></p>
<?php if (strlen(CONTENT) > 255): ?>
<a class="readmore">read more</a>
<p class="more"><?php echo substr(CONTENT,256); ?></p>
<?php endif; ?>
The javascript would look something like this, a quick toggle.
var $readmore = $('.readmore');
$readmore.bind('click',function(){
var $next = $(this).next('p');
$next[$next.is(':visible') ? 'hide' : 'show']();
});
The way I do it for the summaries on my blog is using a comment:
<!--more-->
Then I have a function to get just the summary of the blog post:
$i = strpos($contents, '<!--more-->');
if ($i !== false) {
$i += strlen('<!--more-->');
return substr($contents, 0, $i);
}
else return $contents;
I think Michael has it correct, except if I understand you want to create a new window when that link is clicked, so I think the best way to accomplish that is probably to pass the post ID or whatever unique identifier it has to a new page that executes an sql query to retrieve the post.
This is easiest through a get string:
<a href="fullpost.php?id=<?php echo $id; ?>" target="_blank">Read More</a>
Then us Michael's substr function there to cut the post where you wanted to cut it.
Another idea that you might want to look into is if you're looking to increase load time by cutting your posts short, then you could consider ajaxing in the content to a hidden frame that has an animated slide down or something like that. That's what I do on my page.
If you wanted to display the whole post on a new page and not just the cut-off part then just send them to that page and pass the post id to execute the query.
I don't understand completely what you want to do... click "read more" link and show up the second p-tag, which was hidden before? Is that correct?!
<a href="javascript:void(0);" onclick="jsFunction()">read more</a>
The jsFunction() is a placeholder for displaying the p-tag. What do you want so say with the span-element :-)?
精彩评论