使用wordpress将图像移动到另一个div

使用wordpress将图像移动到另一个div,wordpress,image,Wordpress,Image,我是Wordpress的新手,这是我的第一个Wordpress网站,所以请容忍我 这是我的index.php: <?php get_header(); ?> <div id="content"> <section id="posts"> <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

我是Wordpress的新手,这是我的第一个Wordpress网站,所以请容忍我

这是我的index.php:

<?php get_header(); ?>
   <div id="content">
        <section id="posts">
                <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                <section class="post">
                    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?>        </a></h2>
                    <div class="holder">

                    </div>
                    <p><?php the_content(); ?></p>
                </section>
                <?php endwhile; else: ?>
     <p><?php _e('Nothing here, sorry.'); ?></p>
     <?php endif; ?>
        </section>
        <aside>
            <?php get_sidebar(); ?>
        </aside>
         <div style="clear:both"></div>
        </div>   
        <div class="push"></div>
    </div>
</div>  
    <?php get_footer(); ?>

基本上,我想要的是从帖子中找到第一个图像,并将其放在.holder div中。它的工作原理应该类似于jQuery中的detach()和append()


我知道我应该使用过滤器和操作,但我不知道如何开始,所以任何帮助都将不胜感激

在您的文件functions.php中:

<?php 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 = $matches [1] [0];

if(empty($first_img)){ 
 //Defines a default image
 $first_img = "/images/default.jpg";
}
return $first_img;
}
?>

在index.php文件中,放置以下内容:

<div class="holder">
   <img src="<?php echo catch_that_image() ?>" />
</div>

" />

你为什么不直接将支架内的图像回显出来,而不是试图移动它around@DavidChase正如我所说,我是新手,你能给我解释一下怎么做,或者给我写一段代码吗?你能做的就是设置一个“特色形象”“你知道这个函数吗?试过了,对我来说真的不起作用。你想让holder div包含帖子中的多个第一个图像吗?谢谢,但是.holder只显示img src,而不是实际的图像。只是在.holder(而不是)中添加了“/”,一切都很好,谢谢。”。