自定义帖子类型中未显示自定义字段

时间:2016-05-05 作者:CZorio

我正在整理一个基本的登录页http://shurity.com/, 在试图清理代码时,我想为用户用动态内容替换一些静态内容。

所讨论的元素是单击“订阅”时弹出的模式。。。当我尝试将文本包含在循环中时,它不会显示。下面的代码是一个模板文件,它与我为模式创建的自定义帖子类型相匹配,帖子类型有3个自定义字段,分别用于模式的标题、正文和页脚文本。\\u content()正在提取实际表单。

<?php 
$modal_header    = get_field(\'modal_header\');
$modal_body    = get_field(\'modal_body\');
$modal_footer    = get_field(\'modal_footer\');

 ?>

<!-- MODAL
================================================== -->
<div class="modal fade" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">

        <?php
            $args = array(
                \'post_type\' => \'modal\',
                \'posts_per_page\' => 1
                );

            $loop = new WP_Query($args); ?>


                <?php
                if ( $loop -> have_posts() ) :

                    /* Start the Loop */
                    while ( $loop -> have_posts() ) : $loop -> the_post();

                        /*
                         * Include the Post-Format-specific template for the content.
                         * If you want to override this in a child theme, then include a file
                         * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                         */
                        //get_template_part( \'template-parts/content\', get_post_format() );
                        ?>

                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                            <h4 class="modal-title" id="myModalLabel"><i class="fa fa-envelope"></i> <?php echo $modal_header; ?></h4>
                        </div><!-- modal-header -->

                        <div class="modal-body">
                            <p><?php echo $modal_body; ?></p>

                            <?php the_content(); ?>
                        </div><!-- modal-body -->

                        <hr>

                        <p><small><?php echo $modal_footer; ?></small></p>

                    <?php endwhile;

                else :

                    get_template_part( \'template-parts/content\', \'none\' );

                endif; ?>

                <?php  wp_reset_postdata(); ?>

        </div><!-- modal-content -->
    </div><!-- modal-dialog -->
</div><!-- modal -->
我在页脚中调用此模板部分。php。我是Wordpress的新手,如果错误很明显,请原谅。表单基本上会出现,但我的三个自定义字段不会出现。这就是现在的样子。。

enter image description here

3 个回复
SO网友:Owais Alam

请尝试以下代码

<?php
/*
Template Name: Template_Name
*/
get_header();
$modal_header    = get_field(\'modal_header\');
$modal_body    = get_field(\'modal_body\');
$modal_footer    = get_field(\'modal_footer\');
echo \'<pre>\';print_r($modal_header);echo \'</pre>\'; // show the content of filed its for checking purpose.
echo \'<pre>\';print_r($modal_body);echo \'</pre>\';// show the content of filed its for checking purpose.
echo \'<pre>\';print_r($modal_footer);echo \'</pre>\';// show the content of filed its for checking purpose.
 ?>

<!-- MODAL
================================================== -->
<div class="modal fade" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">

        <?php
            $args = array(
                \'post_type\' => \'modal\',
                \'posts_per_page\' => 1
                );

            $loop = new WP_Query($args); ?>


                <?php
                if ( $loop -> have_posts() ) :

                    /* Start the Loop */
                    while ( $loop -> have_posts() ) : $loop -> the_post();
                     ?>

                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                            <h4 class="modal-title" id="myModalLabel"><i class="fa fa-envelope"></i> <?php echo $modal_header; ?></h4>
                        </div><!-- modal-header -->

                        <div class="modal-body">
                            <p><?php echo $modal_body; ?></p>

                            <?php the_content(); ?>
                        </div><!-- modal-body -->

                        <hr>

                        <p><small><?php echo $modal_footer; ?></small></p>

                    <?php endwhile;

                else :

                    get_template_part( \'template-parts/content\', \'none\' );

                endif; ?>

                <?php  wp_reset_postdata(); ?>

        </div><!-- modal-content -->
    </div><!-- modal-dialog -->
</div><!-- modal -->

<?php get_footer(); ?>

SO网友:CZorio

在Stackoverflow上找到答案。自定义字段需要在循环内调用,如。。。

while ( $loop -> have_posts() ) : $loop -> the_post();

$modal_header    = get_field(\'modal_header\');
$modal_body      = get_field(\'modal_body\');
$modal_footer    = get_field(\'modal_footer\');

....
答案来自Codeforest

SO网友:MahdiY

尝试以下操作:

    <?php
        $args = array(
            \'post_type\' => \'modal\',
            \'posts_per_page\' => 1
            );

        $loop = new WP_Query($args); ?>


            <?php
            if ( $loop->have_posts() ) :

                /* Start the Loop */
                while ( $loop->have_posts() ) : $loop->the_post();

                    /*
                     * Include the Post-Format-specific template for the content.
                     * If you want to override this in a child theme, then include a file
                     * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                     */
                    //get_template_part( \'template-parts/content\', get_post_format() );
                    ?>

                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                        <h4 class="modal-title" id="myModalLabel"><i class="fa fa-envelope"></i> <?php echo get_field(\'modal_header\'); ?></h4>
                    </div><!-- modal-header -->

                    <div class="modal-body">
                        <p><?php echo get_field(\'modal_body\'); ?></p>

                        <?php the_content(); ?>
                    </div><!-- modal-body -->

                    <hr>

                    <p><small><?php echo get_field(\'modal_footer\'); ?></small></p>

                <?php endwhile;

            else :

                get_template_part( \'template-parts/content\', \'none\' );

            endif; ?>

            <?php  wp_reset_postdata(); ?>

    </div><!-- modal-content -->
</div><!-- modal-dialog -->

相关推荐