[英]Passing Custom Post type field outside of wp_query loop
我有2個Slick Sliders,它們有自己的循環,只能通過每個post types標簽進行過濾,效果很好。 但是,我試圖將每個循環之外的某些自定義字段傳遞給模式。 這是其中一個滑塊的代碼,與第二個滑塊基本相同:
環:
<section class="regular slider-actual actual-projects-container">
<?php $args = array('post_type' => 'proyecto', 'tag' => 'actual'); ?>
<?php $loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); $postid=the_ID();?>
<a href="#" data-toggle="modal" data-target="#myModal-<? the_ID();?>">
<img src="<?php the_field('project_image'); ?>">
</a>
<div class="projects-container">
<div class="name-bar row">
<div class="col-7">
<h2 class="proj-title"><?php the_field('project_name'); ?></h2>
</div>
<div class="col-5">
<h2 class="proj-cat"></h2>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<h1>No posts here!</h1>
<?php endif; ?>
</section>
模態:
<div class="modal fade" id="myModal-<? the_ID(); ?>" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'proyecto', true);
?>
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"><?php the_field('project_name'); ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="modBody">
<?php the_field('project_video');?>
</div>
</div>
</div>
</div>
我一直在閱讀,考慮到該模式僅顯示第一篇帖子信息,無論單擊哪個幻燈片,似乎帖子ID都沒有傳遞到該模式中。 因此,至少它是在檢索帖子,但不是在檢索它所對應的帖子。 如果有幫助,我將ACF用於自定義字段。 任何幫助,將不勝感激,謝謝。
您可以使用echo get_field( 'meta_key', get_the_ID() );
或者可能將$postid
更改$postid
get_the_ID()
或執行諸如global $post;
並echo $post->ID;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.