我需要帮助我在我的functions.php
文件中使用的2个WordPress功能,根据段落数量在博客文章中推送广告代码。
您目前使用的是什么代码?
以下是我目前在functions.php
文件中使用的代码:
/*Add ad after 20 paragraph of post if there is more than 21 paragraph*/
add_filter( 'the_content', 'ad_20', 15 );
function ad_20( $content ) {
global $post;
if( check_paragraph_count_blog( $content ) > 21 ) {
$ad_code = '...ad code goes here...';
if ( $post->post_type == 'post' ) {
return prefix_insert_after_paragraph( $ad_code, 20, $content );
}
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
//Check paragraph count on a blog post
function check_paragraph_count_blog( $content ) {
global $post;
if ( $post->post_type == 'post' ) {
$count = substr_count( $content, '</p>' );
return $count;
} else {
return 0;
}
}
您的代码有什么问题?
好吧,我的代码工作正常,没有任何错误,但它不符合我想要的完整目的。
您希望代码做什么?
我目前正在使用和发布的代码的主要问题是prefix_insert_after_paragraph()
功能和&amp; check_paragraph_count_blog()
功能检查所有p
代码,无论它们位于何处。但这不是我想要的,我想要以下内容:
p
,<code>
,<pre>
,<code class="some-language-name">
中的<pre class="some-language-name>
代码。p
代码中的div
代码,例如<div class="callout some-class some-other-class">
。 某些div
代码有什么问题?
好吧,我在文章中使用了几个短代码来显示一些精心设计的音符,标注等等。现在,如果计数器考虑那些div进行计数,那么它可能会在短代码设计中显示广告,使整个外观和感觉变坏。
示例段落输入
<p>At the time of creating any blog or news based websites most webmasters gives the least amount of importance to the commenting system of their website, without even understanding the importance of it. Eventually comment section of a website is the only place where people interact with the author when they are exited or happy with the article and helps to grow the whole website community. In most cases they end up using some third party commenting system like Disqus or Spot.im etc. without even realizing what a blunder they are making. I’ve seen many websites (both big & popular as well as small websites) using Disqus commenting system, without even realizing the consequences. And by the time you will realize it, your site would have become so big & popular they you can’t take the risk of changing your commenting system. If you are thinking why, keep reading.</p>
<p><a href="I want to omit this P from counting"><img src="I want to omit this p from counting"></a></p>
<p>As creating websites has become very easy now-a-days many non-techy people can make a websites too, but they don’t get the insights of an experienced personal. Before writing this article I’ve used disqus for months to research it thoroughly and at the same time I’ve also tried Spot.im (a new player in this arena) but in both cases I’ve come up with the same conclusion. Never ever use these third party commenting system on your website. Here are the 7 facts about Disqus and similar commenting system for which I will suggest you to stay away from them.</p>
您对我们有什么要求?
我需要你的帮助。如果有人能够为我提供prefix_insert_after_paragraph()
和check_paragraph_count_blog()
函数的重写版本将会非常有用,它会对p
标记进行计数并通过省略上述条件进行检查。
提前感谢您,期待您的帮助。
关于下面发布的答案的一些更新
下面发布的答案可以正常运行,没有任何问题,但请注意它只能使用一次。例如,如果您想在博文中推送3个广告,从而创建了3个函数,例如ad_10()
,ad_20()
和ad_30()
,则以下代码仅适用于其中任何一个。如果您在WordPress functions.php
中添加了多个功能,则可能会显示空白内容。要记住的事情。
答案 0 :(得分:1)
使用DOMDocument
- 而不是正则表达式 - 您可以轻松处理这项工作。我们的想法是选择不属于这些特定元素的所有p
标记,换句话说,选择不属于此类父级的所有p
标记。
这一切都是通过XPath查询完成的:
//p[
not(
ancestor::div[contains(@class, 'callout') or contains(@class, 'callin')]
or ancestor::pre
or ancestor::code
or a/img # As per comments
)
]
如果您发现这是一个否定的查询,它会查找p
个div
的所有callout
元素callin
或{ {1}}类(您可以按照类似的语法添加更多类),pre
或code
元素(注意:所有pre
和code
元素)
顺便说一下,您不需要任何其他功能,所有事情都在ad_20()
正则表达式不是针对这种复杂情况(HTML解析)的工具。我不是说你不能解析HTML。除非你完全了解自己在做什么,否则你可以这样做。
<强> Live demo 强>
add_filter('the_content', 'ad_20', 15);
function ad_20($content) {
global $post;
$adCode = '...ad code goes here...';
// Ad code will be added right after 20th paragraph
$paragraphNumber = 20;
// Convert to HTML entities
$content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8');
if ($post->post_type == 'post') {
libxml_use_internal_errors(true);
// Initializing a new DOM object
$dom = new DOMDocument;
// Load HTML content
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
// Initializing a new XPath object
$xpath = new DOMXPath($dom);
// Query all `p` tags that their parent is not those specific elements
$paragraphs = $xpath->query('//p[not(ancestor::div[contains(@class, \'callout\') or contains(@class, \'callin\')] or ancestor::pre or ancestor::code or a/img)]');
// If we have a number of satisfying paragraphs
if ($paragraphs->length > $paragraphNumber) {
// Loading and importing javascript code
// <span> is important
$script = '<span>.........code.........</span>';
$newDom = new DOMDocument;
$newDom->loadHTML($script, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$node = $newDom->getElementsByTagName('span')->item(0);
$adNode = $dom->importNode($node, true);
// Add our ad node after `$paragraphNumber`th paragraph
$paragraphs->item($paragraphNumber)->parentNode->insertBefore($adNode, $paragraphs->item($paragraphNumber));
}
libxml_use_internal_errors(false);
return $dom->saveHTML();
}
return $content;
}