简体   繁体   English

使用php在自定义内容中创建动态“阅读更多”链接

[英]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. 所以我有一个自定义的CMS系统,基本上有一个正常的WYSIWYG(tinymce),用户可以写他们的博客文章。 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. 我已经查看了“分页”这篇文章,但我找不到任何过去“你必须自己编写这些功能”,所以我的第一个想法就是添加两个textareas,但这不太方便。

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. 我可以使用php或jquery或其他任何东西将你的代码转换为链接,然后使用htaccess文件来处理URL的其余部分。

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 tempus 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). 如果我理解正确,你想在PHP中生成这个并在javascript中处理更多的读取(特别是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. javascript看起来像这样,快速切换。

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. 我认为迈克尔是正确的,除非我理解你想要在点击链接时创建一个新窗口,所以我认为实现这一目标的最佳方法可能是将帖子ID或其所拥有的任何唯一标识符传递给新页面执行sql查询以检索帖子。

This is easiest through a get string: 这通过get字符串最简单:

<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. 如果您想在新页面上显示整个帖子而不仅仅是截止部分,那么只需将它们发送到该页面并传递帖子ID即可执行查询。

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? 我完全不明白你想做什么...点击“阅读更多”链接并显示之前隐藏的第二个p-tag? Is that correct?! 那是对的吗?!

<a href="javascript:void(0);" onclick="jsFunction()">read more</a>

The jsFunction() is a placeholder for displaying the p-tag. jsFunction()是一个用于显示p-tag的占位符。 What do you want so say with the span-element :-)? 你想用span-element怎么说:-)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM