php - 当我从媒体库加载图片时,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中插入帖子。图像已上传但未附加。当我从媒体库加载图像时,但是当我从计算机的文件夹中加载时
解决方案
我用 get_post() 交换了 get_the_ID() 现在可以工作了
推荐阅读
- heroku - 如何使用部署在heroku上的rasa制作电报机器人
- mysql - 使用 slqu 在插入时使用 slick 返回结果集
- javascript - 更改特定输入字段时如何避免表单的onchange事件?
- bash - Bash Cronjob 没有执行第二个命令来启动新的 gnu-screen
- rxjs - 无限滚动的可观察分页以保持最新
- excel - VBA从另一个表调用标签
- assembly - 希望将数组中的第一个值修改为程序集中的新最大值
- eigen - 使用 unaryExpr 在 Eigen 中模拟 std::transform-like API
- selenium - Selenium Edge - 如何清除缓存并关闭恢复页面弹出窗口
- mysql - 更新数据库:使用 mysql 的 asp.net core 错误