dongzhuifeng1843 2019-04-05 23:40
浏览 111
已采纳

WordPress:不能在PHP回显线中包含自定义的帖子类型循环(使用ACF的“the_field”)

I'm learning how to create new custom post types with WordPress + Custom Post Types UI + Advanced Custom Fields, and I'm having a weird problem. Basically I want to be able to add a custom HTML "data-target" attribute (to open modals with Bootstrap) directly from my Wordpress dashboard.

Here is the code :

<div class="slider slider--columns" data-arrows="true" data-paging="true">
<ul class="slides">

<?php 

$args = array(
'post_type' => 'MY_POST_TYPE',
'posts_per_page' => -1 
);

$MYPOSTTYPE = new WP_Query ($args);
if ($MYPOSTTYPE->have_posts() ):
    while ($MYPOSTTYPE->have_posts() ): $MYPOSTTYPE->the_post();

echo '<li class="col-sm-4 col-xs-6" data-toggle="modal" data-target="'.the_field('datatarget_gds').'">';
echo '<div id="MY_ID" style="background-image: url(BGIMAGE.png) !important;"><img style="visibility: hidden;" alt="Image" src="dummyimage.png"></div>';
echo '</li>';


endwhile; endif;
wp_reset_postdata();

?>

</ul>
</div>

But when starting the web page, the values that should appear inside the "data-target" attribute appear just before the opening of the <li> tag, in perfect order by the way. Why ?
Oh and as you can see I'm using Flickity here, starting in the first Div, but I doubt that's the fault.

Thanks !

  • 写回答

1条回答 默认 最新

  • douyu4535 2019-04-06 00:13
    关注

    Okay, well, problem solved. I simply realized the solution was to put that HTML code out of the PHP.

    So here is the concrete code :

    <div class="slider slider--columns" data-arrows="true" data-paging="true">
    <ul class="slides">
    
    <?php 
    
    $args = array(
    'post_type' => 'MY_POST_TYPE',
    'posts_per_page' => -1 
    );
    
    $MYPOSTTYPE = new WP_Query ($args);
    if ($MYPOSTTYPE->have_posts() ):
        while ($MYPOSTTYPE->have_posts() ): $MYPOSTTYPE->the_post();
    
    ?>
    
    <li class="col-sm-4 col-xs-6" data-toggle="modal" data-target="<?php the_field('datatarget_gds') ?>">
    <div id="MY_ID" style="background-image: url(BGIMAGE.png) !important;"><img style="visibility: hidden;" alt="Image" src="dummyimage.png"></div>
    </li>
    
    <?php
    
    endwhile; endif;
    wp_reset_postdata();
    
    ?>
    

    But still, I imagine this may be complicated if for some reason you need to use some PHP echo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条