• matius

    (@matius)


    For example, I need a widget that will display the contents of a post, without actually creating a post.

    Sort of more like a random quote plugin except with the ability to include HTML or PHP blocks.

    Is there anything like that?

Viewing 4 replies - 1 through 4 (of 4 total)
  • MichaelH

    (@michaelh)

    Consider downloading and installing Otto’s PHP Code Widget, then something like this code in one of those widgets:

    <?php
    $args=array(
      'orderby' => 'rand'
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
        the_content();
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    MichaelH

    (@michaelh)

    Oh forget what I said–you don’t want to use posts.

    Maybe consider using a Custom Post Type and save your snippets in those, then just change the above so that

    'post_type' => 'your custom post type',

    Thread Starter matius

    (@matius)

    Thanks MichaelH – appreciate!

    Thread Starter matius

    (@matius)

    Thanks MichaelH, what do you suppose the following error from the code above is . . . I’m trying to add it to a PHP Code widget in a sidebar.

    I used the Custom Post Type UI plugin to create a ‘movies’ post type.

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in . . . php, eval()'d code on line 4

    Guessing because it’s not in the loop or whatever. Is it possible to do this in the sidebar?

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Any such widget to randomly display code snippets’ is closed to new replies.