好的,我有一个自定义的帖子类型,叫做多媒体库。它使用一个转发器字段构建,可以拍摄视频、图像、链接、iframe等。
I\'m using this plugin 要将这些自定义帖子插入到博客帖子中,基本上允许我使用一个短代码将我的MM库插入到博客帖子中。
问题是。。。If和While语句似乎需要一个post ID来实际返回正确的信息。但我不太确定是否能做到这一点。
这是我的代码:
<?php
/*
Template Name: Gallerys
*/
//Variables
$initialCardLoad = 5;
$loopLazyLoad = 0;
$gridPostID = get_the_ID();
?>
<section class="grid-content">
<div class="gridGallery">
<?php
// check if the repeater field has rows of data
if( have_rows(\'card\', 355) ):
// loop through the rows of data
while ( have_rows(\'card\', 355) ) : the_row();
$loopLazyLoad++; // iterate on loop each time you loop through
// display a sub field value inside a card
?>
<?php
//VARIABLES//
$title = get_sub_field(\'card_title\');
$childImage = get_sub_field(\'card_picture\');
$file = get_sub_field(\'card_video\');
$video = $file[\'url\'];
if($loopLazyLoad > $initialCardLoad):
if( $childImage ): ?>
<div class="grid-item">
<a data-fancybox="gallery" data-caption="<?php echo $title ?>" href="<?php
echo $childImage[\'url\'];//big one here ?>">
<img class="lazy" data-src="<?php echo $childImage[\'url\']; ?>">
</a>
<?php endif; ?>
<?php if( $video ): ?>
<div class="grid-item">
<video class="gallery-video lazy" loop autoplay muted>
<source src="<?php echo $video; ?>" type="video/mp4">
</video>
<?php endif; ?>
</div> <?php //end of grid item div
else : // Note the combination of the words.
if( $childImage ): ?>
<div class="grid-item">
<a data-fancybox="gallery" data-caption="<?php echo $title ?>" href="<?php
echo $childImage[\'url\'];//big one here ?>">
<img class="" src="<?php echo $childImage[\'url\']; ?>">
</a>
<?php endif; ?>
<?php if( $video ): ?>
<div class="grid-item">
<video class="gallery-video" loop autoplay muted>
<source src="<?php echo $video; ?>" type="video/mp4">
</video>
<?php endif; ?>
</div> <!-- end of grid item div -->
<?php endif; //end of initial card load of loop
endwhile; //end of while statement
355是手动插入正确的post ID,用于测试其是否工作。是的!但我如何让它充满活力呢?当我尝试使用get\\u the\\u ID()时,它只会获取我插入的帖子的ID,而不是我插入的帖子的ID。
最合适的回答,由SO网友:Jacob Peattie 整理而成
从插件代码中,我可以看到它使用query_posts()
若要为模板设置新上下文,请使用display="blah.php"
.
因此,您只需在模板周围添加循环:
<?php
/**
* Template Name: Gallery
*/
while ( have_posts() ): the_post(); // <-- Important bit
if( have_rows( \'card\' ) ):
while ( have_rows( \'card\' ) ) : the_row();
// Stuff
endwhile;
endif;
endwhile; // <-- Important bit