我正在使用以下代码在我的内容中放置一些广告代码。
<?php
$content = apply_filters('the_content', $post->post_content);
$content = explode (' ', $content);
$halfway_mark = ceil(count($content) / 2);
$first_half_content = implode(' ', array_slice($content, 0, $halfway_mark));
$second_half_content = implode(' ', array_slice($content, $halfway_mark));
echo $first_half_content.'...';
echo ' YOUR ADS CODE';
echo $second_half_content;
?>
我该如何修改它,以便我可以在纯文本段落之间同时放置 2 个广告(两个包含广告的 <p>...</p> 不应包含图像或嵌入式视频)。
我想避免使用 jQuery。
我的用例示例...
最佳答案
这是我对这个问题的看法。抱歉发帖有点晚了(并且错过了赏金 :-( ),但这是忙碌的一周,所以我把所有的事情都零零碎碎地做了。
快速结束
我没有删除第一段中的附件。我真的不明白为什么必须为了广告而破坏内容
我在我的代码中做了很多检查,以尝试确保广告仅插入纯文本段落之间。纯文本段落被认为是不包含 img、li 和 ul 标签的段落
我已正确记录每个代码块,因此您可以轻松完成每个部分。我还添加了 todo如有必要,您需要注意的文档 block
我用过wptexturize将 p 标签应用于 the_content .每个双换行符构成一个段落
根据需要修改和使用此代码。我已经测试过了,所以我这边没有错误。
这是代码。希望这对您有用。 *附言!所有这些都进入您的 functions.php。您的模板文件不需要任何其他代码或模组
<?php
add_filter( 'the_content', 'so_25888630_ad_between_paragraphs' );
function so_25888630_ad_between_paragraphs($content){
/**-----------------------------------------------------------------------------
*
* @author Pieter Goosen <http://stackoverflow.com/users/1908141/pieter-goosen>
* @return Ads in between $content
* @link http://stackoverflow.com/q/25888630/1908141
*
* Special thanks to the following answers on my questions that helped me to
* to achieve this
* - http://stackoverflow.com/a/26032282/1908141
* - http://stackoverflow.com/a/25988355/1908141
* - http://stackoverflow.com/a/26010955/1908141
* - http://wordpress.stackexchange.com/a/162787/31545
*
*------------------------------------------------------------------------------*/
if( in_the_loop() ){ //Simply make sure that these changes effect the main query only
/**-----------------------------------------------------------------------------
*
* wptexturize is applied to the $content. This inserts p tags that will help to
* split the text into paragraphs. The text is split into paragraphs after each
* closing p tag. Remember, each double break constitutes a paragraph.
*
* @todo If you really need to delete the attachments in paragraph one, you want
* to do it here before you start your foreach loop
*
*------------------------------------------------------------------------------*/
$closing_p = '</p>';
$paragraphs = explode( $closing_p, wptexturize($content) );
/**-----------------------------------------------------------------------------
*
* The amount of paragraphs is counted to determine add frequency. If there are
* less than four paragraphs, only one ad will be placed. If the paragraph count
* is more than 4, the text is split into two sections, $first and $second according
* to the midpoint of the text. $totals will either contain the full text (if
* paragraph count is less than 4) or an array of the two separate sections of
* text
*
* @todo Set paragraph count to suite your needs
*
*------------------------------------------------------------------------------*/
$count = count( $paragraphs );
if( 4 >= $count ) {
$totals = array( $paragraphs );
}else{
$midpoint = floor($count / 2);
$first = array_slice($paragraphs, 0, $midpoint );
if( $count%2 == 1 ) {
$second = array_slice( $paragraphs, $midpoint, $midpoint, true );
}else{
$second = array_slice( $paragraphs, $midpoint, $midpoint-1, true );
}
$totals = array( $first, $second );
}
$new_paras = array();
foreach ( $totals as $key_total=>$total ) {
/**-----------------------------------------------------------------------------
*
* This is where all the important stuff happens
* The first thing that is done is a work count on every paragraph
* Each paragraph is is also checked if the following tags, a, li and ul exists
* If any of the above tags are found or the text count is less than 10, 0 is
* returned for this paragraph. ($p will hold these values for later checking)
* If none of the above conditions are true, 1 will be returned. 1 will represent
* paragraphs that qualify for add insertion, and these will determine where an ad
* will go
* returned for this paragraph. ($p will hold these values for later checking)
*
* @todo You can delete or add rules here to your liking
*
*------------------------------------------------------------------------------*/
$p = array();
foreach ( $total as $key_paras=>$paragraph ) {
$word_count = count(explode(' ', $paragraph));
if( preg_match( '~<(?:img|ul|li)[ >]~', $paragraph ) || $word_count < 10 ) {
$p[$key_paras] = 0;
}else{
$p[$key_paras] = 1;
}
}
/**-----------------------------------------------------------------------------
*
* Return a position where an add will be inserted
* This code checks if there are two adjacent 1's, and then return the second key
* The ad will be inserted between these keys
* If there are no two adjacent 1's, "no_ad" is returned into array $m
* This means that no ad will be inserted in that section
*
*------------------------------------------------------------------------------*/
$m = array();
foreach ( $p as $key=>$value ) {
if( 1 === $value && array_key_exists( $key-1, $p ) && $p[$key] === $p[$key-1] && !$m){
$m[] = $key;
}elseif( !array_key_exists( $key+1, $p ) && !$m ) {
$m[] = 'no-ad';
}
}
/**-----------------------------------------------------------------------------
*
* Use two different ads, one for each section
* Only ad1 is displayed if there is less than 4 paragraphs
*
* @todo Replace "PLACE YOUR ADD NO 1 HERE" with your add or code. Leave p tags
* @todo I will try to insert widgets here to make it dynamic
*
*------------------------------------------------------------------------------*/
if( $key_total == 0 ){
$ad = array( 'ad1' => '<p>PLACE YOUR ADD NO 1 HERE</p>' );
}else{
$ad = array( 'ad2' => '<p>PLACE YOUR ADD NO 2 HERE</p>' );
}
/**-----------------------------------------------------------------------------
*
* This code loops through all the paragraphs and checks each key against $mail
* and $key_para
* Each paragraph is returned to an array called $new_paras. $new_paras will
* hold the new content that will be passed to $content.
* If a key matches the value of $m (which holds the array key of the position
* where an ad should be inserted) an add is inserted. If $m holds a value of
* 'no_ad', no ad will be inserted
*
*------------------------------------------------------------------------------*/
foreach ( $total as $key_para=>$para ) {
if( !in_array( 'no_ad', $m ) && $key_para === $m[0] ){
$new_paras[key($ad)] = $ad[key($ad)];
$new_paras[$key_para] = $para;
}else{
$new_paras[$key_para] = $para;
}
}
}
/**-----------------------------------------------------------------------------
*
* $content should be a string, not an array. $new_paras is an array, which will
* not work. $new_paras are converted to a string with implode, and then passed
* to $content which will be our new content
*
*------------------------------------------------------------------------------*/
$content = implode( ' ', $new_paras );
}
return $content;
}
编辑
来自您的评论:
你应该使用下面的代码来调试<pre><?php var_dump($NAME_OF_VARIABLE); ?></pre> .对于您的第一期,我将使用 ?><pre><?php var_dump($paragraphs); ?></pre><?php就在这一行之后 $paragraphs = explode( $closing_p, wpautop($content) );以准确查看内容是如何拆分的。这会让您了解您的内容是否被正确分割。
同时使用 ?><pre><?php var_dump($p); ?></pre><?php就在这一行之后 $p = array();检查赋予特定段落的值。请记住,带有 img、li 和 ul 标签的段落应该有一个 0。 ,还有少于 10 个单词的段落。其余的应该有一个 1
编辑 -> 删除段落的问题已解决。感谢您向我指出该缺陷。从来没有意识到这一点。我已经更新了代码,所以只需复制并粘贴即可
编辑 2
请注意,您无法使用您正在使用的在线工具测试您的代码。这不是 the_content() 输出内容的真实反射(reflect)。 .您使用在线工具看到的输出在发送到屏幕之前会被过滤和标记为 the_content() .如果您使用 google chrome 等浏览器检查输出,您会看到 p 标签已正确应用。
我还更改了代码中带有图像标签的a标签
关于php - 在纯文本段落之间放置广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25888630/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行
📢博客主页:https://blog.csdn.net/weixin_43197380📢欢迎点赞👍收藏⭐留言📝如有错误敬请指正!📢本文由Loewen丶原创,首发于CSDN,转载注明出处🙉📢现在的付出,都会是一种沉淀,只为让你成为更好的人✨文章预览:一.分辨率(Resolution)1、工业相机的分辨率是如何定义的?2、工业相机的分辨率是如何选择的?二.精度(Accuracy)1、像素精度(PixelAccuracy)2、定位精度和重复定位精度(RepeatPrecision)三.公差(Tolerance)四.课后作业(Post-ClassExercises)视觉行业的初学者,甚至是做了1~2年
我有这个代码:context"Visitingtheusers#indexpage."dobefore(:each){visitusers_path}subject{page}pending('iii'){shouldhave_no_css('table#users')}pending{shouldhavecontent('Youhavereachedthispageduetoapermissionic错误')}它会导致几个待处理,例如ManagingUsersGivenapractitionerloggedin.Visitingtheusers#indexpage.#Noreason
由于匿名block和散列block看起来大致相同。我正在玩它。我做了一些严肃的观察,如下所示:{}.class#=>Hash好的,这很酷。空block被视为Hash。print{}.class#=>NilClassputs{}.class#=>NilClass为什么上面的代码和NilClass一样,下面的代码又显示了Hash?puts({}.class)#Hash#=>nilprint({}.class)#Hash=>nil谁能帮我理解上面发生了什么?我完全不同意@Lindydancer的观点你如何解释下面几行:print{}.class#NilClassprint[].class#A
在许多ruby类之间共享记录器实例的最佳(正确)方法是什么?现在我只是将记录器创建为全局$logger=Logger.new变量,但我觉得有更好的方法可以在不使用全局变量的情况下执行此操作。如果我有以下内容:moduleFooclassAclassBclassC...classZend在所有类之间共享记录器实例的最佳方式是什么?我是以某种方式在Foo模块中声明/创建记录器还是只是使用全局$logger没问题? 最佳答案 在模块中添加常量:moduleFooLogger=Logger.newclassAclassBclassC..
我一直在玩一个脚本,它在Chrome中获取选定的文本并在Google中查找它,提供四个最佳选择,然后粘贴相关链接。它以不同的格式粘贴,具体取决于当前在Chrome中打开的页面-DokuWiki打开的DokuWiki格式,普通网站的HTML,我想要我的WordPress所见即所得编辑器的富文本。我尝试使用pbpaste-Preferrtf来查看没有其他样式的富文本链接在粘贴板上的样子,但它仍然输出纯文本。在文本编辑中保存文件并进行试验后,我想出了以下内容text=%q|{\rtf1{\field{\*\fldinst{HYPERLINK"URL"}}{\fldrsltTEXT}}}|te