Support

Account

Home Forums General Issues Can't get values while creating loop for shortcode

Solved

Can't get values while creating loop for shortcode

  • So i’m trying to create small plugin for specific shortcode, but i’m not getting values from subfields, so probably i’m missing something.

    The code:

    function ssf(){
    	global $post;
    	$post_id = $post->id;
    	$rep = get_field('sourcepage', $post_id);
    	if ($rep && is_array($rep)) { 
    		$linkbutton = the_sub_field('link_przycisku');
    	    $textbutton = the_sub_field('nazwa_przycisku');
    		foreach ($rep as $item) {
    			$output .= '<span class="set blue">
    				<a class="btn-pbt pri ico" href="'.$item['linkbutton'].'">'.$item['textbutton'].'</a>
    			</span>';	
    		}
    	}
    	return $output;
    }
    add_shortcode('guzik', 'ssf');

    Basically it’s repeater with two subfields. The Output from shortcode should render button with custom link and custom name.

    Output doesn’t render any values from fields(only span container), i would appreciate some help in that matter.

  • Hi @nikodemsky.

    Thanks for reaching out to us.

    You will need to change the_sub_field method to get_sub_field in order to output the value.

    Let me know how it goes.

  • Already tested it before, but it’s the same.

    You can look by yourself:
    https://playernotes.pl/test/

    Proper button should look like this:
    button

    Fields are filled ofc:
    fields

  • Ok, i solved my problem. Seems like those two directives was not even required(i can use subfields as arrays).

    The problem was with NAMES of those fields! I forgot _ symbol in between.

    So the code at the end is:

    function ssf(){
    	global $post;
    	$post_id = $post->id;
    	$rep = get_field('sourcepage', $post_id);
    	if ($rep && is_array($rep)) {
    		foreach ($rep as $item) {
    			$output .= '<span class="set blue">
    				<a class="btn-pbt pri ico" href="'.$item['link_przycisku'].'">'.$item['nazwa_przycisku'].'</a>
    			</span>';
    		}
    	}
    	return $output;
    }
    add_shortcode('guzik', 'ssf');
  • Hi @nikodemsky

    I’m glad you were able to solve the problem.

    Feel free to reach out to me in case something else comes up 🙂

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Can't get values while creating loop for shortcode’ is closed to new replies.