1

I'm building a website based on posts (kind of blog platform).

I would, however, like to be able to choose which post, from any category, to display in the homepage in a news box system. All the plugins/ themes I've been trying automatically display them from the most recent or only one category or tag.

The problem is, I'd like to be able to choose a particular post, from any category.

Is there a way to achieve this? If anybody can give me a suggestion, it will be wonderful!

  • 1
    Did you find your answer, and if so which one did you use and why? – Brad Dalton Aug 2 '14 at 6:22
1

Why not simply make any post sticky using the visibility settings in the Publish box located on all Edit Post screens and add styling to make it look like a news box.

enter image description here

0

You have a few options here to achieve this without using a plugin.

OPTION 1

The simplest way to go about this is with a custom query in the spot you would like to display the post. Here you can make use of WP_Query to create a custom query.

The best way to go about this would be to make use of the post ID which you can use in your custom query to retrieve and display a custom post. Have a look at the Post and Page Parameters in WP_Query.

A very simple query would look like this. Change the post ID's with your own

<?php
// The Query
$args = array(
    //'p' => 149' //for single post
    //'posts_per_page' => 1, //for single post
    'post__in' => array( 149,159 ), //for multiple posts
    'posts_per_page' => 2, //for multiple posts
    'ignore_sticky_posts' => 1
);

$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) : 
    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        ?>
            <h2><?php the_title(); ?></h2>
        <?php 
    endwhile; 
        wp_reset_postdata();
    else:  
endif; 

OPTION 2

Add your custom query as above inside a content.php type template file, call it something like content-news.php, and then call it in any template as follows using get_template_part( $slug, $name )

get_template_part( 'content', 'news' );

This has the advantage that you don't have to repeat your custom query over and over again if you need to use it in multiple places. You just have to call one line of code everytime

OPTION 3

If you need to make this dynamic, I would suggest creating a widget. I suppose that this will be like a featured content area, so I would suggest that you register a new sidebar, and then calling that inside your header.php just after the opening body tag. You can make use of conditional tags to load your sidebar conditionally on pages and templates

To create your widget, have a look at the Widget API. There are also very nice tutorials out there which you can have a look at.

My way of thinking here is that you can create a text box inside the widget into which you can add a post ID or a couple of post ID's which you can safe in the database. For your custom query, which will be inside your widget, you can assign a variable to the ID's that you going to retrieve from the db, (the ones you've manually added in the text box). This varaible you are then going to feedback to p or post__in.

This way you can quickly change the content of your newsbox by just simply changing the post ID's you've entered in your textbox in your custom widget, and you can simply remove the widget with the build in drag and drop functionality.

This is a widget I've quickly modified. You add the post ID's in the back end in the widget text block as a comma separated string. This is converted to an array and passed to post__in. This is just a rough draft widget, so feel free to refine it

class Posts_Widget extends WP_Widget {

    function __construct() {
        $widget_ops = array('classname' => 'widget_posts', 'description' => __( 'Display selected posts from post ID&#8217;s') );
        parent::__construct('posts_widget', __('Posts Widget'), $widget_ops);
    }

    function widget( $args, $instance ) {
    $cache = wp_cache_get( 'Posts_Widget', 'widget' );

    if ( !is_array( $cache ) )
        $cache = array();

    if ( ! isset( $args['widget_id'] ) )
        $args['widget_id'] = null;

    if ( isset( $cache[$args['widget_id']] ) ) {
        echo $cache[$args['widget_id']];
        return;
    }

    ob_start();
    extract( $args, EXTR_SKIP );

        ob_start();
        extract( $args );

        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Important Posts' ) : $instance['title'], $instance, $this->id_base );

        $ids = empty( $instance['postids'] ) ? '' : $instance['postids'];

        $array_ids = array_map('intval', explode(',', $ids));

        $ppp = count($array_ids);

        $pa = array(
            'post__in' => $array_ids,
            'posts_per_page' => $ppp,
            'ignore_sticky_posts' => 1
        );
        $widget_posts = new WP_Query( $pa );

        if ( $widget_posts->have_posts() ) :

        echo $before_widget;
        echo $before_title;
        echo $title; // Can set this with a widget option, or omit altogether
        echo $after_title;

        ?>
    <ul>
        <div class="custom-widget">
        <?php while ( $widget_posts->have_posts() ) : $widget_posts->the_post(); ?>

            <?php the_title(); ?>

        <?php endwhile; ?>

    </ul>
        <?php

        echo $after_widget;

        // Reset the post globals as this query will have stomped on it
        wp_reset_postdata();

        // end 
        endif;

    $cache[$args['widget_id']] = ob_get_flush();
    wp_cache_set( 'Posts_Widget', $cache, 'widget' );
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);

        $instance['postids'] = strip_tags( $new_instance['postids'] );

        return $instance;
    }

    function form( $instance ) {
        //Defaults
        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'postids' => '') );
        $title = esc_attr( $instance['title'] );
        $ids = esc_attr( $instance['postids'] );
    ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
        <p>
            <label for="<?php echo $this->get_field_id('postids'); ?>"><?php _e( 'Post ID&#8217;s:' ); ?></label> <input type="text" value="<?php echo $ids; ?>" name="<?php echo $this->get_field_name('postids'); ?>" id="<?php echo $this->get_field_id('postids'); ?>" class="widefat" />
            <br />
            <small><?php _e( 'Post IDs, separated by commas.' ); ?></small>
        </p>
<?php
    }
}

add_action( 'widgets_init', function(){
     register_widget( 'Posts_Widget' );
});

CONCLUSION

This should give you a basic idea on how to solve your problem. I hope you find this helpful

  • Thank you for this, Im trying to make it work now :) would let you know :) – tibewww Jul 29 '14 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.