使自定义字段图像附件工作 - WordPress

时间:2014-10-22 14:21:33

标签: php wordpress

我正在为预先存在的WordPress网站添加一些功能。这个网站不是我写的,我只是调整了原有的代码。

我试图在我们的某个模板上添加一个位于某些手风琴下拉列表上方的图像。 我已经能够将自定义字段添加到custom_fields.php文件中,并且它显示在WP管理员端,但我无法弄清楚如何使图像显示在浏览器中。

我试图添加的图片我正在命名" accordion_image"

自定义字段:

Carbon_Container::factory('custom_fields', __('Info Page Titles', 'domain'))
    ->show_on_post_type('page')
    ->show_on_template('template-info.php')
    ->add_fields(array(
        Carbon_Field::factory('text', 'info_subtitle'),
    ));

Carbon_Container::factory('custom_fields', __('Accordions Section', 'domain'))
    ->show_on_post_type('page')
    ->show_on_template('template-info.php')
    ->add_fields(array(
        Carbon_Field::factory('text', 'acc_section_title', 'Section Title'),
        Carbon_Field::factory('complex', 'accordions')
            ->add_fields(array(
                Carbon_Field::factory('text', 'accordions_title'),
                Carbon_Field::factory('complex', 'accordion')
                    ->add_fields(array(
            Carbon_Field::factory('attachment', 'accordion_image')
              ->set_required(true),
                        Carbon_Field::factory('text', 'accordion_head')
                            ->set_required(true),
                        Carbon_Field::factory('textarea', 'accordion_content')
                            ->set_required(true),
                    )),
            )),
    ));

模板标记:

            <?php 
            $acc_section_title = carbon_get_the_post_meta('acc_section_title');
            $section_accordions = carbon_get_the_post_meta('accordions', 'complex');

            if (!empty($section_accordions) && is_array($section_accordions)): ?>
                <div class="col-right">
                    <?php if ($acc_section_title): ?>
                        <h4><?php echo esc_html($acc_section_title); ?></h4>
                    <?php endif ?>

                    <div class="faq">                       
                        <?php foreach ($section_accordions as $counter => $accordions): 
                            $accordions_title = $accordions['accordions_title'];

                            if ($accordions_title): ?>
                                <h5><?php echo esc_html($accordions_title) ?></h5>
                            <?php endif ?>

                            <?php if (!empty($accordions['accordion']) && is_array($accordions['accordion'])): ?>
                                <div class="accordion">
                                  <?php foreach ($accordions['accordion'] as $acc_counter => $accordion): ?>
                                      <div class="accordion-section">
                                        <?php 
                  $accordion_image = $accordion['accordion_image'];
                                        $accordion_head = $accordion['accordion_head'];
                                        $accordion_content = $accordion['accordion_content'];

                   if ($accordion_image): ?>
                     <div class="accordion-img">
                      <?php echo esc_html($accordion_image) ?>
                    </div><!-- /.accordion-img -->
                  <?php endif ?>


                                        if ($accordion_head): ?>
                                            <div class="accordion-head">
                                                <?php echo esc_html($accordion_head) ?>
                                            </div><!-- /.accordion-head -->
                                        <?php endif ?>

                                        <?php if ($accordion_content): ?>
                                            <div class="accordion-body">
                                                <?php echo wpautop($accordion_content); ?>
                                            </div><!-- /.accordion-body -->
                                        <?php endif ?>
                                      </div><!-- /.accordion-section -->
                                  <?php endforeach ?>
                                </div><!-- /.accordion -->
                            <?php endif ?>
                        <?php endforeach ?>
                    </div><!-- /.faq -->
                </div><!-- /.col-right -->
            <?php endif ?>

我刚刚学习php,所以如果这是一个愚蠢的问题我会道歉。 谢谢大家的帮助!

1 个答案:

答案 0 :(得分:1)

看起来问题是语法问题的混合......以及一个简单的问题,即你回显图像的ID,但不输出图像本身。您需要使用wp_get_attachment_image() functionecho esc_html($accordion_image);,而不是echo wp_get_attachment_image( esc_html($accordion_image), 'full' );,而不是$acc_section_title = carbon_get_the_post_meta('acc_section_title'); $section_accordions = carbon_get_the_post_meta('accordions', 'complex'); if (!empty($section_accordions) && is_array($section_accordions)): ?> <div class="col-right"> <?php if ($acc_section_title): ?> <h4><?php echo esc_html($acc_section_title); ?></h4> <?php endif; ?> <div class="faq"> <?php foreach ($section_accordions as $counter => $accordions): $accordions_title = $accordions['accordions_title']; if ($accordions_title): ?> <h5><?php echo esc_html($accordions_title); ?></h5> <?php endif; ?> <?php if (!empty($accordions['accordion']) && is_array($accordions['accordion'])): ?> <div class="accordion"> <?php foreach ($accordions['accordion'] as $acc_counter => $accordion): ?> <div class="accordion-section"> <?php $accordion_image = $accordion['accordion_image']; $accordion_head = $accordion['accordion_head']; $accordion_content = $accordion['accordion_content']; if ($accordion_image): ?> <div class="accordion-img"> <?php echo wp_get_attachment_image( esc_html($accordion_image), 'full' ); ?> </div><!-- /.accordion-img --> <?php endif; ?> <?php if ($accordion_head): ?> <div class="accordion-head"> <?php echo esc_html($accordion_head); ?> </div><!-- /.accordion-head --> <?php endif; ?> <?php if ($accordion_content): ?> <div class="accordion-body"> <?php echo wpautop($accordion_content); ?> </div><!-- /.accordion-body --> <?php endif; ?> </div><!-- /.accordion-section --> <?php endforeach; ?> </div><!-- /.accordion --> <?php endif; endforeach; ?> </div><!-- /.faq --> </div><!-- /.col-right --> <?php endif; ?>

{{1}}