WordPress.org

Ready to get started?Download WordPress

Forums

Any such widget to randomly display code snippets (5 posts)

  1. matius
    Member
    Posted 3 years ago #

    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?

  2. MichaelH
    Member
    Posted 3 years ago #

    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().
    ?>
  3. MichaelH
    Member
    Posted 3 years ago #

    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',
  4. matius
    Member
    Posted 3 years ago #

    Thanks MichaelH - appreciate!

  5. matius
    Member
    Posted 3 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.