WordPress.org

Ready to get started?Download WordPress

Forums

Crayon Syntax Highlighter
running crayon outside of the loop (1 post)

  1. r_dahl
    Member
    Posted 3 months ago #

    hi,

    thanks for this great plugin!

    I'm trying to realise the following. you find my 2 issues at the bottom of this post.

    each post has 2 attachments which are .txt-files.
    crayon pulls the contents of these two files and displays it within the post when viewing it.

    (I use the plugin "simple fields for the 2 file upload fields)

    because this works outside of the loop (as far as I know?) because the code is not pasted in the post wysiwyg-editor, I need to use crayon in a different way explained here:

    https://github.com/aramk/crayon-syntax-highlighter/issues/131

    my funtions.php
    note: "original code" is the 1st attached text-file, "example code" the 2nd one.

    function code_display_original() {
    
    	$original_code = simple_fields_get_post_value(get_the_id(), array(1, 1), true);
    	$original_code_url = get_attached_file($original_code[id]);
    	$source_original = file_get_contents($original_code_url);
    
    $crayon_original = '<pre class="lang:html ">' . $source_original . '</pre>';
    
    return CrayonWP::highlight($crayon_original);
    
    }
    
    function code_display_example() {
    
    	$example_code = simple_fields_get_post_value(get_the_id(), array(1, 2), true);
    	$example_code_url = get_attached_file($example_code[id]);
    	$source_example = file_get_contents($example_code_url);
    
    $crayon_example = '<pre class="lang:html ">' . $source_example . '</pre>';
    
    return CrayonWP::highlight($crayon_example);
    }

    and this goes in my theme to display the highlighted code

    <h3>Original Code</h3>
    <?php echo code_display_original(); ?>
    
    <h3>Example Code</h3>
    <?php echo code_display_example(); ?>

    2 issues:

    1.
    the first code is displayed in a perfect way.
    the 2nd code, too, unless I press the "toggle plain code"-button or the "copy"-button which both produces a white screen in the code field.

    2.
    the css-file-links are displayed right before the crayon-code-part. which is in the <body>-tag and therefore strictly speaking invalid.

    I'm a beginner when it comes to wordpress and PHP in general as you might have noticed.

    I would really appreciate any sort of help or hints :)

    thanks!

    https://wordpress.org/plugins/crayon-syntax-highlighter/

Reply

You must log in to post.

About this Plugin

About this Topic