31

This should be easy points as I forgot how and can't find it on Google.

How do I accomplish something like this:

Blah Blah Blah some code Blah Blah

in wordpress? pre doesn't work as it will give a line break.

25

<tt>text</tt> gives text

Wait... Wordpress? What HTML tags does it support?

  • You can write code inline by using backticks (thing on the ~ key) – Blender May 9 '11 at 23:30
  • I'm not sure how the OP is writing this (either via HTML or the WYSIWYG interface), but Wordpress supports HTML. – Blender May 9 '11 at 23:41
  • 19
    <tt> is not supported in HTML5, use CSS or <code> instead. – Sophivorus Mar 24 '13 at 18:18
  • The problem with this solution is when the preformatted text contains control character such as < it will be treated as tags unless you escape it manually – gerrytan Jun 26 '13 at 1:36
33

It may be better to use <code> than <pre> to display inline code, for two reasons:

  • it's treated as inline
  • the semantics of <code> are a much better match than <pre>

But definitely use CSS classes (as @Blender points out).

There's a caveat: <code> doesn't escape <> brackets, so if you want to display HTML then you have to manually escape brackets as &lt; and &gt;. (which you should really do anyway; tags in HTML really should be clearly distinguished from data)

  • Another caveat: <code> collapses whitespace, which has bitten me before when I'm trying to discuss significant whitespace in a string with people. – Andy Mar 9 '17 at 1:45
10

You can have an inline <pre> by adding some custom CSS to your theme's stylesheet:

pre.inline {
   display: inline;
}

Now, when you write:

I am some <pre class="inline">code, see?</pre> Foo.

It shows up like this:

I am some code, see? Foo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.