English 中文(简体)
Differentiating between P tags wrapping images and P tags wrapping text nodes in Wordpress?
原标题:

I need a good solution to be able to style <p> tags differently in a wordpress post. More specifically I need to differentiate between paragraphs and images. Wordpress just wraps all new lines in <P> tags.

Here are some possibilities:

  • Strip all <p> tags and add <P> tags where appropriate (around text)
  • Add a class on <P> tags that wrap text
  • Somehow differentiate between them as is (p img does not work.. I need to select the p tags that have a img child... I d rather not use selectors that don t work in IE 6)
  • Other solutions?

I do not want blog contributors to have to do something manually (add div tags), I want this done in the background.

I know other people are having problems with this. Please let me know your thoughts!

Thanks all, Matt Mueller

最佳回答

You could create a content filter function something like the below in your theme s functions.php file or in a plugin. Note that this will add a class to any paragraph in the post body which has an image as it s first child element (providing no text comes first), which may not be the exact behaviour you want.

function my_content_filter($content) {
    return preg_replace( |<p>(<img[^<]*)</p>|i ,  <p class="foo">${1}</p> , $content);
}
add_filter( the_content ,  my_content_filter );
问题回答
function format_content($content) {
    $contentArr = explode("
", $content);
    $output = "";
    foreach ($contentArr as $entity) {
        $entity = trim($entity);
        $numChars = strlen($entity);
        if(substr($entity, 0, 1) != "<" && substr($entity, $numChars-1) != ">" && $numChars > 0)
            $entity =  <p> .$entity. </p> ;
        $output .= $entity;
    }
    return $output;
}

I m sure I ll run into some corner cases but this works pretty well for the time being.. It uses get_the_content() to prevent p tags from being added, then it passes the content to this function, which wraps stray text that doesn t start with a tag in p tags, then returns.

What about a simple plugin that utilizes jQuery s unwrap? http://wordpress.org/extend/plugins/unwrap-images/

DISCLAIMER: I did develop this plugin for my own needs.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签