解析WordPress的发布内容 [英] Parsing WordPress post content

查看:74
本文介绍了解析WordPress的发布内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使在规划阶段,我也有一个奇怪的布局可以避开,并且处于亏损状态。本质上,我需要分离出所有不是 .gallery 的内容,并将其放入< aside /> 。我最初考虑使用 edit_post 插件钩从插件API ,但后来决定反对它,因为这个内容的变化是特定的布局,我想保持一个干净的数据库。 所以...



如何解析WP的 the_content 名为.gallery ?诚然,不是一个PHP的家伙,所以我倍加感谢帮助!






根据Michael的评论 - 这里是WP的一个例子

<$ p $ > < div class =entry-content>
< div class =gallery>
< dl class =gallery-item>
< dt class =gallery-icon portrait>
< img src =/ imagePath / etc.jpgclass =attachment-thumbnail>
< / dt>
< / dl>
< dl class =gallery-item>
< dt class =gallery-icon portrait>
< img src =/ imagePath / etc.jpgclass =attachment-thumbnail>
< / dt>
< / dl>
< dl class =gallery-item>
< dt class =gallery-icon portrait>
< img src =/ imagePath / etc.jpgclass =attachment-thumbnail>
< / dt>
< / dl>
< / div>
< p> Curabitur vulputate,ligula lacinia scelerisque tempor,lacus lacus ornare ante,ac egestas est urnana amet arcu。每个conubia nostra,每个inceptos himenaeos班级taciti sociosqu广告litora扭矩。 Sed m​​olestie augue sit amet。< / p>
< ul>
< li>项目A< / li>
< li>项目B< / li>
< li>项目C< / li>
< / ul>
< / div>

所需输出

 < div class =entry-content> 
< div class =gallery>
< dl class =gallery-item>
< dt class =gallery-icon portrait>
< img src =/ imagePath / etc.jpgclass =attachment-thumbnail>
< / dt>
< / dl>
< dl class =gallery-item>
< dt class =gallery-icon portrait>
< img src =/ imagePath / etc.jpgclass =attachment-thumbnail>
< / dt>
< / dl>
< dl class =gallery-item>
< dt class =gallery-icon portrait>
< img src =/ imagePath / etc.jpgclass =attachment-thumbnail>
< / dt>
< / dl>
< / div>
<预计>
< p> Curabitur vulputate,ligula lacinia scelerisque tempor,lacus lacus ornare ante,ac egestas est urnana amet arcu。每个conubia nostra,每个inceptos himenaeos班级taciti sociosqu广告litora扭矩。 Sed m​​olestie augue sit amet。< / p>
< ul>
< li>项目A< / li>
< li>项目B< / li>
< li>项目C< / li>
< / ul>
< / aside>
< / div>


解决方案

您需要使用 Dom Parser 。以下是您如何使用标记作为示例进行操作的示例。测试取得了理想的结果,所以希望这会给你带来你需要的开始:

  add_filter('the_content','wrap_nongallery_aside ',20); 
函数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();
}


I've got a weird layout to get around and am at a loss, even in the planning stage. Essentially I need to separate out all content that's not a .gallery and put it into an <aside />. I initially considered a plugin using the edit_post hook from the Plugin API, but have since decided against it because this content change is layout specific and I want to maintain a clean database. So...

How can I parse through WP's the_content for content that's not .gallery? Admittedly not a PHP guy, so I doubly appreciate the help!


As per Michael's comment below - here's an example of WP's the_content class output:

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>

Desired Output

<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>

解决方案

You'll want to use a Dom Parser for this. Here's an example in how you can go about it using your markup as an example. Testing yielded the desired results, so hopefully this will give you the head start you need:

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();
}

这篇关于解析WordPress的发布内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