希望我只是遇到了一些简单的事情
下面是一个场景,我创建了一个名为tr_get_post_image_tplt
这将为正在调用的帖子显示帖子缩略图(或自定义大小)。
我在头版循环中这样称呼它:
<a href="<?php the_permalink(); ?>"<?php post_class(\'thumbnail span1\'); ?> ><?php tr_get_post_image_tplt(\'thumbnail\', \'thumbnail\');?><a/>
我希望函数输出如下内容:
<a href="/864/" class="post-864 post type-post status-publish format-quote hentry category-general thumbnail span1"><img src="/wp-content/themes/_tr_roots/img/post-thumb-default.gif" class="post-864 post type-post status-publish format-quote hentry category-general thumbnail" alt="something attachment"/><a/>
但我得到的是:
<a href="/864/"class="post-864 post type-post status-publish format-quote hentry category-general thumbnail span1">class="post-864 post type-post status-publish format-quote hentry category-general thumbnail"<p><img src="/wp-content/themes/_tr_roots/img/post-thumb-default.gif" alt="something attachment"/></p>
<a/>
请注意,该类已从
<a>
已添加标记和额外标记。
我已经滔滔不绝地讲了几个小时了,所以我祈祷这不是什么愚蠢的拼写错误。
但假设这不是一个拼写错误,有人能解释一下发生了什么,以及今后如何避免吗?
下面是存储在功能插件中的函数(尽管我在functions.php中尝试过,但结果相同):
function tr_get_post_image_tplt($size=\'thumbnail\', $class=\'thumbnail\', $default=\'post-thumb-default.gif\') {
global $post;
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(), $size );
$alt_text = get_the_title(get_post_thumbnail_id());
$tr_post_thumb = \'<img src="\' . $image_src[0] . \'" \' . post_class($class) . \' alt="\' . $alt_text . \'post" />\';
//$tr_post_thumb = the_post_thumbnail($size); isn\'t flexable enough
}
else{
$photos = get_children( array(
\'post_parent\' => $post->ID,
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'order\' => \'ASC\',
\'orderby\' => \'menu_order ID\'
) );
if (!empty($photos)) {
$photo = array_shift($photos);
$image_src = wp_get_attachment_image_src($photo->ID, $size);
$alt_text = get_the_title($photo->ID);
$tr_post_thumb = \'<img src="\' . $image_src[0] . \'" \' . post_class($class) . \'" alt="\' . $alt_text . \'" />\';
}
else {
// if above fails, spit out a default image
$alt_text = get_the_title($post->ID);
$tr_post_thumb = \'<img src="\' . get_stylesheet_directory_uri() . \'/img/\' . $default . \'" \' . post_class($class) . \' alt="\' . $alt_text . \' attachment"/>\';
}
}
echo apply_filters(\'the_content\', $tr_post_thumb );
}
非常感谢分享您的经验!