如果您仍然想使用SO type backtick标记来设计内联代码示例的样式,我已经创建了一些代码来完成它。要使其成为您自己的插件,只需将下面的代码添加到您的函数中。php。它调用wordpress过滤器“the\\u content”,以便在显示内容时将转换应用于内容,从而防止任何转换存储在数据库中。
function style_my_inline($content){
//what you use to denote your code
$inline_marker = "`";
//regex for code
$pattern = "/".$inline_marker."[\\w\\D\\d]+?".$inline_marker."/";
preg_match_all($pattern,$content,$matches);
//what you want your surrounding markup to be
$prepend_tag = "<span class = \\"code\\">";
$append_tag = "</span>";
//for each occurance in preg match results...
foreach($matches as $match){
for($i=0;$i<count($match);$i++){
//remove inline marker from match text
$match_without_inline_marker = str_replace($inline_marker,\'\',$match[$i]);
//add surrounding markup to match
$match_with_tags = $prepend_tag.$match_without_inline_marker.$append_tag;
//replace match in original content with marked-up match
$content = str_replace($match[$i],$match_with_tags,$content);
}
}
return $content;
}
apply_filters("the_content","style_my_inline");
现在,我用一些伪文本测试了上面的代码,并使用反勾号定义代码块,如下所示:
Lorem ipsum dolor sit amet, `consectetur adipiscing elit`. Donec nec magna erat. `Aenean nisi ante`, semper vel imperdiet sed, laoreet.
然后应用以下css:
<style type = "text/css">
span.code{color:#56aaff;font-family:courier;background:#e5e5e5;padding:1px;}
</style>
我得到的结果如下:
希望这对你有用。