wordpress template is outside the <Section>, how to display inside?

Asked

Viewed 35 times

-2

$teste_tp_slugs = ['company', 'search', 'customers'];

foreach ($teste_tp_slugs as $teste_tp_slug) {
    $output = '';
    $output .= '<section id="teste-site-home-' . $teste_tp_slug . '">';
    $output .= '<div class="container">';
    $output .= '<div class="row">';
    $output .= get_template_part('template-parts/home/sections/content', 'company');
    $output .= '</div>';
    $output .= '</div>';
    $output .= '</section>';

    return $output;
}

You’re showing off like this, off the ROW

Está exibindo assim

The right thing would be

  • 1

    This is usage error, you are using a syntax that does not correspond to manual orientation. This function does not return the value to concatenate, it plays the part in the output. What happens there is that you are generating the part immediately in the HTML, and then print the output (after you have already played the part in the output) then it is expected that the result of the function will come first. Show the output until you open the dív, then you call the function, and then close the div and the rest, then it is in the desired sequence.

  • 1

    @Johnnysilva note that this is a feature of the wordpress function you are using. It doesn’t return the value in the function, it plays straight out, so you can’t concatenate. I could understand what you want, but the function was not made to use like this (I think it would be better if it worked as you imagined, but whoever did the WP does not think the same way). You could do an exit with output buffer, but I think it gets worse. (starts with ob_start();, gives echo and at the end makes $output = ob_get_contents();

  • @Bacco understand, really has to be using "echo". I appreciate it, I’m still learning, and I need some of the information, but somehow I found the information.

  • @Johnnysilva I pressed the answer below the colleague Heathcliff to clarify better, test and see if solved. The "output buffer" avoids direct output in HTML, and at the end it plays in the value Return.

  • @Bacco Thank you very much!

  • @Johnnysilva note that if you give a Return inside the foreach will only see the first Slug, need to do the Return off, I hit this in the code of the fellow tb

  • @If you use "echo" you don’t even need ob_start() and Return at the end, it already displays all rsrs.

  • 1

    Yeah, it gets simpler, so I initially suggested echo. The output buffer solution is only interesting if you then need to concatenate the result with something else. In general, the less complicated, the better. I sincerely avoid using output buffer whenever possible The rare cases it makes sense to use are just these, of using something ready from third parties that does not work as expected (it almost always pays to do right and dispense with third-party code, in particular wordpress, which "redoes" something that PHP already has much simpler native)

Show 3 more comments

1 answer

3


$teste_tp_slugs = ['company', 'search', 'customers'];

ob_start();
foreach ($teste_tp_slugs as $teste_tp_slug) {
    echo '<section id="teste-site-home-' . $teste_tp_slug . '">';
    echo '<div class="container">';
    echo '<div class="row">';
    get_template_part('template-parts/home/sections/content', $teste_tp_slugs);
    echo '</div>';
    echo '</div>';
    echo '</section>';
}
return ob_get_clean();

Try that right there :)

  • it returned only the Parts template, but the other outputs ( <Section> etc) did not return :(

  • 1

    I took the liberty of touching your post. Considering that you are using output buffer, you need echo and not concatenation otherwise it is not in order. I approached and removed the Return from the loop so that the loop reaches the end. See comments on the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.