0

使用高级自定义字段插件,我有一个自定义帖子类型,其中分配了各种自定义字段。

我正在尝试输出灵活内容字段“内容”中包含的所有数据

我让它输出“text_ad”没问题,但由于某种原因,我无法确定我们的“newsletter_article”,它是一个帖子对象 - 让它工作的任何方向都会令人惊叹。

阅读此https://www.advancedcustomfields.com/resources/flexible-content/和此https://www.advancedcustomfields.com/resources/post-object/

<?php
// check if the flexible content field has rows of data
if( have_rows('content') ):

 // loop through the rows of data
while ( have_rows('content') ) : the_row();

    if( get_row_layout() == 'text_ad' ):

        echo the_sub_field('text_ad_title');
        echo the_sub_field('text_ad_url');
        echo the_sub_field('text_ad_description');

    elseif( get_row_layout() == 'newsletter_article' ): 

        $post_object = get_sub_field('the_newsletter_article');
            if( $post_object ):
                   $post = $post_object;
                   setup_postdata( $post );?>
                   <strong><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></strong>

           <?php 
           wp_reset_postdata();
           endif;

    endif;

endwhile;

else :

// no layouts found

endif;
?>
4

1 回答 1

0

因为我只是得到一个对象,所以我可以像这样访问数据。

<?php
    elseif( get_row_layout() == 'newsletter_article' ): 


        $post_object = get_sub_field('the_newsletter_article');
        $newstitle = $post_object->post_title;
        $newsdescription = $post_object->post_content;
        ?>

        <div style="background-color: #cccccc; margin:0 0 20px 0;">
         <p><strong><?php echo $newstitle; ?></strong></p>
         <p><?php echo $newsdescription; ?></p>
        </div>

   <?php endif; ?>
于 2018-06-27T12:04:58.553 回答