Hinzufügen eines hervorgehobenen Bildes zum Anzeigen des Widget

Ich habe unten den Plugin-Code, der benutzerdefinierte Post-Typen als ein neues Post-Widget anzeigt. Ich möchte, dass dieser Code auch den Auszug der Beiträge und das vorgestellte Bild der Beiträge anzeigt. Was und wo sollte ich hinzufügen, um das vorgestellte Bild und den Auszugstext hinzuzufügen?

Ich würde mich freuen, wenn Sie mir helfen können.

 'widget_recent_entries', 'description' => __( 'Your site's most recent custom Posts.', 'custom-post-type-widgets' ) ); parent::__construct( 'custom-post-type-recent-posts', __( 'Recent Posts (Custom Post Type)', 'custom-post-type-widgets' ), $widget_ops ); $this->alt_option_name = 'widget_custom_post_type_recent_posts'; } public function widget( $args, $instance ) { if ( ! isset( $args['widget_id'] ) ) { $args['widget_id'] = $this->id; } $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Recent Posts', 'custom-post-type-widgets' ) : $instance['title'], $instance, $this->id_base ); $posttype = ! empty( $instance['posttype'] ) ? $instance['posttype'] : 'post'; if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) ) { $number = 5; } $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false; $post_types = get_post_types( array( 'public' => true ), 'objects' ); if ( array_key_exists( $posttype, (array) $post_types ) ) { $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'post_type' => $posttype, 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, ) ) ); if ( $r->have_posts() ) : ?>   
    have_posts() ) : $r->the_post(); ?>
  • <a href="https://wordpress.stackexchange.com/questions/249131/adding-featured-image-to-post-display-widget/">

<label for="get_field_id( 'title' ); ?>"> <input class="widefat" id="get_field_id( 'title' ); ?>" name="get_field_name( 'title' ); ?>" type="text" value="" />

true ), 'objects' ); printf( '

' . '', $this->get_field_id( 'posttype' ), __( 'Post Type:', 'custom-post-type-widgets' ), $this->get_field_name( 'posttype' ) ); foreach ( $post_types as $post_type => $value ) { if ( 'attachment' === $post_type ) { continue; } printf( '%s', esc_attr( $post_type ), selected( $post_type, $posttype, false ), __( $value->label, 'custom-post-type-widgets' ) ); } echo '

'; ?>

<label for="get_field_id( 'number' ); ?>"> <input id="get_field_id( 'number' ); ?>" name="get_field_name( 'number' ); ?>" type="text" value="" size="3" />

<input class="checkbox" type="checkbox" id="get_field_id( 'show_date' ); ?>" name="get_field_name( 'show_date' ); ?>" /> <label for="get_field_id( 'show_date' ); ?>">

<?php } }

Solutions Collecting From Web of "Hinzufügen eines hervorgehobenen Bildes zum Anzeigen des Widget"

Ihr aktualisierter Code

 /** * Custom Post Type Recent Posts widget class * * @since 1.0.0 * @package Custom Post Type Widgets */ class WP_Custom_Post_Type_Widgets_Recent_Posts extends WP_Widget { public function __construct() { $widget_ops = array( 'classname' => 'widget_recent_entries', 'description' => __( 'Your site's most recent custom Posts.', 'custom-post-type-widgets' ) ); parent::__construct( 'custom-post-type-recent-posts', __( 'Recent Posts (Custom Post Type)', 'custom-post-type-widgets' ), $widget_ops ); $this->alt_option_name = 'widget_custom_post_type_recent_posts'; } public function widget( $args, $instance ) { if ( ! isset( $args['widget_id'] ) ) { $args['widget_id'] = $this->id; } $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Recent Posts', 'custom-post-type-widgets' ) : $instance['title'], $instance, $this->id_base ); $posttype = ! empty( $instance['posttype'] ) ? $instance['posttype'] : 'post'; if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) ) { $number = 5; } $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false; $post_types = get_post_types( array( 'public' => true ), 'objects' ); if ( array_key_exists( $posttype, (array) $post_types ) ) { $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'post_type' => $posttype, 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, ) ) ); if ( $r->have_posts() ) : ?> < ?php echo $args['before_widget']; ?> < ?php if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; } ?> 
    < ?php while ( $r->have_posts() ) : $r->the_post(); ?>
  • < ?php has_post_thumbnail() ? the_post_thumbnail() : ''?> < ?php get_the_title() ? the_title() : the_ID(); ?> < ?php if ( $show_date ) : ?> < ?php endif; ?>
  • < ?php endwhile; ?>
< ?php echo $args['after_widget']; ?> < ?php wp_reset_postdata(); endif; } } public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags( $new_instance['title'] ); $instance['posttype'] = strip_tags( $new_instance['posttype'] ); $instance['number'] = (int) $new_instance['number']; $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false; return $instance; } public function form( $instance ) { $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; $posttype = isset( $instance['posttype'] ) ? $instance['posttype']: 'post'; $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false; ?>

< ?php $post_types = get_post_types( array( 'public' => true ), 'objects' ); printf( '

' . '

'; ?>

id="< ?php echo $this->get_field_id( 'show_date' ); ?>" name="< ?php echo $this->get_field_name( 'show_date' ); ?>" />

< ?php } }

Die wichtigsten Tricks sind hier in diesem Teil-

 
  • < ?php has_post_thumbnail() ? the_post_thumbnail() : ''?> < ?php get_the_title() ? the_title() : the_ID(); ?> < ?php if ( $show_date ) : ?> < ?php endif; ?>