代码之家  ›  专栏  ›  技术社区  ›  Sarfraz

wordpress中每篇文章中的图片

  •  0
  • Sarfraz  · 技术社区  · 16 年前

    如果你来访 this site ,您将看到每个帖子都有一个图像和摘要。实施这一点的正确方法是什么?

    这是用wordpress自定义域完成的吗?或者这是否被编码在 图像PHP 主题文件夹中是否存在文件?我该怎么做?

    3 回复  |  直到 7 年前
        1
  •  2
  •   ahmet2106    16 年前

    有一个更好的方法-你也可以使用这个函数-

    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;
    }
    

    如果将此函数插入主题的functions.php中,则可以插入

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

    在single.php和index.php中

    此函数将捕获有史以来的第一个图像,并将显示它,如果没有人可用-它将使用一个默认图像,您可以更改…

    或者另一种方式:

    <?php $image = get_post_meta($post->ID, 'postimage', true); ?>
    
    <img src="<?php echo $image; ?>" alt="<?php the_title(); ?>" />
    

    如果你把它放在index.php或single.php中,它将使用字段“postimage”(posts/pages中的customfield)中给出的图像。

    我希望这有帮助

        2
  •  2
  •   Gary    16 年前

    最有可能是由一个自定义字段获取图像源。然后post模板将被更改,以查看是否设置了图像,如果设置了图像,则包括该图像。

        3
  •  1
  •   Sam    14 年前

    添加一个额外的答案给那些通过google找到这个答案的人,因为最初的答案意味着需要大量的手工编码。

    catswhocode博客看起来不再像是描述过的,所以这个建议可能并不完全适合,但我认为值得一提的是,wordpress现在显式地支持“post thumbnails”。有关详细信息,请参见此处: http://codex.wordpress.org/Post_Thumbnails

    至于文章只是头版的摘要,实现这一点的一种方法是替换对 the_content(~~~) (例如,在content.php中)有一个 the_excerpt() . 有关摘录的更多信息,请参见 http://codex.wordpress.org/Excerpt