wordpress附件没有抓住正确的图像

时间:2017-09-29 18:22:15

标签: php wordpress attachment

我试图在帖子中附加图片列表来构建滑块,但是抓住的图像是媒体库中的前两个图像而不是图像将其添加到帖子

内容-gallery.php

<article id="post-<?php the_ID(); ?>" <?php post_class( 'advanceplr-format-gallery' ); ?>>
<header class="entry-header text-center">

    <?php if( advanceplr_get_attachment() ):
        $attachments = advanceplr_get_attachment(7);
        var_dump($attachments);
    ?>

    <div id="post-gallery-<?php the_ID(); ?>" class="carousel slide" data-ride="carousel">

        <div class="carousel-inner" role="listbox">

            <?php
                $i = 0;
                foreach( $attachments as $attachment ):
                $active = ( $i == 0 ? ' active' : '' );
            ?>

            <div class="item<?php echo $active; ?> background-image standard-featured" style="background-image: url( <?php echo wp_get_attachment_url( $attachment->ID ); ?> );"></div>

            <?php $i++; endforeach; ?>

        </div><!-- .carousel-inner -->

            <a class="left carousel-control" href="#post-gallery-<?php the_ID(); ?>" role="button" data-slide="prev">
                <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                <span class="sr-only">Previous</span>
            </a>
            <a class="right carousel-control" href="#post-gallery-<?php the_ID(); ?>" role="button" data-slide="next">
                <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
            </a>

    </div><!-- .carousel -->

        <?php endif; ?>

        <?php the_title( '<h1 class="entry-title"><a href="'. esc_url( get_permalink() ) .'" rel="bookmark">', '</a></h1>'); ?>

        <div class="entry-meta">
            <?php echo advanceplr_posted_meta(); ?>
        </div>

</header>

<div class="entry-content">

    <div class="entry-excerpt">
        <?php the_excerpt(); ?>
    </div>

    <div class="button-container text-center">
        <a href="<?php the_permalink(); ?>" class="btn btn-advanceplr"><?php _e( 'Read More' ); ?></a>
    </div>

</div><!-- .entry-content -->

<footer class="entry-footer">
    <?php echo advanceplr_posted_footer(); ?>
</footer>

主题support.php

function advanceplr_get_attachment( $num = 1 ){

$output = '';
if( has_post_thumbnail() && $num == 1 ): 
    $output = wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) );
else:
    $attachments = get_posts( array( 
        'post_type' => 'attachment',
        'posts_per_page' => $num,
        'post_parent' => get_the_ID()
    ) );
    if( $attachments && $num == 1 ):
        foreach ( $attachments as $attachment ):
            $output = wp_get_attachment_url( $attachment->ID );
        endforeach;
    elseif( $attachments && $num > 1 ):
        $output = $attachments;
    endif;

    wp_reset_postdata();

endif;

return $output; }

我还有一个使用相同代码的图像的帖子格式,它可以正常工作,

内容-image.php

<article id="post-<?php  the_ID(); ?>" <?php post_class( 'advanceplr-format-image' ); ?> >

    <header class="entry-header text-center background-image" style="background-image: url(<?php echo advanceplr_get_attachment(); ?>);">


        <?php the_title( '<h1 class="entry-title"><a href="'. esc_url( get_permalink() ) .'" rel="bookmark">', '</a></h1>' ); ?>

        <div class="entry-meta">
            <?php echo advanceplr_posted_meta(); ?>
        </div>

        <div class="entry-excerpt image-caption">
            <?php the_excerpt(); ?>
        </div>

    </header>

<footer class="entry-footer">
    <?php echo advanceplr_posted_footer(); ?>
</footer>

0 个答案:

没有答案