WordPress.org

Support

Support » Plugins and Hacks » Widget to display one specified post

Widget to display one specified post

  • mediatician

    @mediatician

    There must be a widget to do this somewhere but I can’t find it. I need a widget that will display a post in it, not a category etc but one single specified post. Ie the post with the ID of 71 will display and only that one, if thats the ID number entered.

Viewing 2 replies - 1 through 2 (of 2 total)
  • cschmitt

    @cschmitt

    OK, five minute job. No error checking, no styling, no nothing.
    Use at your own risk.
    Create a new file displaypost.php in your wp-content/plugins directory.
    Paste the code below into it.
    Activate the plugin.
    Drag the widget to your sidebar.
    Enter the desired post ID and Bob’s your uncle (I always wanted to use that phrase :))

    Cheers,
    Christian

    <?php
    /*
      Plugin Name: Display Post
      Plugin URI: http://localhost/wordpress
      Description: Display the content of a single post in a widget.
      Author: Christian Schmitt
      Version: 1
      Author URI: http://localhost/wordpress/
     */
    
    class DisplayPost extends WP_Widget
    {
        function DisplayPost()
        {
            $widget_ops = array('description' => "Display the content of a single post in a widget.");
            $this->WP_Widget('displaypost', 'Display Post', $widget_ops);
        }
    
        function widget($args, $instance)
        {
            extract($args);
            $postId = $instance['postid'];
            $thePost = get_post($postId);
            echo $before_widget;
            echo $before_title . $widget_name . $after_title;
            echo $thePost ? $thePost->post_content : "Invalid post ID!";
            echo $after_widget;
        }
    
        function update($new_instance, $old_instance)
        {
            $new_instance = (array) $new_instance;
            $instance['postid'] = strip_tags($new_instance['postid']);
            return $instance;
        }
    
        function form($instance)
        {
            $instance = wp_parse_args((array) $instance, array('postid' => -1));
            $postid = esc_attr($instance['postid']);
    ?>
            <p>
                <label for="<?php echo $this->get_field_id('postid'); ?>">Post ID</label>
                <input type="text"
                       class="widefat"
                       id="<?php echo $this->get_field_id('postid'); ?>"
                       name="<?php echo $this->get_field_name('postid'); ?>"
                       value="<?php echo $postid; ?>" />
            </p>
    <?php
      }
    
    }
    
    function displaypost_init()
    {
        register_widget('DisplayPost');
    }
    
    add_action('widgets_init', 'displaypost_init');
    
    ?>
    mediatician

    @mediatician

    Thanks for that cschmitt – looks great. I found a work around solution that did the job for me in the meantime. I installed the Executable PHP widget and then used the following code to pull in the desired post:

    <?php
    $post_id = 71;
    $queried_post = get_post($post_id);
    ?>
    <div class=”specified-post”><?php echo $queried_post->post_content; ?></div>

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Widget to display one specified post’ is closed to new replies.