Jump to content

Wordpress widget - How to display one post category rather than all


brandnucreative

Recommended Posts

Hi All,

I'm currently trying to find a solution to a problem within a wordpress template I am using. On the homepage, Im using a widget to display recent posts. However, the widget displays all posts, whereas I want it to only display posts from a particular category. What do I need to edit within the php to make this happen?

 

Heres the widget php:

 

 

 

<?php
/**
 * List Blog Posts
 *
 * @since Jobify 1.0
 */
class Jobify_Widget_Blog_Posts extends Jobify_Widget {


/**
* Constructor
*/
public function __construct() {
$this->widget_cssclass    = 'jobify_widget_blog_posts';
$this->widget_description = __( 'Jobify - Display recent blog posts.', 'jobify' );
$this->widget_id          = 'jobify_widget_blog_posts';
$this->widget_name        = __( 'Jobify - Home: Blog Posts', 'jobify' );
$this->settings           = array(
'title' => array(
'type'  => 'text',
'std'   => __( 'Recent News Article', 'jobify' ),
'label' => __( 'Title:', 'jobify' )
),
'description' => array(
'type'  => 'textarea',
'rows'  => 4,
'std'   => '',
'label' => __( 'Description:', 'jobify' ),
)
);
parent::__construct();
}


/**
* widget function.
*
* @see WP_Widget
* @access public
* @param array $args
* @param array $instance
* @return void
*/
function widget( $args, $instance ) {
if ( $this->get_cached_widget( $args ) )
return;


ob_start();


extract( $args );


$title       = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
$description = $instance[ 'description' ];
$posts       = new WP_Query( apply_filters( 'widget_jobify_blog_posts', array( 'posts_per_page' => 3, 'ignore_sticky_posts' => true ) ) );


echo $before_widget;
?>


<div class="container">


<?php if ( $title ) echo $before_title . $title . $after_title; ?>


<?php if ( $description ) : ?>
<p class="homepage-widget-description"><?php echo $description; ?></p>
<?php endif; ?>


<div class="content-grid row">
<?php if ( $posts->have_posts() ) : ?>
<?php while ( $posts->have_posts() ) : $posts->the_post(); ?>
<?php get_template_part( 'content', 'grid' ); ?>
<?php endwhile; ?>
<?php endif; ?>
                        </div>


</div>
                <?php
echo $after_widget;


$content = apply_filters( 'jobify_widget_blog_posts', ob_get_clean(), $instance, $args );


echo $content;


$this->cache_widget( $args, $content );
}
}
Edited by brandnucreative
Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.