我创建了一个小部件,显示您决定的帖子,并显示一个img(不起作用),标题,摘录和链接工作正常。
我可以看到$ 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:
<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>
这是小部件代码:
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');
我可以看到数字(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">';
但我不喜欢这种方法。
答案 0 :(得分:0)
尝试使用以下代码
`$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "size" );`
if ( $thumbnail_src ) : ?>
<img src="<?php echo $thumbnail_src[0]; ?>" width="<?php echo $thumbnail_src[1]; ?>" height="<?php echo $thumbnail_src[2]; ?>" />
<?php endif; ?>
因为您试图在主页,类别页面或任何其他术语页面中获取缩略图。你必须使用 - the_post_thumbnail();
如果你想在特定页面中使用它,比如单页,你应该使用get_post_thumbnail_id($ postId);
另外请确保添加了图片,否则会返回空白
答案 1 :(得分:0)
您需要设置WordPress模板标记功能的postdata才能工作。有时它根本不足以使用:global $post;
在循环中,您需要使用:setup_postdata( $post );
并在完成后:wp_reset_postdata();
将postdata重置为原始帖子或页面。
您的窗口小部件代码中没有WordPress循环,您只是尝试使用全局post对象。
小部件代码中的示例,将其替换为if( $post ){...}
部分:
if ( $post ) {
// Setup the $post so you can use template functions like: the_permalink(); etc...
setup_postdata( $post );
if ( has_post_thumbnail() ) {
echo '<div class="latest_news-image">';
echo '<a href="' . get_permalink() . '" title="' . apply_filters( 'widget_title', get_the_title() ). '">';
echo get_the_post_thumbnail( get_the_ID(), 'full' );
echo '</a>';
echo '</div>';
}
echo '<div class="latest_news-text">';
echo '<h3 class="story_widget_title">' . apply_filters( 'widget_title', get_the_title() ). '</h3>';
echo '<p class="story_widget_excerpt">' . get_the_excerpt() . '</p>';
echo '<a href="' . get_permalink() . '" class="story_widget_readmore" title="Read the post, ' . get_the_title() . '">READ MORE</a>';
echo '</div>';
// Reset the postdata so we avoid conflicts
wp_reset_postdata();
}
了解有关setup_postdata()和wp_reset_postdata()函数的更多信息的相关链接:
如果您遇到问题,请确保: