dongxianchu3541 2013-07-17 10:50
浏览 134

用条件替换<br /> <p> </ p>

In wordpress I am outputting some <div>s along the content of my shortcode. The problem is because wordpress puts paragraphs inside <p></p> tags, the line is broken where the <div> is. What I want to achieve is to replace <p> </p> which surrounds my shortcode with <br />. For example, if my content looks like this:

<p>[shortcodes attr='somethig']value[/shortcode] text<br />
[shortcodes attr='somethig']value[/shortcode] text<br />
text<br />
text [shortcodes attr='somethig']value[/shortcode] text </p>
<p>text<br />
text [shortcodes attr='somethig']value[/shortcode] text<br />
text</p>
<p> text without shortcode </p>
<p>text [shortcodes attr='somethig']value[/shortcode] text </p>

I need it to convert it in

<br/>[shortcodes attr='somethig']value[/shortcode] text<br />
[shortcodes attr='somethig']value[/shortcode] text<br />
text<br />
text [shortcodes attr='somethig']value[/shortcode] text <br />
<br/>text<br />
text [shortcodes attr='somethig']value[/shortcode] text<br />
text<br/>
<p> text without shortcode </p>
<br/>text [shortcodes attr='somethig']value[/shortcode] text <br/>

I tried to solve the problem by myself, but my preg_replace expression was capturing the whole content, instead of individual <p> </p> blocks.

Solutions with jQuery are alright too (but instead of shortcode, a <div> with a specific class must be contained in the paragraph)

LE: The following jQuery worked. However, I am still waiting for a more elegant solution in PHP.

$('p').each(function() {
    if ($(this).next().find('.measurement_value').length > 0) 
    {
        $(this).replaceWith(  '<br />' + $(this).html());
    }
});
  • 写回答

2条回答 默认 最新

  • doujie7346 2013-07-17 10:52
    关注

    Try this jquery code :

    $('p').each(function() {
        if ($(this).find('.class-of-the-div').length > 0) {
            $(this).replaceWith($(this).html() + '<br />');
        }
    });
    

    But, normally, Wordpress replace <br /> with <p></p> on server side.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP