Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Page content in ACF Flexible Content loop WordPress

  • SOLVED

I am using a theme I have found that loops through ACF flexible content modules and displays them on the page as required. Whilst this method is great, I would like to have a fallback option of using the standard page content if no modules are included. Ideally I would like the standard page content to be included via the get_page_template method. I have tried the following but the standard page content is not displaying. Can anyone help?

<?php get_header();?>
<div id="content">
<div id="inner-content" class="cf">
<main id="main" role="main" itemscope itemprop="mainContentOfPage" itemtype="http://schema.org/Blog">

<?php while ( have_posts() ) { the_post();

// Check if ACF is enabled and the modules field exists
if ( function_exists('get_field') && get_field('modules') !== null ) {

the_modules_loop();

} else {

get_template_part( 'partials/content', 'page' );

}

}
?>
</main>
</div>
</div>
<?php get_footer();?>

Answers (4)

2018-02-06

Arnav Joy answers:

Can you please write the_content() in place of get template part function and see if it outputs it or not

Secondary are you sure when there is no flexible content then else part is executed ?


flint_and_tinder comments:

No it doesn't. If I add it outside the <main> tag it does, but not in place of the get_template_part.


Arnav Joy comments:

you have not given me answer for my following question
Secondary are you sure when there is no flexible content then else part is executed ?

can you add me on skype to have quick chat ?
my id is : arnav.joy

2018-02-06

Cesar Contreras answers:

To show the content, you just have to replace
get_template_part( 'partials/content', 'page' );
with
the_content()
in your code


flint_and_tinder comments:

I would specifically like to use a php template for the content feed so that I can use elsewhere.


Cesar Contreras comments:

You can load the custom .PHP file.
Just have to include it in the following way:
get_template_part ('partials / content', 'page');

You are including it correctly, maybe you just need the file.
in this case it is assumed that your file is in the "partials" folder and has the name "content-page.php"

you can read the documentation to better understand how "get_template_parts" works.
https://developer.wordpress.org/reference/functions/get_template_part/

2018-02-06

Andrea P answers:

the line of code:
get_template_part( 'partials/content', 'page' );

will basically just include the file:
your-theme/partials/content-page.php

that's what it means. so you'll need to create that file and put the html markup for showing the normal content.

then this function will actually print out the page content:

<?php the_content(); ?>

in any case, watch out because acf repeaters and probably the flexible content too, sometimes don't result to be null/empty if you added some values and then deleted them.

you'd better check if the flexible content field "have_rows" or not. like:

if ( have_rows('modules') ) {
// loop the acf flexible fields
}
else {
// show the content
}


have a look at this documentation about how to loop through the flexible content fields:
https://www.advancedcustomfields.com/resources/flexible-content/


flint_and_tinder comments:

The content-page.php file does contain that function.


Andrea P comments:

Have you tried to use have_rows() to check if the modules are empty?
I have the feeling that the problem is that the code never goes into the conditional where the get_template_part() is.

2018-02-09

timDesain Nanang answers:

since the @return value of get_field is mixed, false or $value (if the value found),
change this part:
if ( function_exists('get_field') && get_field('modules') !== null ) {
with:
if ( function_exists('get_field') && get_field('modules') ) {