결정한 게시물을 표시하는 위젯을 만들고 img (그 나던 일하지 않습니다)를 표시하고 제목, 발췌 부분 및 링크가 잘 작동합니다.get_the_post_thumbnail 자료 Wordpress가 작동하지 않습니다.
$ post-> ID가 정상적으로 작동하는 것을 볼 수 있습니다. "add_theme_support ('post-thumbnails');" 추가되었습니다. WP 버전이 마지막 버전입니다.
echo '<a href="' . get_permalink($post->ID) . '" title="' . apply_filters('widget_title', $post->post_title). '">';
echo get_the_post_thumbnail($post->ID, 'full');
echo '</a>';
내가 사진을 볼 수 없습니다 위젯을 실행하고 이것은 HTML 인 경우 :
if (!defined('ABSPATH'))
die('-1');
add_action('widgets_init', function(){
register_widget('Latest_News_Widget');
});
/**
* Adds Latest News Widget.
*/
class Latest_News_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'Latest_News_Widget', // Base ID
__('Latest News Widget', 'text_domain'), // Name
array('description' => __('Latest News Widget!', 'text_domain'),) // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget($args, $instance) {
// get the excerpt of the required story
if ($instance['story_id'] == 0) {
$gp_args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'orderby' => 'post_date',
'order' => 'desc',
'post_status' => 'publish'
);
$posts = get_posts($gp_args);
if ($posts) {
$post = $post[0];
} else {
$post = null;
}
} else {
$post = get_post($instance['story_id']);
}
if (array_key_exists('before_widget', $args)) echo $args['before_widget'];
if ($post) {
if (has_post_thumbnail($post->ID)) {
echo '<div class="latest_news-image">';
echo '<a href="' . get_permalink($post->ID) . '" title="' . apply_filters('widget_title', $post->post_title). '">';
echo get_the_post_thumbnail($post->ID, 'full');
echo '</a>';
echo '</div>';
}
echo '<div class="latest_news-text">';
echo '<h3 class="story_widget_title">' . apply_filters('widget_title', $post->post_title). '</h3>';
echo '<p class="story_widget_excerpt">' . $post->post_excerpt . '</p>';
echo '<a href="' . get_permalink($post->ID) . '" class="story_widget_readmore" title="Read the post, ' . $post->post_title . '">READ MORE</a>';
echo '</div>';
} else {
echo __('No recent post found.', 'text_domain');
}
if (array_key_exists('after_widget', $args)) echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form($instance) {
if (isset($instance[ 'story_id' ])) {
$story_id = $instance[ 'story_id' ];
}
else {
$story_id = 0;
}
?>
<p>
<label for="<?php echo $this->get_field_id('story_id'); ?>"><?php _e('Story:'); ?></label>
<select id="<?php echo $this->get_field_id('story_id'); ?>" name="<?php echo $this->get_field_name('story_id'); ?>">
<option value="0">Most recent</option>
<?php
// get the exceprt of the most recent story
$gp_args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'orderby' => 'post_date',
'order' => 'desc',
'post_status' => 'publish'
);
$posts = get_posts($gp_args);
foreach($posts as $post) {
$selected = ($post->ID == $story_id) ? 'selected' : '';
if (strlen($post->post_title) > 30) {
$title = substr($post->post_title, 0, 27) . '...';
} else {
$title = $post->post_title;
}
echo '<option value="' . $post->ID . '" ' . $selected . '>' . $title . '</option>';
}
?>
</select>
</p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update($new_instance, $old_instance) {
$instance = array();
$instance['story_id'] = (! empty($new_instance['story_id'])) ? strip_tags($new_instance['story_id']) : '';
return $instance;
}
} // class Latest News Widget
편집 : 1 :
$image_id = get_post_thumbnail_id($post->ID);
echo $image_id;
echo the_post_thumbnail($image_id, 'full');
<div class="latest_news-image"><a href="http://localhost/whatever/springs-is-in-the-headline/" title="Springs is in the headline"><img src="" alt="Home" class="thumbnail"></a></div>
이 위젯 코드입니다
번호 (79)를 볼 수 있지만 여전히 비었다! :(
편집 2 :
은 내가해야 "수동"
$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
echo '<img src="'.$thumbnail_src[0].'" alt="' . apply_filters('widget_title', $post->post_title). '" height="200" width="200">';
하지만이 방법처럼 해달라고 작동하는지 확인하려면
https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/ –
코드가 작동해야합니다. 게시 이미지의 설정을 해제하고 새 이미지를 설정하십시오. 어쩌면 이상한 버그 일 수도 있습니다 ... –