Single Post Widget

The times they are a-changin’.

This post seems to be older than 6 years—a long time on the internet. It might be outdated.

Widget zum Einfügen eines einzelnen Artikels (Post Type „post“).
Optionen: Zeige Titel, zeige Inhalt, zeige Artikelbild (Post Thumbnail).
Das Ganze kann auch ein Plugin werden, aber mir kam es speziell auf eine Lösung ohne Plugin an – ähnlich wie das Ephemera Widget im Twenty Eleven Theme.

Die Funktion wird z.B. in einer Datei namens widget.php geparkt, und die kommt ins Verzeichnis des aktiven Theme. Anschliessend muss das Widget a) registriert und b) geladen werden. Die Registrierung wird über den Hook widgets_init() in der functions.php abgelegt:

[php]function meine_widgets_init() {
register_widget( ‚Glueckpress_Single_Post_Widget‘ );
}
add_action( ‚widgets_init‘, ‚meine_widgets_init‘, 12 );
[/php]

Zum Laden der Datei kommt der Aufruf require_once( TEMPLATEPATH . '/widget.php' ); (bzw. STYLESHEETPATH, wenn widget.php im Verzeichnis eines Child Theme liegt).

Funktion

Inhalt der widget.php:

[php]// Make sure we have a theme domain for localization
if( ! defined( ‚THEME_DOMAIN‘ ) )
define( ‚THEME_DOMAIN‘, get_template() ); // or get_stylesheet() if child-theme is localized

/**
* Makes a custom widget for displaying any published post.
*
* @package WordPress
*/
class Glueckpress_Single_Post_Widget extends WP_Widget {

/**
* Constructor
*
* @return void
**/
function Glueckpress_Single_Post_Widget() {
$widget_ops = array( ‚classname‘ => ‚widget_glueckpress_single_post‘, ‚description‘ => __( ‚Use this widget to display any single post from your published posts.‘, THEME_DOMAIN ) );
$this->WP_Widget( ‚widget_glueckpress_single_post‘, __( ‚Single Post‘, THEME_DOMAIN ), $widget_ops );
$this->alt_option_name = ‚widget_glueckpress_single_post‘;

add_action( ’save_post‘, array(&$this, ‚flush_widget_cache‘ ) );
add_action( ‚deleted_post‘, array(&$this, ‚flush_widget_cache‘ ) );
add_action( ’switch_theme‘, array(&$this, ‚flush_widget_cache‘ ) );
}

/**
* Outputs the HTML for this widget.
*
* @param array An array of standard parameters for widgets in this theme
* @param array An array of settings for this widget instance
* @return void Echoes it’s output
*/
function widget( $args, $instance ) {
global $post;
$cache = wp_cache_get( ‚widget_glueckpress_single_post‘, ‚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 );

// Widget settings
$post_id = isset( $instance[‚post_id‘] ) ? $instance[‚post_id‘] : 0;
$show_post_title = isset( $instance[’show_post_title‘] ) ? $instance[’show_post_title‘] : false;
$show_post_content = isset( $instance[’show_post_content‘] ) ? $instance[’show_post_content‘] : false;
$show_post_image = isset( $instance[’show_post_image‘] ) ? $instance[’show_post_image‘] : false;

$post = $content_post = get_post( $post_id );
$content = $content_post->post_content;
$content = apply_filters(‚the_content‘, $content);
$content = str_replace(‚]]>‘, ‚]]>‘, $content);

echo $before_widget;

if ( $show_post_title ) // Post title
echo $before_title . $content_post->post_title . $after_title . "\n"; // Post title

if ( $show_post_image ) : // Posts image ?>

<figure id="showcase-slider-image-<?php echo $post_id; ?>" class="showcase-slider-image">
<?php echo get_the_post_thumbnail( $post_id, array( FEATURED_IMAGE_WIDTH, FEATURED_IMAGE_HEIGHT ) ) . "\n"; ?>
</figure><?php

endif;

if ( $show_post_content )
echo $content . "\n"; // Post content

echo $after_widget;

$cache[$args[‚widget_id‘]] = ob_get_flush();
wp_cache_set( ‚widget_glueckpress_single_post‘, $cache, ‚widget‘ );

}

