• Hi,

    I don’t know if the problem is about the theme, but when I try to add a source code to my post like the following piece of code, It doesn’t work and

    [code language="cpp"]
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    #include <sstream>
    using namespace std;
    struct FileRead{
    void readFromFile();
    vector<vector<string>> person;
    };
    [/code]

    It views the code like that:

    How can I solve this issue?

    Thank you.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi quents. Does it do the same thing in one of the default WP themes like Twenty Fifteen?

    Thread Starter quents

    (@quents)

    I tried it now and yes, It does the same thing. That means this isn’t about the theme. But I have no idea what it is about, I couldn’t see anyone experience a problem like that.

    Are you using a custom shortcode?

    Thread Starter quents

    (@quents)

    I don’t think so. How could it be related to this?

    What you’re trying to do won’t work without a custom shortcode. Using [code] with square brackets isn't proper HTML and so WordPress tries to parse the parts in angled brackets (<iostream>, <string>, etc.) as HTML, which fails. You probably need to get a syntax highlighting plugin to do what you're trying to do.

    Thread Starter quents

    (@quents)

    But, If I type a code like that (without brackets):

    [code language="cpp"]
    string line;
    [/code]

    The problem still occurs.

    What’s happening is that when you use square brackets like you’re doing, WordPress interprets that text as a shortcode, which is essentially a short way of inserting HTML and/or JavaScript into a post. But WordPress doesn’t know about the shortcode you’re trying to use, so it just displays the text as is. You probably need to get a syntax highlighting plugin to do what you’re trying to do.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Problem when posting source code’ is closed to new replies.