advanced custom fields - If statement shortcode

So, I am creating a shortcode to return some ACF content.The following code works:function howitworks() {$page = get_pag

So, I am creating a shortcode to return some ACF content.

The following code works:

function howitworks() {
    $page = get_page_by_title('shop');
    if (have_rows('steps', $page->ID)) :
        while (have_rows('steps', $page->ID)) : the_row();
            $image = get_sub_field('icon');
            $i = '<div class="col">
                    <img src="'.$image['sizes']['thumbnail'].'" alt="'.$image['alt'].'" title="'.$image['alt'].'" />
                    <h2>'.the_sub_field('step').'</h2>
                    <p>'.the_sub_field('description').'</p>
                </div>';
        endwhile;
    endif;
    return $i;
}
add_shortcode('howitworks', 'howitworks');

The problem I am having is I'd like to add a div container in-between the if and the while. I have tried multiple ways. I tried another variable like $j = '<div class="test">' then added another return return $i, $j...but then I have a syntax error. If I add a semi-colon the error goes away, but the div doesn't get returned. If I wanted the div inside the while, I'd be fine. But it seems like I'm having issues because I'm trying to put it outside the while.

The other thing I tried was just to return the outer div like: return '<div class="test">'; which will return the div, but then my original return doesn't return anything.

Any ideas?

Thanks,
Josh

So, I am creating a shortcode to return some ACF content.

The following code works:

function howitworks() {
    $page = get_page_by_title('shop');
    if (have_rows('steps', $page->ID)) :
        while (have_rows('steps', $page->ID)) : the_row();
            $image = get_sub_field('icon');
            $i = '<div class="col">
                    <img src="'.$image['sizes']['thumbnail'].'" alt="'.$image['alt'].'" title="'.$image['alt'].'" />
                    <h2>'.the_sub_field('step').'</h2>
                    <p>'.the_sub_field('description').'</p>
                </div>';
        endwhile;
    endif;
    return $i;
}
add_shortcode('howitworks', 'howitworks');

The problem I am having is I'd like to add a div container in-between the if and the while. I have tried multiple ways. I tried another variable like $j = '<div class="test">' then added another return return $i, $j...but then I have a syntax error. If I add a semi-colon the error goes away, but the div doesn't get returned. If I wanted the div inside the while, I'd be fine. But it seems like I'm having issues because I'm trying to put it outside the while.

The other thing I tried was just to return the outer div like: return '<div class="test">'; which will return the div, but then my original return doesn't return anything.

Any ideas?

Thanks,
Josh

Share Improve this question edited Nov 22, 2019 at 18:38 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Nov 22, 2019 at 18:20 joshmrodgjoshmrodg 1,1731 gold badge16 silver badges42 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Try this:

<?php

function howitworks() {
    $page = get_page_by_title('shop');
    $html = '';
    if (have_rows('steps', $page->ID)) :
        $html .= '<div class="test">';
        while (have_rows('steps', $page->ID)) : the_row();
            $image = get_sub_field('icon');
            $html .= '<div class="col">
                    <img src="'.$image['sizes']['thumbnail'].'" alt="'.$image['alt'].'" title="'.$image['alt'].'" />
                    <h2>'.get_sub_field('step').'</h2>
                    <p>'.get_sub_field('description').'</p>
                </div>';
        endwhile;
        $html .= '</div>';
    endif;
    return $html;
}
add_shortcode('howitworks', 'howitworks');
?>

EDIT: I change up my code for replace the_sub_field by get_sub_field

So, I wanted to give Sam credit, he solved the issue, so his answer is the right one...I just made a little tweak to get it 100% working. I made get_sub_field variables instead of calling them inline, which helped them display properly (instead of above my div container) My final code looks like:

function howitworks() {
    $page = get_page_by_title('shop');
    $html = '';
    if (have_rows('steps', $page->ID)) :
        $html .= '<div class="test">';
        while (have_rows('steps', $page->ID)) : the_row();
            $image = get_sub_field('icon');
            $step = get_sub_field('step');
            $desc = get_sub_field('description');
            $html .= '<div class="col"><img src="'.$image['sizes']['thumbnail'].'" alt="'.$image['alt'].'" title="'.$image['alt'].'" /><h2>'.$step.'</h2><p>'.$desc.'</p></div>';
        endwhile;
        $html .= '</div>';
    endif;
    return $html;
}
add_shortcode('howitworks', 'howitworks');

Thanks,
Josh

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744978209a4604276.html

相关推荐

  • advanced custom fields - If statement shortcode

    So, I am creating a shortcode to return some ACF content.The following code works:function howitworks() {$page = get_pag

    22小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信