• Resolved castorpollux285

    (@pascal2851981)


    Hello,

    I would like to use the shortcode in a template. The shortcode should enclose html and php. This is what I tried:

    <php echo do_shortcode('[expand title="Mehr Erfahren" swaptitle="Weniger" targclass="collapse-container" id="impressum" rel="star-highlander" findme="auto" trigclass="mehr-erfahren-trigger" tag="div"] <div class="mehr-erfahren-text-container"><div class="mehr-erfahren-text"><?php the_sub_field('bodytext', 'option'); ?></div></div>[/expand]'); ?>

    But unfortunateley it is not working. I guess the problem is that I try to execute php in php..?

    Thanks in advance!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author twinpictures

    (@twinpictures)

    Correct. Try this:

    <?php echo do_shortcode('[expand title="Mehr Erfahren" swaptitle="Weniger" targclass="collapse-container" id="impressum" rel="star-highlander" findme="auto" trigclass="mehr-erfahren-trigger" tag="div"] <div class="mehr-erfahren-text-container"><div class="mehr-erfahren-text">'. the_sub_field('bodytext', 'option') .'</div></div>[/expand]'); ?>
    
    Thread Starter castorpollux285

    (@pascal2851981)

    Thanks for your fast reply.

    When I insert your code, the collapsing text is positioned about the trigger.

    Here is a link to the website: https://krenn.design/collapse-o-matic

    It is the part under “überschrift”.

    Plugin Author twinpictures

    (@twinpictures)

    Not sure what mehr-erfahren-text-container and mehr-erfahren-text elements are for… try:

    <?php echo do_shortcode('[expand title="Mehr Erfahren" swaptitle="Weniger" targclass="collapse-container" id="impressum" rel="star-highlander" findme="auto" trigclass="mehr-erfahren-trigger" tag="div"]'. the_sub_field('bodytext', 'option') .'[/expand]'); ?>

    Regardless, it’s best to start off small and add things one by one so you know what is causing your issue.

    Thread Starter castorpollux285

    (@pascal2851981)

    I tried this but still the same result.

    Plugin Author twinpictures

    (@twinpictures)

    Still not seeing any content for the_sub_field('bodytext', 'option')
    try replacing this with the following:

    <?php 
    $sub_field = the_sub_field('bodytext', 'option');
    var_dump('sub_field:', sub_field);
    
    echo do_shortcode('[expand title="Mehr Erfahren" swaptitle="Weniger" targclass="collapse-container" id="impressum" rel="star-highlander" findme="auto" trigclass="mehr-erfahren-trigger" tag="div"]'. sub_field .'[/expand]');
    ?>
    Thread Starter castorpollux285

    (@pascal2851981)

    The content for

    the_sub_field('bodytext', 'option')

    is positioned above the trigger. I renamed it to “Bodytext”.

    Unfortunateley the last code is not working.

    Plugin Author twinpictures

    (@twinpictures)

    Please change:
    to:

    $sub_field = the_sub_field('bodytext', 'option');
    var_dump('sub_field:', $sub_field);
    
    echo do_shortcode('[expand title="Mehr Erfahren" swaptitle="Weniger" targclass="collapse-container" id="impressum" rel="star-highlander" findme="auto" trigclass="mehr-erfahren-trigger" tag="div"]'. $sub_field .'[/expand]');
    Thread Starter castorpollux285

    (@pascal2851981)

    Unfortunateley not working neither.

    Would it help to use Collapse-Pro-Matic or Collapse Commander?

    Plugin Author twinpictures

    (@twinpictures)

    Unfortunateley not working neither.

    Not so fast… take a look:

    string(10) "sub_field:" NULL
    This is a direct dump to the screen of the $sub_field variable defined as:

    $sub_field = the_sub_field('bodytext', 'option');
    var_dump('sub_field:', $sub_field);
    

    So the_sub_field('bodytext', 'option') is returning: NULL

    First, you will want to verify that the_sub_field function is working as desired.
    Upgrading to either Collapse-Pro-Matic or Collapse Commander will not magically fix this, but it will provide you a very high level of support that will get this issue resolved a bit quicker… but, not required.

    Thread Starter castorpollux285

    (@pascal2851981)

    Thanks for your reply.

    I’m not sure if I understand correctly, you mean that

    the_sub_field('bodytext', 'option')

    is not returning anything?

    But it is returning the Text “Bodytext” that is positioned above the trigger?

    Plugin Author twinpictures

    (@twinpictures)

    Ah so!
    This means that the_sub_field('bodytext', 'option') is not ‘returning’ the value, but rather directly echoing it. Assuming this is the ACF the_sub_field function, then the documentation provides the answer:

    This function will display a sub field value from a repeater field or flexible content field loop. This function is used within a have_rows() loop.

    This function is the same as echo get_sub_field(‘name’);.

    So the correct code should use get_sub_field as follows:

    $sub_field = get_sub_field(‘bodytext’, ‘option’);

    echo do_shortcode('[expand title="Mehr Erfahren" swaptitle="Weniger" targclass="collapse-container" id="impressum" rel="star-highlander" findme="auto" trigclass="mehr-erfahren-trigger" tag="div"]'. $sub_field .'[/expand]');

    We are going to close this issue as it’s not related to our plugin.

    Thread Starter castorpollux285

    (@pascal2851981)

    That is working neither, but if this is an acf related problem then I understand that you can`t give me support on this problem.

    Thank you!

    Plugin Author twinpictures

    (@twinpictures)

    right. you can test this by using the following:

    $sub_field = 'The target content';
    
    echo do_shortcode('[expand title="Mehr Erfahren" swaptitle="Weniger" targclass="collapse-container" id="impressum" rel="star-highlander" findme="auto" trigclass="mehr-erfahren-trigger" tag="div"]'. $sub_field .'[/expand]');
    

    If that works, then you need to focus on how to properly use the get_sub_field function.

    All the best.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Shortcode in template which includes html and php’ is closed to new replies.