开发者

Image in every post in WordPress

开发者 https://www.devze.com 2022-12-25 10:17 出处:网络
If you visit this site, you will see that there is an image and summary for each post. What is the proper way to implement that?

If you visit this site, you will see that there is an image and summary for each post. What is the proper way to implement that?

Is this done using WordPress custom fields? Or whether this is coded in image.php file present in them开发者_StackOverflow中文版e folder? How do I do that?


There is a better way - you can also use this function too -

function catch_that_image() {
    global $post, $posts;
        $first_img = '';
        ob_start();
        ob_end_clean();
        $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
        $first_img = preg_replace("/_thumb[0-9]\./", "$1.", $first_img);

    // no image found display default image instead
        if(empty($first_img)){
            $first_img = "/wp-content/default.png";
        }
        return $first_img;
}

if you insert this function to your functions.php of your theme you can insert

<img src="<?php echo catch_that_image(); >" width="50" height="50" alt="<?php the_title(); ?>" />

in your single.php and index.php

This function will catch the first image in ever post and will display it, if no one is available - it will use one Default image which you can change...

Or another way:

<?php $image = get_post_meta($post->ID, 'postimage', true); ?>

<img src="<?php echo $image; ?>" alt="<?php the_title(); ?>" />

If you put this in your index.php or single.php it will use the image given in field "postimage" (customfield in posts/pages).


Most likely by a custom field which takes an image source. Then the post template would be changed to see if an image is set and, if it is, include it.


Adding an additional answer for people who find this via Google, as the original answers imply that a lot of hand-coding is needed.

The catswhocode blog no longer looks like described, so this advice may not fit exactly, but I thought it worth mentioning that WordPress now supports "Post Thumbnails" explicitly. For more information, see here: http://codex.wordpress.org/Post_Thumbnails

As for the article only being a summary on the front page, one way to achieve this is by replacing a call to the_content(~~~) (e.g. in content.php) with one to the_excerpt(). For more on excerpts, see http://codex.wordpress.org/Excerpt

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号