24

I have researched this for a few hours and I am kind of frustrated. Maybe I am just missing something as I am new to blogging.

I am not hosting my own blog, I am just using WordPress.com. I want to include snippets of c# code and have them look like they do in Visual Studio, or at least make them look nice, certainly with line numbers and color. The solutions I have seen for this all seem to assume you are hosting your own blog. I cannot figure out how to install plugins.

Is there a widget that will make code snippets look nice, or some other solution I can easily use?

Thank you

EDIT: Sarfraz has outlined one way to solve my problem (thank you!), and I have tried it but there is an issue I have, namely that it does not colorize most of my code (newer keywords like var, from, where, select, etc). Is there a fix to this or is there some other solution?

Blackbam
  • 17,496
  • 26
  • 97
  • 150
Stomp
  • 890
  • 1
  • 6
  • 19

4 Answers4

27

Just edit your aricles in html mode and enclose your code within these tags.

[sourcecode language="css"]
[/sourcecode]

Example:

[sourcecode language="javascript"]
 // javascript hello world program
 alert('Hello, World !!');
[/sourcecode]

Note: You need to specify correct language identifier for the language attribute as shown above.

More Information Here :)

abahgat
  • 13,360
  • 9
  • 35
  • 42
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • I thought I read somewhere that whitespace formatting is lost this way? – Stomp Feb 27 '10 at 21:33
  • Nope, your code looks cleaner with required white space. thanks – Sarfraz Feb 27 '10 at 21:35
  • If it is this simple, why do so many plugins exist, some of them complicated to install? Thanks for the link btw :) – Stomp Feb 27 '10 at 21:37
  • @Stomp: the difference with plugins is that they automate it, not doing it manually, also plugins provide a variety of themes for the code, also you can not add code plugin to wordpress.com. Hope that helps. thanks bye. – Sarfraz Feb 27 '10 at 21:42
  • @Stomp: I deleted my answer as I missed the fact that you're on wordpress.com, not self-hosted. You have to stay with the codemarkup as outlined by Sarfraz.... – markratledge Feb 27 '10 at 23:18
  • Where do you insert square brackets in HTML? Is there an example on how exactly to do it? Really confusing. – Ivan G. Dec 02 '15 at 10:43
7

The [sourcecode] tag usually works fine for C#, but for me it often breaks when I post XAML code.

Instead I use this page to format my code. The result looks nice (you can see it on my blog), but it requires the "Custom CSS" option ($15/year).


EDIT: actually the [sourcecode] tag works fine, and I'm now using it in all my posts

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
6
    [code language="csharp"]
       //Your code here
    [/code]
Burak KÜKRER
  • 69
  • 2
  • 6
4

looks like this has been updated, now you can use

[code language="[the lang you are posting]"]
your code here
[/code]

note: you can shorthand language as lang

[code lang="[the lang you are posting]"]
your code here
[/code]

here is the list of supported languages

workabyte
  • 3,496
  • 2
  • 27
  • 35