Support

Account

Home Forums Add-ons Flexible Content Field Show all the flexible row labels used on a page?

Solving

Show all the flexible row labels used on a page?

  • I have a page with 5 flex fields.

    I want a way to show the labels of each field used on a page at the top for in-page navigation.

    I’ve been trying to get data out of the get_row_layout() but only see the field values, not the labels.

    Is there a way to get the labels used on a page?

    Maybe with with get_field_object()???

  • I really don’t understand what you mean by show all the labels of each field used.

    Can you supply more information.

  • @hube2 Yes no problem.

    So my page has a flexible field with 5 rows that I’m using.

    My goal is to have an in-page navigation menu with the nicename(aka label) of each of the rows with an in-page anchor link that will go to that section of the page.

    Since the pages could be changed, I can’t hardcode this, so I was hoping to set the code up to run through the rows that appear on a page, then set up links to each.

    If i use this code:

    <?php if (have_rows("content_builder")) {
        while (have_rows("content_builder")) {
            the_row();
            $flexField = get_row_layout();
            echo $flexField;
        }
    } ?>

    I get the slugs/name of each row.
    So and I know if i var_dump the main flexible field with get_field_object like:

    <?php if (have_rows("content_builder")) {
        while (have_rows("content_builder")) {
            the_row();
            $subs = get_field_object("content_builder");
            var_dump($subs);
        }
    } ?>

    i can see that the [label] fields are present in the array, “[“label”]=> string(23) “Neighbourhood: Overview”” but I cannot find a way to get it with the other row labels.

    Does that make more sense?

  • You have couple of choice, but either way it will mean looping over the rows twice.

    You can get the entire flex field as an array

    
    $flex_field = get_field('content_builder');
    

    Loop over this to create your in page navigation

    You can also use the have_rows() method.

    Or you can use a combination, doing it different for each. But in any case it will mean 2 loops. One to generate the navigation and another to generate the content.

  • @hube2 I’m still unsure how to get the actual label from that loop.

    If i call get_field(‘content_builder’); outside the “if (have rows…)’ part it gives me all all the data in that field, including rows that are not on the page, though the ‘label’ is in that array when I dump it.

    If i use it within the ‘have_rows’ and then try to do a foreach I get warnings like “Warning: Array to string conversion”

    Like if i set it up as:

    <?php if (have_rows("content_builder")) {
                while (have_rows("content_builder")) {
                    the_row();
                    $flexField = get_field("content_builder");
                    foreach ($flexField as $field) {
                        echo $field;
                        echo "<br>";
                    }
                }
            } ?>

    And if i var_dump $flexField inside the have_rows part, the label isn’t part of the array data. It seems to stay outside that loop.

  • I don’t think you are understanding. I have something similar. I have a flex field. Each layout has two field in it. One field is a unique ID field that is editable. This is what I use as an ID for the container for the row. The other is a label that is to be used in a link to that section. Here is the basics

    
    if (have_rows('flex_field')) {
      ?>
        <ul>
          <?php 
            while (have_rows('flex_field')) {
              the_row();
              ?><li><a href="#<?php 
                  the_sub_field('unique_id); ?>"><?php 
                  the_sub_field('link_label') ?></a></li><?php 
            }
          ?>
        </ul>
      <?php 
      reset_rows('flex_field');
      while (have_rows('flex_field')) {
        the_row();
        ?>
          <div id="<?php the_sub_field('unique_id'); ?>">
            // rest of layout code
          </div>
        <?php 
      }
    }
    
  • Thanks for the quick response!
    So what i’m trying to do though, is i have 5 different flex subfields on a page, each with a bunch of their own sub fields i use to display content.

    I need to get the labels/names of each of those main 5 flex fields. The names are not a sub-field field, but are the actual labels of the sub-field if that makes sense.

    So like:

    Main_FlexFied
    |-> Overview Field.     <--I need this field's label
          -> Overview Sub 1
          -> Overview Sub 2
    |-> Image and text Field.   <--I need this field's label
          -> Image field
          -> Text field
    |-> Full text field      <--I need this field's label
          -> Text title
          -> Text content
  • For that you need to get the field object, yes.

    https://www.advancedcustomfields.com/resources/get_sub_field_object/

    I don’t understand why you are trying to link to every field value.

  • Not every field value, just the first level sub fields under the main flex field.

    So I have 5 sub fields (rows) that could show up on the page.

    I’m building it as sections, so each primary sub-field is a section of the site with its own sub-fields in it.

    I want to just link to each of those main sections with a button that is automatically made based on if the first level sub-field exists on the page.

    The “get_row_layout();” would be great if it had more than just the slug of the row.

    I just can’t find a way to grab the label of those rows.

  • @hube2

    I used this to make it work:

    <?php if (have_rows("content_builder")) {
            $i = 0; ?>
        <section class="second-nav" id="second-nav">
            <div class="container">
                <div class="second-nav__menu" id="second-nav__menu">
                    <?php while (have_rows("content_builder")) {
                        the_row();
                        $i++;
                        if ($i != 1):
    
                            $flexField = get_row_layout();
                            $flexField = str_replace("_", "-", $flexField);
                            echo "<a href='#" . $flexField . "'>";
                            if ($flexField == "neighbourhood-overview") {
                                echo "Overview";
                            } elseif ($flexField == "neighbourhood-map") {
                                echo "What's Around";
                            } elseif ($flexField == "neighbourhood-stories") {
                                echo "Tenant Stories";
                            } elseif ($flexField == "show-other-properties") {
                                echo "Our Properties";
                            } elseif ($flexField == "other-neighbourhoods") {
                                echo "Our Neighbourhoods";
                            } else {
                                $flexField = str_replace(
                                    ["_", "-"],
                                    " ",
                                    $flexField,
                                );
                                echo ucwords($flexField);
                            }
                            echo "</a>";
                            ?>
    
        <?php
                        endif;
                    } 
    }
    }?>
  • @hube2
    Here’s what I used to grab the fields.
    I had to run the loop outside the main flex loop or else it caused timeout errors.
    I also removed the first row of the loop since it was a True/False for checking if the nav should be present on the page.

    Since the “get_row_layout()” only grabs the slug of the row, I had to manually change some of the title, but then made it just work for any others that may show up.

    <?php if (have_rows("content_builder")) {
            $i = 0; ?>
        <section class="second-nav" id="second-nav">
            <div class="container">
                <div class="second-nav__menu" id="second-nav__menu">
                    <?php while (have_rows("content_builder")) {
                        the_row();
                        $i++;
                        if ($i != 1):
    
                            $flexField = get_row_layout();
                            $flexField = str_replace("_", "-", $flexField);
                            echo "<a href='#" . $flexField . "'>";
                            if ($flexField == "neighbourhood-overview") {
                                echo "Overview";
                            } elseif ($flexField == "neighbourhood-map") {
                                echo "What's Around";
                            } elseif ($flexField == "neighbourhood-stories") {
                                echo "Tenant Stories";
                            } elseif ($flexField == "show-other-properties") {
                                echo "Our Properties";
                            } elseif ($flexField == "other-neighbourhoods") {
                                echo "Our Neighbourhoods";
                            } else {
                                $flexField = str_replace(
                                    ["_", "-"],
                                    " ",
                                    $flexField,
                                );
                                echo ucwords($flexField);
                            }
                            echo "</a>";
                            ?>
    
        <?php
                        endif;
                    }
    } } ?>
Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.