问题描述
我需要一个很好的解决方案,以便能够在wordpress文章中不同地样式< p>
。更具体地说,我需要区分段落和图像。 Wordpress只是包装< P>
标签中的所有新行。
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.
这里有一些可能性:
- 除去所有
< p>
标记,并添加< P&
- 在
< P>
- 以某种方式区分它们之间的差异(p img不工作..我需要选择p标签有一个img孩子...我宁愿不使用选择器, t工作在IE 6)
- 其他解决方案?
- 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 know other people are having problems with this. Please let me know your thoughts!
推荐答案
感谢所有,
Matt Mueller
您可以在主题的 functions.php
文件或插件中创建类似于下面的内容过滤器函数。请注意,这将添加一个类到帖子正文中的任何段落,它有一个图像作为它的第一个子元素(不提供任何文本第一),这可能不是你想要的确切行为。
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');
这篇关于区分P标签包装图像和P标签包装Wordpress中的文本节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!