Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Aurelian Spodarec
Aurelian Spodarec
10,801 Points

AFC PRO Flexible Content - Showing nothing

The content doesn't want to display. I have read and went through the documentation many times. I even asked other developers to see and check the code, and it looked correct for them.

Here are my fields: https://imgur.com/a/lMl4U

And below is my code. It always displays nothing. Why is that? I have version 5.6.1

// check if the flexible content field has rows of data

if( have_rows('social_media') ):

     // loop through the rows of data
    while ( have_rows('social_media_icons') ) : the_row();

        if( get_row_layout() == 'social_media_icons' ):

         echo   the_sub_field('media_facebook');

        endif;

    endwhile;

else :

    echo "nothing";// no layouts found

endif;

?>

Try changing your while loop to while ( have_rows('social_media') ) : the_row();

Aurelian Spodarec
Aurelian Spodarec
10,801 Points

That doesn't work either.

// check if the flexible content field has rows of data
if( have_rows('social_media') ):

     // loop through the rows of data
    while ( have_rows('social_media') ) : the_row();

        if( get_row_layout() == 'social_media_icons' ):

         echo   the_sub_field('media_facebook');

        endif;

    endwhile;

else :

    echo "nothing";// no layouts found

endif;

?>
Liam Maclachlan
Liam Maclachlan
22,805 Points

Hi Aurelian

When you say nothing, do you mean you get a white screen or tht it literally displays the word 'nothing'?

Cheers Liam

EDIT: And, silly question, does the page you are using this code on have the required ACF fields? Or are the fields containing any information?

Liam Maclachlan
Liam Maclachlan
22,805 Points

Not an answer but at the core of it, the logic is saying "I do not have the rows 'social_media'". This would be the starting point for debugging. I managed to get it working with the code I posted below and this make me think it is a config issue. either:

  • Wrong post type was selected for the options
  • The wrong template has been modified
  • There is no content in the fields for the post

4 Answers

Liam Maclachlan
Liam Maclachlan
22,805 Points

Make sure that the post type the ACF is set up for (on the backend) is set up for the template you are modifying:

  • post (single.php)
  • page (page.php)
  • category archive (category.php) etc.

Check this page out for a diagram on the WP Hierachy

Aurelian Spodarec
Aurelian Spodarec
10,801 Points

Hi Liam,

I did try and experiment with what you just said.

I need the custom field to be appeared in the Author Page.

So I set up

Show this field group to:

User Role 'is equal' to 'all'.

HOwever if I change this to e.g. the standard way of doing it - it works. So I know it works, the code is all good, etc.... but it has to do something with the author page on the info not beeing found. I suppose you need to get the author id, which is what I had to do in my other fields, but on this one here it doesn't work.

Not sure how can I make this to work.

FOr example this one here, I needed it to do it like this:

<?php
            $user_id = get_the_author_meta('ID');
        ?>


 <h2 class="author-profile__role"><?php the_field('description_role','user_'.$user_id); ?></h2>

Even though its the_field, on the othe rpage just the_field worked.

BUt this is author page, had to pass in the ID.

Not sure about the repeater and flexible content, as they seem to not work from the author page.

Liam Maclachlan
Liam Maclachlan
22,805 Points

Hi Aurelian,

Just need to make sure that the ACF code is inside the WP loop :)

<?php
/* Start the single post Loop */
while ( have_posts() ) : the_post();

    if( have_rows('social_media') ):
        // loop through the rows of data
        while ( have_rows('social_media') ) : the_row();
        // check current row layout
            if( get_row_layout() == 'social_media_icons' ):
            echo get_sub_field('media_facebook');       
        endif;
        endwhile;
    else :
        // no layouts found
        echo 'no content';
    endif;

endwhile; // End of the loop.
?>

Cheers Liam

Liam Maclachlan
Liam Maclachlan
22,805 Points

If this doesn't work, I'm setting up a local dev environment quickly to test :)

Aurelian Spodarec
Aurelian Spodarec
10,801 Points

Hi Liam, thanks but it still doesn't work.

no content 
no content 
no content 
no content 
no content 

Would you take my database and WordPress files perhaps? See if that has to do anything with it?

Can I ask for a slack or skype or something? If you want of course : p

Even posted this on stackoverflow and nothing.

Liam Maclachlan
Liam Maclachlan
22,805 Points

Hmm. It sounds like a config issue then. What post type is this ACF set up for and what template heave you modified? Let me know this and if what ever I think may be the problem isn't, if you could do a screen share I can help from there :)

Cheers Liam

Aurelian Spodarec
Aurelian Spodarec
10,801 Points

Oh, right, feel free to add me on skype at: aurelian.spodarec ( suit and pc - or just add all of me lol ) .

I replied above about the issue though :) I think it's close now.

Liam Maclachlan
Liam Maclachlan
22,805 Points

I've added you on Skype but if I can't see anything immediatly, you may need to give me FTP access and admin rights for the site. If you are working locally, I recommend using Local by Flywheel as you can share links to local projects and then I can just access the sites in the WordPress file editor :)

Liam Maclachlan
Liam Maclachlan
22,805 Points

-- Ignore -- Wrote in the wrong box...

Aurelian Spodarec
Aurelian Spodarec
10,801 Points

Nothing to see here.

WRONG POST

  • DELETE -
Liam Maclachlan
Liam Maclachlan
22,805 Points

Haha! Happens =p. Feel free to @liammaclachlan2 me on here if you have any further issues :)

Aurelian Spodarec
Aurelian Spodarec
10,801 Points

Still the same issue, i just wrote this in wrong box :D