部分代码变为变量

时间:2015-04-14 15:54:40

标签: php

我怎样才能将代码加入到#34;我的代码"一部分?

$out.= '<li class="portfolio_item overlayed_animated_highlight portfolio_item_4' . $slugs . '">
            <div class="overlayed">
                ' . get_the_post_thumbnail() . '
                <div class="overlay">
                    <p>
                        <a href="'.get_permalink().'"><i class="icon-share"></i></a>
                        <a href="'.$thumbnail_src.'" class="fancybox" data-fancybox-group="portfolio"><i class="icon-search"></i></a>
                    </p>
                </div>
            </div>
            /** my code **/
            <h4><a href="' . get_permalink() . '">' . get_the_title() . '</a></h4>
            <span>' . get_the_date() . ' // ' . $in_category . '</span>
        </li>';

/ **我的代码** /

<?php if( get_field('field_name') ): ?>
<div><?php the_field('field_name'); ?></p></div>
<?php endif; ?>

2 个答案:

答案 0 :(得分:1)

您可以使用ternary operator.试试这个:

$out.= '<li class="portfolio_item overlayed_animated_highlight portfolio_item_4' . $slugs . '">
        <div class="overlayed">
            ' . get_the_post_thumbnail() . '
            <div class="overlay">
                <p>
                    <a href="'.get_permalink().'"><i class="icon-share"></i></a>
                    <a href="'.$thumbnail_src.'" class="fancybox" data-fancybox-group="portfolio"><i class="icon-search"></i></a>
                </p>
            </div>
        </div>
        '.(get_field('field_name') ? '<div>'.the_field('field_name').'</div>' : '').'
        <h4><a href="' . get_permalink() . '">' . get_the_title() . '</a></h4>
        <span>' . get_the_date() . ' // ' . $in_category . '</span>
    </li>';

答案 1 :(得分:1)

检查出来:

$out .= '<li class="portfolio_item overlayed_animated_highlight portfolio_item_4' . $slugs . '">
            <div class="overlayed">
                ' . get_the_post_thumbnail() . '
                <div class="overlay">
                    <p>
                        <a href="'.get_permalink().'"><i class="icon-share"></i></a>
                        <a href="'.$thumbnail_src.'" class="fancybox" data-fancybox-group="portfolio"><i class="icon-search"></i></a>
                    </p>
                </div>
            </div>';

if( get_field('field_name') ) {
    $out .= '<div><p>' . the_field('field_name') . '</p></div>';
}

$out .= '<h4><a href="' . get_permalink() . '">' . get_the_title() . '</a></h4>
            <span>' . get_the_date() . ' // ' . $in_category . '</span>
        </li>';