首页 > 解决方案 > 当我从媒体库加载图片时,wordpress 5 空站点

问题描述

我目前正在尝试在 wordpress 中从 bootstrap 4 实现轮播。不幸的是,当我从 wordpress 媒体库加载图片时,我得到了空站点。哪里不足?这是测试代码

<?php
function image_slider_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;
}

?>

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

    <?php if( image_slider_get_attachment() ):
        $attachments = image_slider_get_attachment(5);
    //  var_dump($attachments);
    ?>

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

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

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

                    <div class="carousel-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-prev" href="#post-gallery-<?php the_ID(); ?>" role="button" data-slide="prev">
                <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                <span class="sr-only">Previous</span>
            </a>
            <a class="right carousel-control-next" href="#post-gallery-<?php the_ID(); ?>" role="button" data-slide="next">
                <span class="carousel-control-next-icon" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
            </a>

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

    <?php endif; ?>



</header>

</article>

在此处输入图像描述

我需要以编程方式在wordpress中插入帖子。图像已上传但未附加。当我从媒体库加载图像时,但是当我从计算机的文件夹中加载时

标签: phpwordpresstwitter-bootstrapbootstrap-4wordpress-5

解决方案


我用 get_post() 交换了 get_the_ID() 现在可以工作了


推荐阅读