Support

Account

Home Forums Add-ons Flexible Content Field Order inside a concatenated string

Solved

Order inside a concatenated string

  • I think this is a simple problem, but I’m stuck.

    I’ve got a flexible content block setup for page building, and I’ve run into an issue. One of the options is for the user to specify a heading level from a dropdown (heading 1 through heading 6). Which ever heading level is chosen, a string is output. When I get the data and concatenate the string to wrap some HTML around the data, the order is wrong.

    Here’s the code:

    
    while ( have_rows('page_layout') ) : the_row();
    if( get_row_layout() == 'heading_text' ):
    $headingLevel = the_sub_field('heading_level');
    echo '<h' . $headingLevel . '>Another variable here with heading content</' . $headingLevel . '>';
    

    The HTML output is as such:

    
    Heading 1<h>Another variable here with heading content</>
    

    The text ‘Heading 1’ should appear inside the HTML tags where I’ve used the variable, but it doesn’t.

    I realise I’ll have to then explode the string to isolate the number after ‘heading ‘, but I’d like to solve this problem first.

  • you need to use get_sub_field rather than the_sub_field. The first returns the values the second echoes the value, I also added the missing ‘h’ to the closing tag

    
    while ( have_rows('page_layout') ) : the_row();
      if( get_row_layout() == 'heading_text' ):
        $headingLevel = get_sub_field('heading_level');
        echo '<h' . 
          $headingLevel . 
          '>Another variable here with heading content</h' . 
          $headingLevel . '>';
    
  • Thank you, John. I knew it was something simple.. I don’t know how I missed it. Also, I isolated the heading number using the PHP explode function:

    
    					<?php
    
    						// check if the flexible content field has rows of data
    						if( have_rows('page_layout') ):
    
    						     // loop through the rows of data
    						    while ( have_rows('page_layout') ) : the_row();
    
    								
    								
    						        if( get_row_layout() == 'heading_text' ):
    		        	
    							        $headingLevel = get_sub_field('heading_level');
    							    	$headingLevelIsolated = (explode(" ",$headingLevel));
    							    	$headingContent = get_sub_field('heading_content');
    
    						        	echo '<h' . $headingLevelIsolated[1] . '>' . $headingContent . '</h' . $headingLevelIsolated[1] . '>';
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Order inside a concatenated string’ is closed to new replies.