简体   繁体   中英

Add class to html element with PHP

Is there an easy way to automatically wrap any h2 element in the div class "entry-content" in another div class "entry-header"

So the end result would look something like:

<div class="entry-content">
  <div class="entry-header">
    <h2>Some Title</h2>
  </div>
</div>

I assume this can be done with PHP, but I'm not sure. Thanks for any input!

In terms of wordpress I would probably verge towards creating a shortcode such as

[header]Some Title[/header]

I would make the shortcode take the content and wrap the given code around it.

See some documentation here: http://codex.wordpress.org/Shortcode_API

ugly one:

$content = str_replace('<div class="entry-content">', '<div class="entry-content"><div class="entry-header">', $content);
$content = str_replace('</h2>', '</h2></div>', $content);

Can you do it in prototype ? This would be my easy solution:

var div = $('entry-content');

$(div).insert ({'top'  : '<div class="entry-header">'} );
$(div).insert ({'bottom'  : '</div>'} );

Maybe I'm missing somethig :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM