0

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!

3
  • FWIW, I'm using this in Wordpress, and will be putting text in the h2 element from the WordPress GUI, so the PHP scripting will have to work after the item has been posted. Commented Sep 7, 2011 at 22:08
  • 1
    Does anything else go inside the divs? If not, why not just use CSS to define how you want h2 headers to look? Commented Sep 7, 2011 at 22:11
  • Actually trying to use two separate background styles. one for the h2 and then one for the div class "entry header" Commented Sep 7, 2011 at 22:26

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

I agree. Same thing can be done via JavaScript replace() function, in addition.
0

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

2 Comments

how would I apply that only to the <h2>s in that div entry-content?
var h2 = $$('entry-content h2'); $(h2).insert ({'before' : '<div class="entry-header">'} ); $(h2).insert ({'after' : '</div>'} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.