get_children()自动获取 WordPress 图片库为何不能显示缩略图

网站应用 - 八月 6, 2009 - 来源:单点日志 - 1 Comment -

关于ComFy Magazine Plus wordpress theme 不能自动加载说略图,或者加载了部分图片。

自己无聊,玩了下这个模板,遇到了这个问题,多次尝试和查阅相关资料,我肯定的是ComFy Magazine Plus wordpress theme作者程序没有写错,而是对于wordpress 函数get_children(),可能在写文章的时候,先不要上传照片,先保存一次,在上传插入我们需要的图片,这样测试下来,ComFy Magazine Plus wordpress theme图片就能正常产生缩略图。当然,针对ComFy Magazine,如果你觉得每次都要保存一次比较麻烦,你可以用自定义字段thumbnail添加图片地址,这样说略图是完全可以显示 的。

其实,不止对于ComFy Magazine我们要这么做,只要用到get_children()来获取图片,都需要先保存下文章,再插入图片,目前是这样,也许,以后就不用了。对于网络上的wordpress 2.8部支持ComFy Magazine theme的说法,我给予否定。

以下连接为get_children()函数相关介绍及应用:

wordpress Function Reference/get children

Autoload Attachment From WordPress Post

WordPress中实现自动显示文章缩略图

请正确参阅语言版本

延伸阅读为,相关代码备份

get_children () automatically WordPress Photo Gallery Why can not show thumbnails

translate by google

On ComFy Magazine Plus wordpress theme that can not automatically map to load, or load some pictures.

Their boring, played under the template, have encountered this problem, several attempts and access to relevant information, I am sure that ComFy Magazine Plus wordpress theme author does not wrong, but the wordpress function get_children (), may in writing time, should not upload photos, first save , then upload the picture to insert, so test down, ComFy Magazine Plus wordpress theme thumbnail picture will be able to produce normal le. Of course, for ComFy Magazine, if you think that every time is too much trouble to save time, you can add custom fields thumbnail image address, this map can be displayed.

In fact, more than we would like to ComFy Magazine to do so, as long as the use get_children () to obtain the pictures, are under the need to preserve the article, and then insert the picture, at present this is the case, perhaps, there is no need. The network supports wordpress 2.8 is ComFy Magazine theme, I give the negative.

The following connection for get_children () function and application of related presentations:

wordpress Function Reference/get children

Autoload Attachment From WordPress Post

WordPress中实现自动显示文章缩略图

Please refer to the correct language version.

For an extension of reading, the relevant code backup.

There is no script to automatically load a thumbnail from a WordPress post by default. However, it is very useful for those who are using WordPress Image Gallery. But you can achieve that by using plugin. Here, I will show you how to do it manually and add into your theme.

The Function

<?php
$size = 'thumbnail';
$images = get_children(array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID,))
foreach($images as $image) {
$attachment=wp_get_attachment_image_src($image->ID, $size);
?><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />

get_children() function here will return all the image attachments attached to post (identified by $postID), and assign them to our array $images.
$size is the size of the attachment, you can use “medium” or “full” too.

Apply To Post

Below is the full code in order to display the attachment in front page.

<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php if ($images = get_children(array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID,)))
foreach($images as $image) {
$attachment=wp_get_attachment_image_src($image->ID, $size);
?>
<img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> class="alignleft" />
<?php the_content(''); ?>
<?php } else { ?>
<?php the_content(''); ?>
</div>
</div>
<?php endwhile; ?>

With this, it will check there is any attachment for the post and only the content will be displayed if there is no attachment.

Using The Functions.php

You can also call the function from the functions.php. Firstly, put this code into your functions.php.

function my_attachment_image($postid=0, $size='thumbnail', $attributes='') {
if ($postid<1) $postid = get_the_ID();
if ($images = get_children(array(
'post_parent' => $postid,
'post_type' => 'attachment',
'numberposts' => 1,
'post_mime_type' => 'image',)))
foreach($images as $image) {
$attachment=wp_get_attachment_image_src($image->ID, $size);
?><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> /><?php
}
}

Then call the function in index.php.

<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink(); ?>"><?php my_attachment_image(0, 'thumbnail', 'alt="' . $post->post_title . '"'); ?></a>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>
</div>
<?php endwhile; ?>

Advance – Custom Field and Attachment

As we know, not all of us are using WordPress Image Gallery. Some of us may prefer to handle the attachments in a different way. Then I come up with this solution. I am using this function in my one of my premium wordpress theme – “Our Community“.

<?php
while (have_posts()) : the_post();
$img = get_post_meta($post->ID, "img", TRUE); // Declare the custom field for the image
?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="post-sum box">
<h2><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php $size = 'thumbnail';
if($img != null or $img != '') { // If there is a value for the custom field ?>
<img src="<?php echo $img; ?>" alt="<?php the_title_attribute(); ?>" width="150" height="150" /><?php the_content(''); ?>
<?php
} elseif ($images = get_children(array( // Check if there is any attachments
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID,)))
foreach($images as $image) {
$attachment=wp_get_attachment_image_src($image->ID, $size);
?><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
<?php the_content(''); ?>
<?php } else { // If there is no custom field value and attachments ?>
<?php the_content(''); ?>
<?php } ?>
</div>
</div>
<?php endwhile; ?>

Considering the user might using other than WordPress Image Gallery, I have to provide some options for them. Here, the priority will be in using custom field. If there is no value for it, the function will then check for any attachment. Finally, only text will be displayed if neither of them exist.



无觅相关文章插件,快速提升流量

《get_children()自动获取 WordPress 图片库为何不能显示缩略图》由单点日志原创提供!
转载请注明:http://spoint.babyshoot.cn/archives/2009/08/get-children-automatically-wordpress-photo-gallery-why-can-not-show-thumbnails.html

1 Comment│赶紧发话! »

发表评论

您必须登录后才能发表评论。
8