Hi,
I would like to display in the home, instead of updates budypress news of a certain category. Without losing the current formatting/layout. How can I do this?
<?php
/*
*
*/
add_action( 'widgets_init', function(){
register_widget( 'NBRC_LatestPosts' );
});
class NBRC_LatestPosts extends WP_Widget {
/**
* Sets up the widgets name etc
*/
function __construct() {
parent::__construct(
'nbrclatestposts', // Base ID
__( 'NBRC Latest Posts', 'text_domain' ), // Name
array( 'description' => __( 'Widget to output latest posts in a category.', 'text_domain' ), ) // Args
);
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
// outputs the content of the widget
extract($args, EXTR_SKIP);
$title = apply_filters('widget_title', empty($instance['title']) ? __( 'Latest Blog Posts', GKTPLNAME ) : $instance['title'], $instance, $this->id_base);
echo $before_widget;
echo $before_title;
echo $title;
echo $after_title;
$blog_category = empty($instance['blog_category']) ? 'latest' : $instance['blog_category'];
$total_amount = empty($instance['total_amount']) ? 4 : $instance['total_amount'];
$photo_width = empty($instance['photo_width']) ? 310 : $instance['photo_width'];
$category_query_args = array(
'category' => $blog_category,
'posts_per_page' => $total_amount
);
$category_query = get_posts( $category_query_args );
if(!is_numeric($total_amount)) {
$total_amount = 0;
} else {
$total_amount = intval($total_amount);
}
$width = 3000;
$new_width = $total_amount * $photo_width;
if($new_width > 3000) {
$width = $new_width;
}
$amount_page = 4;
?>
<div data-cols="<?php echo $amount_page; ?>" style="width: <?php echo $width; ?>px">
<?php if ($category_query == null) : ?>
<h4> <?php echo ('There has been no recent activity.'); ?></h4>
<?php else : ?>
<?php foreach($category_query as $post):
if(has_post_thumbnail( $post->ID )) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
}
?>
<figure style="width: <?php echo $photo_width; ?>px">
<img src="<?php echo $image[0]; ?>" alt="<?php _e('View full image'); ?>" />
<figcaption>
<small><?php echo $post->post_title; ?></small>
<p><?php echo $this->activity_text($post->post_content, 15, false); ?></p>
<a href =""><?php _e('Read more...'); ?></a>
</figcaption>
</figure>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php
echo $after_widget;
}
function activity_text($input, $amount, $text_end) {
$output = '';
$input = strip_tags($input);
$input = explode(' ', $input);
for($i = 0; $i < $amount; $i++) {
if(isset($input[$i])) {
$output .= $input[$i] . ' ';
}
}
if(count($input) > $amount) {
$output .= '…';
}
return $output;
}
/**
*
* Outputs the HTML code of the widget in the back-end
*
* @param array instance of the widget settings
* @return void - HTML output
*
**/
function form($instance) {
$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
$blog_category = isset($instance['blog_category']) ? esc_attr($instance['blog_category']) : 'latest';
$total_amount = isset($instance['total_amount']) ? esc_attr($instance['total_amount']) : 4;
$photo_width = isset($instance['photo_width']) ? esc_attr($instance['photo_width']) : 310;
?>
<div class="gk-bp">
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', GKTPLNAME ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<!-- Category Select Menu -->
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'blog_category' ) ); ?>"><?php _e( 'Data source:', GKTPLNAME ); ?></label>
<select id="<?php echo $this->get_field_id('blog_category'); ?>" name="<?php echo $this->get_field_name('blog_category'); ?>">
<?php foreach(get_terms('category','parent=0&hide_empty=0') as $term) { ?>
<option <?php selected( $instance['blog_category'], $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
<?php } ?>
</select>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('total_amount')); ?>"><?php _e('Total amount of statuses', GKTPLNAME); ?></label>
<input class="short" id="<?php echo esc_attr( $this->get_field_id( 'total_amount' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'total_amount' ) ); ?>" type="text" value="<?php echo esc_attr( $total_amount ); ?>" />
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'photo_width' ) ); ?>"><?php _e( 'Photo width (px):', GKTPLNAME ); ?></label>
<input class="short" id="<?php echo esc_attr( $this->get_field_id( 'photo_width' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'photo_width' ) ); ?>" type="text" value="<?php echo esc_attr( $photo_width ); ?>" />
</p>
</div>
<?php
}
/**
*
* Used in the back-end to update the module options
*
* @param array new instance of the widget settings
* @param array old instance of the widget settings
* @return updated instance of the widget settings
*
**/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['blog_category'] = strip_tags($new_instance['blog_category']);
$instance['total_amount'] = strip_tags($new_instance['total_amount']);
$instance['photo_width'] = strip_tags( $new_instance['photo_width'] );
$alloptions = wp_cache_get('alloptions', 'options');
if(isset($alloptions['nbrclatestposts'])) {
delete_option( 'nbrclatestposts' );
}
return $instance;
}
}
?>
$found = locate_template( 'path/to/latestblogposts.php', TRUE, TRUE );