使用ACF在自定义wordpress页面模板中显示单个帖子内容

时间:2017-11-02 21:15:19

标签: php wordpress advanced-custom-fields

我创建了一个自定义字段,允许我从我网站中的各个帖子中进行选择。 导出的acf xml

<title>Posts to page</title>
        <link>http://wp.wafisherinteractive.com/nabc/?acf=acf_posts-to-page</link>
        <pubDate>Wed, 01 Nov 2017 16:41:44 +0000</pubDate>
        <dc:creator>XXXX</dc:creator>
        <guid isPermaLink="false">XXXXXXXXXXX</guid>
        <wp:post_id>6505</wp:post_id>
        <wp:post_date>2017-11-01 11:41:44</wp:post_date>
        <wp:post_date_gmt>2017-11-01 16:41:44</wp:post_date_gmt>
        <wp:comment_status>closed</wp:comment_status>
        <wp:ping_status>closed</wp:ping_status>
        <wp:post_name>acf_posts-to-page</wp:post_name>
        <wp:status>publish</wp:status>
        <wp:post_parent>0</wp:post_parent>
        <wp:menu_order>0</wp:menu_order>
        <wp:post_type>acf</wp:post_type>
        <wp:post_password></wp:post_password>
        <wp:postmeta>
            <wp:meta_key>_edit_last</wp:meta_key>
            <wp:meta_value><![CDATA[1]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key>_publicize_pending</wp:meta_key>
            <wp:meta_value><![CDATA[1]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key>field_59f9f8b8ad422</wp:meta_key>
            <wp:meta_value><![CDATA[a:12:{s:3:"key";s:19:"field_59f9f8b8ad422";s:5:"label";s:12:"post to page";s:4:"name";s:12:"post_to_page";s:4:"type";s:11:"post_object";s:12:"instructions";s:50:"Select the post you'd like to display on this page";s:8:"required";s:1:"1";s:9:"post_type";a:1:{i:0;s:3:"all";}s:8:"taxonomy";a:1:{i:0;s:3:"all";}s:10:"allow_null";s:1:"0";s:8:"multiple";s:1:"1";s:17:"conditional_logic";a:3:{s:6:"status";s:1:"0";s:5:"rules";a:1:{i:0;a:2:{s:5:"field";s:4:"null";s:8:"operator";s:2:"==";}}s:8:"allorany";s:3:"all";}s:8:"order_no";i:0;}]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key>position</wp:meta_key>
            <wp:meta_value><![CDATA[normal]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key>layout</wp:meta_key>
            <wp:meta_value><![CDATA[no_box]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key>hide_on_screen</wp:meta_key>
            <wp:meta_value><![CDATA[]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key>rule</wp:meta_key>
            <wp:meta_value><![CDATA[a:5:{s:5:"param";s:13:"page_template";s:8:"operator";s:2:"==";s:5:"value";s:15:"single_post.php";s:8:"order_no";i:0;s:8:"group_no";i:0;}]]></wp:meta_value>
        </wp:postmeta>
    </item>

我希望能够选择帖子并在新页面上分配该内容。我将自定义字段限定为自定义页面模板

<?php 

/* Template Name: Single Post */

get_header(); ?>



    <div class="row">
        <div class="large-12 columns">  

                <?php

                /*
                *  Loop through post objects (assuming this is a multi-select field) ( setup postdata )
                *  Using this method, you can use all the normal WP functions as the $post object is temporarily initialized within the loop
                *  Read more: http://codex.wordpress.org/Template_Tags/get_posts#Reset_after_Postlists_with_offset
                */

                $post_objects = get_field('post_objects');

                if( $post_objects ): ?>
                    <ul>
                    <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
                        <?php setup_postdata($post); ?>
                        <li>
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            <span>Post Object Custom Field: <?php the_field('post_to_page'); ?></span>
                        </li>
                    <?php endforeach; ?>
                    </ul>
                    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
                <?php endif;

                /*
                *  Loop through post objects (assuming this is a multi-select field) ( don't setup postdata )
                *  Using this method, the $post object is never changed so all functions need a seccond parameter of the post ID in question.
                */

                $post_objects = get_field('post_objects');

                if( $post_objects ): ?>
                    <ul>
                    <?php foreach( $post_objects as $post_object): ?>
                        <li>
                            <a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a>
                            <span>Post Object Custom Field: <?php the_field('post_to_page', $post_object->ID); ?></span>
                        </li>
                    <?php endforeach; ?>
                    </ul>
                <?php endif;

                ?>

        </div>
    </div>

<?php get_footer(); ?>

所以当我用该模板创建一个新页面时,我可以通过它的标题选择一个帖子。 these are a list of the post titles

但该帖子的内容不会显示。

1 个答案:

答案 0 :(得分:0)

在您发布的示例中,您没有将内容写入页面的任何位置。这个模板标签也被称为&#39; the_content&#39;