Support

Account

Home Forums Add-ons Flexible Content Field Store single layout's content & load on another page

Solved

Store single layout's content & load on another page

    • halo

    • June 2, 2020 at 1:41 am

    Hey 🙂

    I’m looking for a way to make our client’s lives easier.

    When creating a page using flexible content there might be a “Call to action” layout; they might primarily use this layout in two or three ways across the site so instead of having them re-input the content on every page they’d like to be able to save the two or three ways they primarily use the layout and then be able to select that on another page, loading in the layout and it’s options.

    For example, this post does a great job of loading an entire flexible content layout from one page to another, but what i’d like to be able to do is save a specific layout’s content/options that can then be individually added to the field array.

    Thanks for any help/ideas, appreciated

  • It would be very difficult to pick a specific layout already created for another page. How I handle this is that I create a CPT where “global layouts” or something similar can be added. Then on the page there a is layout called “Global Layout” that includes a single post object field where they can select one of the posts in the CPT.

    • halo

    • June 2, 2020 at 6:01 pm

    Thanks John, great solution to the problem without pushing the boat out and doing something crazy, many thanks 🙂

  • Just make sure that the client understands the concept that this new thing is for layouts that will be use on multiple pages of a site. I once did this and the person entering the content created every layout as a global layout and then used nothing but global layouts when creating pages. It was a nightmare and the need to query 6 to 10 other posts to display each page seriously dragged down the performance of the site.

    • oliwa

    • January 9, 2021 at 11:37 pm

    Hello, this is very interesting as i’m currently working to make exactly this feature possible. With a “Post Object” row layout i’m able to get title and other metadata from the ‘Global’ CPT but i struggle with getting the attached row layout from that ‘global’ post. Is there any code sample or further advice how to do this?

  • My use of flex fields in likely rather different than others. I have a function similar to:

    
    functions do_layouts($post_id=false) {
       if (!$post_id) {
         // call function or figure out post id to use
       }
       if (have_rows('flex-field', $post_id) {
         while (have_rows('flex-field, $post_id) {
           the_row();
           $layout = get_row_layout();
           switch($layout) {
              // ...
              case 'global-panel':
                $gp = get_field('post-object', false, false); // ID only
                if ($gp) {
                  do_layouts($gp);
                }
                break;
              // ...
              default:
                get_template_part('template-parts/layouts/'.$gp);
                break;
           } // end switch
         } // end whild
       } // end if
    }
    
    • oliwa

    • January 10, 2021 at 10:53 pm

    Thank you John, i feel still not sure if i understand that approach the right way. In my case, the pages and the “Global Layout” CPT (set as Post) do use the same include (Field Group) where the Flexible Content Layouts are defined.

    Everything works fine on the pages, but i get no data when i add a row from the global layouts. I guess i have to pass the ID and get the metadata of the global layout, and then switch back (wp_reset_postdata) for the next row that could be an element of the page. Or maybe i’m totally wrong with this way?

  • If you are only getting the ACF fields from the CPT then you don’t need to get the post. If you need the post data then this is completely different that using flex field layouts and I don’t have enough information to make any suggestions.

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

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.