Home › Forums › Add-ons › Flexible Content Field › 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] . '>';
The topic ‘Order inside a concatenated string’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.