douan6815 2018-07-05 16:26
浏览 72
已采纳

Wordpress:如果将html / php混合到帖子中,则插入块的最佳做法

I'm trying to insert a block of mixed html/php code into a Wordpress post. It is filled by custom field values from ACF Pro. It's supposed to be part of the post, so I try to avoid editing template files.

The block looks contains roughly the following:

<?php if (get_field('infobox_anzeigen') == 1): ?>
    <div class="touristische-informationen">

    <h2>Informationen</h2>

      <!-- Start Allgemein -->
      <?php if( have_rows('ti_allg')): ?>

      <h3>Übersicht</h3>
      <div class="ti-div">

        <?php if (have_rows('ti_allg_bezeichnung')): ?>
          <p><b>Name: <?php the_field('ti_allg_bezeichnung_name') ?> (<?php the_field('ti_allg_typ'); ?>)</b></p>
            <?php if (get_field('ti_allg_bezeichnung_kanji')): ?>
              <p>Kanji: <?php the_field('ti_allg_bezeichnung_kanji'); ?> | <?php the_field('ti_allg_bezeichnung_kana'); ?> | <i><?php the_field('ti_allg_bezeichnung_hepburn'); ?></i></p>
            <?php endif; ?>
        <?php endif; ?>

        <?php if (get_field('ti_allg_besucht')): ?>
          <p>Besucht am: <?php the_field('ti_allg_besucht'); ?></p>
        <?php endif; ?>

...imagine the end of this code.

What's certainly working are those "insert php snippets" plugins, which I'd like to avoid for now. I tried making a custom shortcode via custom plugin, but because of the mixed code nature, I ran into issues with echo and output buffering etc. Although that would be probably the best solution for my use case as this would allow me to still add text before and after the block.

How would you tackle this problem? Using a insert php plugin? Following the custom shortcode approach? Something entirely else?

  • 写回答

1条回答 默认 最新

  • douhuangjie4503 2018-07-05 16:43
    关注

    The order I'd consider tackling these is:

    1. Modifying single.php, whichever template is responsible for this, or a custom functions file (functions.php, a /mu-plugins/ file, or plugin, etc.)
    2. Using custom shortcodes.
    3. Using a PHP inserter plugin. Either one that lets you use [php]echo 'test';[/php] type tags or inserted snippets via shortcodes.

    Note that 3 doesn't make sense if this block of code is repeated even a few times, and 2 doesn't make sense if it's repeated on the majority of posts unless you need granular control of where it's inserted.

    Honestly the best practice here is to drop that into single.php wherever it goes, or filter/hook it in using something like functions.php. "It's supposed to be part of the post" doesn't mean you can't modify template files. Just hook it in the right place.

    If it's supposed to be in between content somewhere, then you'll probably need to consider using a shortcode or mergetag to tell it exactly where to insert this code. If it goes before/after the content (but still in the content area) modifying the template is a-okay and very straight forward and probably the "best practice" in that case.

    Edit: Here's an example of appending the code using output buffer and the_content filter:

    <?php
        add_filter( 'the_content', 'so_51196107_after_content' );
        function so_51196107_after_content( $content ){
            if( is_singular( 'post' ) ):
                if( get_field('infobox_anzeigen') == 1 ): ob_start(); ?>
                    <div class="touristische-informationen">
                        <h2>Informationen</h2>
                        <?php if( have_rows( 'ti_allg' ) ): ?>
                            <h3>Übersicht</h3>
                            <div class="ti-div">
                                <?php if( have_rows( 'ti_allg_bezeichnung' ) ): ?>
                                    <p><b>Name: <?php the_field( 'ti_allg_bezeichnung_name' ) ?> (<?php the_field( 'ti_allg_typ' ); ?>)</b></p>
                                    <?php if( get_field( 'ti_allg_bezeichnung_kanji' ) ): ?>
                                        <p>Kanji: <?php the_field( 'ti_allg_bezeichnung_kanji' ); ?> | <?php the_field( 'ti_allg_bezeichnung_kana' ); ?> | <i><?php the_field( 'ti_allg_bezeichnung_hepburn' ); ?></i></p>
                                    <?php endif; ?>
                                <?php endif; ?>
                                <?php if( get_field( 'ti_allg_besucht' ) ): ?>
                                    <p>Besucht am: <?php the_field('ti_allg_besucht'); ?></p>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                <?php
                    $infobox  = ob_get_clean();
                    $content .= $infobox;
                    endif;
            endif;
    
            return $content;
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看