/**
* Deals with the settings when they are saved by the admin. Here is
* where any validation should be dealt with.
*/
function update( $new_instance, $old_instance ) {

$instance = $old_instance;

$title = strip_tags( $new_instance[‚title‘] );
$instance[‚post_id‘] = (int) $new_instance[‚post_id‘];
$instance[’show_post_title‘] = (bool) $new_instance[’show_post_title‘];
$instance[’show_post_content‘] = (bool) $new_instance[’show_post_content‘];
$instance[’show_post_image‘] = (bool) $new_instance[’show_post_image‘];

$this->flush_widget_cache();

$alloptions = wp_cache_get( ‚alloptions‘, ‚options‘ );
if ( isset( $alloptions[‚widget_glueckpress_single_post‘] ) )
delete_option( ‚widget_glueckpress_single_post‘ );

return $instance;
}

function flush_widget_cache() {
wp_cache_delete( ‚widget_glueckpress_single_post‘, ‚widget‘ );
}

/**
* Displays the form for this widget on the Widgets page of the WP Admin area.
*/
function form( $instance ) {

$title = empty( $instance[‚post_id‘] ) ? __( ‚Single Post‘, THEME_DOMAIN ) : get_the_title( $instance[‚post_id‘] );
$post_id = isset( $instance[‚post_id‘] ) ? $instance[‚post_id‘] : 0;
$show_post_title = isset( $instance[’show_post_title‘] ) ? $instance[’show_post_title‘] : true;
$show_post_content = isset( $instance[’show_post_content‘] ) ? $instance[’show_post_content‘] : true;
$show_post_image = isset( $instance[’show_post_image‘] ) ? $instance[’show_post_image‘] : true; ?>

<p>
<label for="<?php echo $this->get_field_id(‚post_id‘); ?>"><?php echo __( ‚Display Post:‘, THEME_DOMAIN ) ?></label>
<select class="widefat" id="<?php echo $this->get_field_id(‚post_id‘); ?>" name="<?php echo $this->get_field_name(‚post_id‘); ?>">
<?php
$wp_query = new WP_Query( array(
‚post_type‘ => ‚post‘,
’showposts‘ => -1,
‚post_status‘ => ‚publish‘
));
if ( $wp_query->have_posts() ) : // begin have_posts() statement
while ( $wp_query->have_posts() ) :

$wp_query->the_post();
$current_id = get_the_ID();

if( $current_id == $post_id ) {
$extra = ’selected‘;
} else {
$extra = “;
}
echo ‚<option value="‘ . $current_id . ‚" ‚ .$extra . ‚>‘ . get_the_title() . ‚</option>‘;

endwhile;
else: // continuing have_posts() statement

echo ‚<option value="empty">‘ . __( ‚No posts available.‘, THEME_DOMAIN ) . ‚</option>‘;

endif; // end have_posts() statement
?>
</select>
</p>

<input type="hidden" id="<?php echo $this->get_field_id( ‚title‘ ); ?>" name="<?php echo $this->get_field_name( ‚title‘ ); ?>" value="<?php echo $title; ?>" />
<p>
<input class="checkbox" type="checkbox" <?php checked( (bool) $show_post_title, true ); ?> id="<?php echo $this->get_field_id( ’show_post_title‘ ); ?>" name="<?php echo $this->get_field_name( ’show_post_title‘ ); ?>" />
<label for="<?php echo $this->get_field_id( ’show_post_title‘ ); ?>"><?php echo __( ‚Display title‘, THEME_DOMAIN ) ?></label>
</p>

<p>
<input class="checkbox" type="checkbox" <?php checked( (bool) $show_post_content, true ); ?> id="<?php echo $this->get_field_id( ’show_post_content‘ ); ?>" name="<?php echo $this->get_field_name( ’show_post_content‘ ); ?>" />
<label for="<?php echo $this->get_field_id( ’show_post_content‘ ); ?>"><?php echo __( ‚Display content‘, THEME_DOMAIN ) ?></label>
</p>

<p>
<input class="checkbox" type="checkbox" <?php checked( (bool) $show_post_image, true ); ?> id="<?php echo $this->get_field_id( ’show_post_image‘ ); ?>" name="<?php echo $this->get_field_name( ’show_post_image‘ ); ?>" />
<label for="<?php echo $this->get_field_id( ’show_post_image‘ ); ?>"><?php echo __( ‚Display featured image‘, THEME_DOMAIN ) ?></label>
</p><?php

wp_reset_query();
}

} // end class Glueckpress_Single_Post_Widget()[/php]

Ausgabe

Je nachdem, wie du die Optionen setzt, werden Titel, Inhalt und Bild des Artikels im Frontend dargestellt.

Single Post Widget
Single Post Widget