Skip to content

Instantly share code, notes, and snippets.

@johnymachine
Created August 1, 2017 19:48
Show Gist options
  • Select an option

  • Save johnymachine/bdb925f962104a47705def1ebbc9c7d1 to your computer and use it in GitHub Desktop.

Select an option

Save johnymachine/bdb925f962104a47705def1ebbc9c7d1 to your computer and use it in GitHub Desktop.
WP - Latest post from category
<?php
/**
* Custom Latest Post By Category Widget
*
* @package Barletta
*/
class barletta_latest_post extends WP_Widget {
public function __construct(){
$widget_ops = array('classname' => 'barletta-latest-post','description' => esc_html__( "Barletta Latest Post Widget", 'barletta') );
parent::__construct('barletta_latest_post', esc_html__('Barletta Latest Post Widget','barletta'), $widget_ops);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
$category = ! empty( $instance['category'] ) ? $instance['category'] : false;
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
$query_args = array(
'cat' => $category,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'has_password' => false,
'posts_per_page' => 1
);
$posts = new WP_Query( $query_args );
if($posts->have_posts()){
while($posts->have_posts()){
$posts->the_post(); ?>
<article class="list-post" style="border-bottom: none;margin-bottom: 0px;padding-bottom: 0px;">
<div class="list-post-body" style="padding: 0px;">
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div class="post-meta">
<span>Přidáno dne: <?php echo get_the_date(); ?></span>
</div>
<?php the_content()?>
<div class="read-more"><a href="<?php the_permalink(); ?>"><?php esc_html_e( 'Continue Reading', 'barletta' ); ?></a></div>
</div>
</article>
<?php }
wp_reset_query();
}
echo $args['after_widget'];
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['category'] = sanitize_text_field( $new_instance['category'] );
return $instance;
}
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'category' => '' ) );
$title = sanitize_text_field( $instance['title'] );
$category = sanitize_text_field( $instance['category'] );
?>
<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 esc_attr($title); ?>" /></p>
<p><label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Category:'); ?></label>
<select class='widefat' id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" >
<?php
$cats = get_categories( array( 'hide_empty' => 0 ) );
foreach ( $cats as $cat ) {
echo '<option value="' . $cat->term_id . '"'
. selected( $category, $cat->term_id, false )
. '>'. esc_html( $cat->name ) . '</option>';
}
?>
</select>
</p>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment