【问题标题】:Php code between Tab Shortcode选项卡简码之间的 PHP 代码
【发布时间】:2018-02-21 08:50:53
【问题描述】:

我想在 tabby tabs 短代码之间插入 php 代码。

我正在使用插件tabby tab 进行标签视图,并在我的主题模板中添加了此代码:

<?php echo do_shortcode('[tabby title="Gallary Name"]
  name content 
  [tabby title="Images"]

  [tabbyending]'); ?>

我想使用如下代码在图像选项卡下使用自定义字段库:

    <?php echo do_shortcode('[tabby title="Gallary Name"]
  name content 
  [tabby title="Images"]

<?php 
$i = 0;
$images = get_field('vil_pics');
if( $images ): ?>
 <div>
   <ul>
        <?php foreach( $images as $image ): ?>
            <li<?php if ( $i % 3 == 0 ) echo ' class="break"' ?>>
                <a href="<?php echo $image['url']; ?>">
                     <img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
                </a><p>.</p>
                            </li>
        <?php endforeach; ?>
    </ul></div> 
<?php endif; ?>

[tabbyending]'); ?>

此代码不起作用,它显示一个空白页。我该如何解决这个问题?

【问题讨论】:

  • 一个问题是你们俩都在做的第一行echo。你会想要编译所有东西然后把它吐出来。

标签: php wordpress advanced-custom-fields


【解决方案1】:

Tabby 使用一个全局变量来跟踪正在发生的事情,所以我认为其中任何一个都可以。第一个更简单一点,但第二个肯定会起作用。

选项1:按顺序输出所有内容:

echo do_shortcode( '[tabby title="Gallery Name"] name content' );
echo do_shortcode( '[tabby title="Images"]' );

// your php code as-is
$i = 0;
$images = get_field('vil_pics');
if( $images ): ?>
  <div>
    <ul>
        <?php foreach( $images as $image ):
          $i++ ?>
          <li<?php if ( $i % 3 == 0 ) echo ' class="break"' ?>>
            <a href="<?php echo $image['url']; ?>">
              <img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
            </a><p>.</p>
          </li>
        <?php endforeach; ?>
    </ul>
  </div> 
<?php endif;

echo do_shortcode( '[tabbyending]' );

或选项 2: 将所有内容保存到变量中并一次全部输出:

$output = '';

$output .= '[tabby title="Gallery Name"] name content';
$output .= '[tabby title="Images"]';

$i = 0;
$images = get_field('vil_pics');
if ( $images ) {
  $output .= '<div><ul>';
    foreach( $images as $image ) {
      $i++;
      $li_class = ( $i % 3 == 0 ) ? ' class="break"' : '';

      $output .= '<li' . $li_class . '>';
      $output .= '<a href="' . $image['url'] . '">';
      $output .= '<img src="' . $image['sizes']['thumbnail'] . '" alt="' . $image['alt'] . '" />';
      $output .= '</a><p>.</p></li>';
    }
  $output .= '</div></ul>';
}

$output .= '[tabbyending]';

echo do_shortcode( $output );

请注意,我没有看到任何增加 $i 所以我添加了它。其他一切都照原样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多