如何调用WordPress First Image Post或附加图像或默认

时间:2012-02-08 作者:dan

谁能帮我开发一个wordpress脚本,如果它不存在,它将获得第一个附加(上传)的图像。

Theoretically:[如果存在]

<img src="<?php echo catch_that_image() ?>" />
[或其他]

<img src="\'. wp_get_attachment_url($attachment->ID) . \'" alt="Main Image" id="mainImage"" />
[或其他]

<img src="<?php bloginfo(\'template_url\') ?>/images/default.png" />

My current code looks like this (UPDATED.. AGAIN)

<?php  
 $args = array(
 \'post_parent\' => $post->ID,
 \'post_type\' => \'attachment\',
 \'post_mime_type\' => \'image\',
 \'orderby\' => \'menu_order\',
 \'order\' => \'ASC\',
 \'offset\' => \'0\',
 \'numberposts\' => 1 
 );
 $attachments = get_posts($args);
 $images =& get_children( \'post_type=attachment&post_mime_type=image&post_parent=\' . $post->ID );
 if ($attachments) {
 foreach ($attachments as $attachment) {
 if(wp_attachment_is_image( $attachment->ID )) {
 echo \'<img src="\'. wp_get_attachment_url($attachment->ID) . \'" alt="Main Image" id="mainImage"" />\'; }}} 
 <!-- else if ( count( $images ) > 0 ) echo get_the_post_thumbnail; } -->
 else {
 echo \'<img src="http://hellotokyostore.com/wp-content/themes/hellotokyo/images/default.png" alt="Hello Tokyo" />\'; }
?>

2 个回复
SO网友:Mamaduka

查看JustinTadlock的[获取图像][1]插件,它提供了许多获取图像的选项和后备方法。

SO网友:turbonerd

Updated answer

更新了dan在这篇帖子上评论的代码。

<?php 

    $args = array(
        \'post_parent\' => $post->ID,
        \'post_type\' => \'attachment\',
        \'post_mime_type\' => \'image\',
        \'orderby\' => \'menu_order\',
        \'order\' => \'ASC\',
        \'offset\' => \'0\',
        \'numberposts\' => 1 
    );

    $images = get_posts($args);

    if ( count( $images ) > 0 ) {

        echo \'<img src="\'. wp_get_attachment_url($images[0]->ID) . \'" alt="Main Image" id="mainImage"" />\';

    } else {

       echo \'<img src="http://hellotokyostore.com/wp-content/themes/hellotokyo/images/default.png" alt="Hello Tokyo"/>\';

    }

?>
<小时>

This information is now out of date but may be useful to another.

我想你想做的是:

每篇文章都有一个缩略图如果没有附加缩略图,请使用默认图像this address. 看看它,这样你就可以了解“特色图片”是如何工作的。

要点是在你的帖子页面上有一个名为“特色图片”的新框,你可以在其中为每篇帖子设置一个“缩略图”。

将以下内容添加到主题functions.php.

<?php

if (function_exists(\'add_theme_support\')) {
    add_theme_support(\'post-thumbnails\');
    /* change these dimensions to suit your website.
       noticed you used 250 * 250 on the page you linked in
       OP comments, so I\'ve used that. you may want to increase
       the size of your individual page\'s thumbnails */
    add_image_size(\'index-categories\', 250, 250, true);
    add_image_size(\'page-single\', 250, 250, true);
}

function InsertFeaturedImage($content) {

    global $post;

    $original_content = $content;

    $default_thumbnail = get_bloginfo(\'template_directory\') . \'/images/default_thumbnail.png\';
    $default_src = \'<img src="\' . $default_thumbnail . \'" alt="Your caption here" />\';

    if ( current_theme_supports( \'post-thumbnails\' ) ) {

        if ( ( is_page() ) || ( is_single() ) ) {
            /* if you\'re viewing a single post
               250 * 250 as designated above */

            if ( has_post_thumbnail() ) {
                $content = the_post_thumbnail(\'page-single\');
            } else {
                $content = $default_src;
            }

            $content .= $original_content;
        } else {
            /* if you\'re viewing a list of posts, use this size of thumbnail
               250 * 250 as designated above */

            if ( has_post_thumbnail() ) {   
                $content = the_post_thumbnail(\'index-categories\');
            } else {
                $content = $default_src;
            }

            $content .= $original_content;
        }

    }

    return $content;
}

add_filter( \'the_content\', \'InsertFeaturedImage\' );

?>
请毫不犹豫地询问这其中是否有任何部分不合理。

结束