0

我正在为高级自定义字段使用灵活内容插件,并且在我的模板代码中格式化一个简单的按钮链接时遇到了问题。

通常我会使用这样的东西......

<a class="button" href="<?php the_field('button-link'); ?>"><?php the_field('button-text'); ?></a>

...输出以下html:

<a class="button" href="http://url.com/the-link">The Text</a>

但我不知道如何调整它以在我的灵活内容模板中工作:

<?php

if( have_rows('sidebar-content') ):

    while ( have_rows('sidebar-content') ) : the_row();

        if( get_row_layout() == 'text-content' ):

            echo '<div class="sidebar-text-content">';
                the_sub_field('flexible-text-content');
            echo '</div>';

        elseif( get_row_layout() == 'quote' ):

            echo '<blockquote>';
                the_sub_field('quote-text');
                echo '<span class="quote-source">';
                    the_sub_field('quote-source');
                echo '</span>';
            echo '</blockquote>';

        elseif( get_row_layout() == 'images' ):

            $image = get_sub_field('image');
            echo '<img src="' . $image['sizes']['large'] . '" alt="' . $image['alt'] . '" />';

        endif;

    endwhile;

endif;

?>

有什么建议么?提前致谢

4

1 回答 1

0

对于那些将来可能会发现这很有帮助的人,我将我的代码修改如下:

<?php if( have_rows('sidebar-content') ): ?>

    <?php while ( have_rows('sidebar-content') ) : the_row(); ?>

        <?php if( get_row_layout() == 'text-content' ): ?>

            <div class="sidebar-text-content">
                <?php the_sub_field('flexible-text-content'); ?>
            </div>

        <?php elseif( get_row_layout() == 'quote' ): ?>

            <blockquote>
                <?php the_sub_field('quote-text'); ?>
                <span class="quote-source">
                    <?php the_sub_field('quote-source'); ?>
                </span>
            </blockquote>

        <?php elseif( get_row_layout() == 'images' ):

            $image = get_sub_field('image');
            echo '<img src="' . $image['sizes']['large'] . '" alt="' . $image['alt'] . '" />';

        ?>

        <?php elseif( get_row_layout() == 'button' ): ?>

            <a class="button" href="<?php the_sub_field('button-link'); ?>"><?php the_sub_field('button-text'); ?></a>

        <?php endif; ?>

    <?php endwhile; ?>

<?php endif; ?>
于 2014-02-13T18:53:27.263 回答