如何在页面上获取标题、描述、图片?

时间:2015-12-04 作者:harley

我创建了一个模板,但它没有输出任何内容

 <?php
/*
Template Name: Gallery
*/

get_header(); 


?>
<section>

    <div class="row">
        <div class="container">
            <ul>
                <?php if ( have_posts() ) : while ( have_posts() ) : the_post();    

                 $args = array(
                   \'post_type\' => \'attachment\',
                   \'numberposts\' => -1,
                   \'post_status\' => null,
                   \'post_parent\' => $post->ID
                  );

                  $attachments = get_posts( $args );
                     if ( $attachments ) {
                        foreach ( $attachments as $attachment ) {
                           echo \'<li>\';
                           echo wp_get_attachment_image( $attachment->ID, \'full\' );
                           echo \'<p>\';
                           echo apply_filters( \'the_title\', $attachment->post_title );
                           echo \'</p></li>\';
                          }
                     }

                 endwhile; endif; ?>
                </ul>
        </div>
    </div>
</section>
<?php get_footer();?>

1 个回复
最合适的回答,由SO网友:Kvvaradha 整理而成

用同样的方法尝试get_children. 它会帮你的。下面是一个示例代码。

    $args = array(
    \'numberposts\' => -1,
    \'order\' => \'ASC\',
    \'post_mime_type\' => \'image\',
    \'post_parent\' => $post->ID,
    \'post_status\' => null,
    \'post_type\' => \'attachment\',
);

$attachments = get_children( $args );

if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
                   echo $attachment->post_title; 

                    echo $attachment->post_content; 

        $image_attributes = wp_get_attachment_image_src( $attachment->ID, \'thumbnail\' )  ? wp_get_attachment_image_src( $attachment->ID, \'thumbnail\' ) :   wp_get_attachment_image_src( $attachment->ID, \'full\' );

        echo \'<img src="\' . wp_get_attachment_thumb_url( $attachment->ID ) . \'" class="current">\';
      }
    }
我不确定你代码中的问题。但这会给你带来结果。

相关推荐