0

我正在尝试将静态 HTML 网站转换为 WordPress 主题。目前,有以下(静态)代码:

<div class="postwrap">
    <div class="first-col">

        <!-- Post Description -->
         <h1>Post Title</h1>
        <p>Post description. At essimag nimilib errorum fuga. Harunt faci aut elitatur.</p>

    </div>
    <div class="second-col">

        <!-- Post Images -->
        <img src="images/placeholder.png" alt="This is a photo">

    </div>
</div>

如您所见,其中的所有内容first-col都是基于文本的。里面的所有内容second-col都是图片,WordPress 只是简单的使用<?php get_content(); ?>抓取这个内容。

有没有办法可以将提交的图像排序到单独的 div 中,例如second-col?我知道这可以用 jQuery 完成,但我想知道是否有 PHP 或 WordPress 的方法。

4

1 回答 1

4

Yes, you can display any images that have been uploaded/attached to a post/page by using the following code.

<div class="second-col">

    <?php
    // Get all images uploaded/attached to this post/page.
    if (
        $images = get_posts(
            array(

                'post_parent'       =>  get_the_ID(),
                'post_type'         =>  'attachment',
                'post_mime_type'    =>  'image',
                'orderby'           =>  'menu_order',
                'order'             =>  'ASC'

                )
            )
        ) :

    // Loop through the results and display each full-size image.
    foreach( $images as $image ) :
        echo wp_get_attachment_image( $image->ID, 'full' );
    endforeach;

    endif;
    ?>

</div>

get_posts() is a WordPress function that returns an array of posts based on the parameters we pass to it. We then loop through that array and call the wp_get_attachment_image() function on each value in it. wp_get_attachment_image() is another WordPress function that returns an HTML image element.

In order to display the images only in the second column, do NOT insert them into the post/page. Simply upload them from the post/page edit screen then close the modal window and update the post/page. You could also upload images from the media library and attach them to the post/page you want.

于 2013-08-28T17:02:25.373 回答