即使在规划阶段,我也有一种奇怪的布局可以绕过而且不知所措。基本上我需要将所有不是.gallery
的内容分开并将其放入<aside />
。我最初使用edit_post
hook from the Plugin API考虑了一个插件,但后来决定反对它,因为这个内容更改是特定于布局的,我想维护一个干净的数据库。的所以...
如何通过WP the_content
解析不是.gallery
的内容?不可否认,不是一个PHP家伙,所以我非常感谢你的帮助!
根据迈克尔下面的评论 - 这是WP the_content
班级输出的一个例子:
HTML
<div class="entry-content">
<div class="gallery">
<dl class="gallery-item">
<dt class="gallery-icon portrait">
<img src="/imagePath/etc.jpg" class="attachment-thumbnail">
</dt>
</dl>
<dl class="gallery-item">
<dt class="gallery-icon portrait">
<img src="/imagePath/etc.jpg" class="attachment-thumbnail">
</dt>
</dl>
<dl class="gallery-item">
<dt class="gallery-icon portrait">
<img src="/imagePath/etc.jpg" class="attachment-thumbnail">
</dt>
</dl>
</div>
<p>Curabitur vulputate, ligula lacinia scelerisque tempor, lacus lacus ornare ante, ac egestas est urna sit amet arcu. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed molestie augue sit amet.</p>
<ul>
<li>Item A</li>
<li>Item B</li>
<li>Item C</li>
</ul>
</div>
期望输出
<div class="entry-content">
<div class="gallery">
<dl class="gallery-item">
<dt class="gallery-icon portrait">
<img src="/imagePath/etc.jpg" class="attachment-thumbnail">
</dt>
</dl>
<dl class="gallery-item">
<dt class="gallery-icon portrait">
<img src="/imagePath/etc.jpg" class="attachment-thumbnail">
</dt>
</dl>
<dl class="gallery-item">
<dt class="gallery-icon portrait">
<img src="/imagePath/etc.jpg" class="attachment-thumbnail">
</dt>
</dl>
</div>
<aside>
<p>Curabitur vulputate, ligula lacinia scelerisque tempor, lacus lacus ornare ante, ac egestas est urna sit amet arcu. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed molestie augue sit amet.</p>
<ul>
<li>Item A</li>
<li>Item B</li>
<li>Item C</li>
</ul>
</aside>
</div>
答案 0 :(得分:5)
您需要使用Dom Parser。以下是一个示例,说明如何使用标记作为示例。测试产生了预期的结果,所以希望这将为您提供所需的先机:
add_filter( 'the_content', 'wrap_nongallery_aside', 20 );
function wrap_nongallery_aside($content){
$dom = new DOMDocument();
$dom->loadHTML($content);
$aside = $dom->createElement('aside');
$xpath = new DOMXPath($dom);
$not_gallery = $xpath->query('//div[@class="entry-content"]/*[not(contains(@class, "gallery"))]');
foreach($not_gallery as $ng){
$aside->appendChild($ng);
}
$dom->getElementsByTagName('div')->item(0)->appendChild($aside);
return $dom->saveHTML();
}
答案 1 :(得分:0)
Maiorano84的答案非常精彩,但在回答之前我制定了一种不太具体的方法,所以我觉得分享是好的。
我最初注销了插件方法,因为它需要更改帖子内容本身 - 不仅仅是输出的格式,还意识到插件独立于主题安装。下面是一个非常简单的,以开发人员为目标的插件,可将[aside /]
个短代码转换为HTML
个元素。它完全基于BSD Aside Sean D Burkin。我最终会为WP文本编辑器添加一个按钮并开源。
<?php
/*
Plugin Name: RW Content Aside
Description: Inserts aside formatting into post content via shortcodes
Author: Daniel Redwood
Version: 0.1
Author URI: http://www.rdwd.fm/
Based on SBD Aside by Sean B. Durkin:
Original Plugin: http://seanbdurkin.id.au/pascaliburnus2/archives/51
Author: http://www.seanbdurkin.id.au
*/
if ( !is_admin() ){
add_filter('the_content', 'handle_rw_aside');
}
function generate_random_str( $length=10)
{
return substr(md5(rand()), 0, $length);
}
function generate_place_marker()
{
return '@' . generate_random_str( 10) . '@';
}
function GetBody( $aside_instruction) {
return preg_replace( '~^((<p>)? \S+\s*=\s*.*?(<br \/>|<\/p>)\n?)*~mi', '', $aside_instruction);
}
function handle_rw_aside($the_content)
{
$begin = generate_place_marker();
$end = generate_place_marker();
$new_content = preg_replace(
'~^((<p>)?\[aside\](<br />|</p>))(.*?)(^(<p>)?\[\/aside\](<br />|</p>))~ms',
$begin . '$4' . $end,
$the_content);
$new_content = preg_replace_callback(
'~^(<p>)?(!+\[\/?aside\])~m',
function ($match) {
return $match[1] . substr( $match[2], 1);
},
$new_content);
$pattern = '~'.$begin.'(.*?)'.$end.'~s';
return preg_replace_callback(
$pattern,
function ($match) {
$aside_instruction = $match[1];
$body = GetBody( $aside_instruction);
$aside = '<aside class="contentAside">' . $body . '</aside>';
return $aside;
},
$new_content);
}
?>