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

Adam Smallman
Adam Smallman
4,182 Points

work the_field not using css on all posts

Hi guys, I am following the videos on wordpress and I have come into a issue.

I have a basic website, I have created a few 'work' fields using the plugins.

I have used this code to display them onto the screen but the css does not generate for all of them only one of them.

So the first post works fine but the other posts have no css on them other then color change.

<ul id="gallery"> <?php

        $args = array (
          'post_type' => 'work'
        );

        $the_query = new WP_Query($args);
      ?>
      <li>
        <?php if( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post() ; ?>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?>
          <p><?php the_field('description'); ?></p>
        </a>
      </li>

<?php endwhile; else:?> <p>There are no posts</p> <?php endif ?> </ul>

Like I said it works, just that the css does not load for the other posts that it generates. The color of the text does

1 Answer

If your css is based on the li, then that may your issue. I would suggest moving the loop outside the list item, so that it is generating a list item on each post, rather than just anchors within a single list item. Also I don't see the opening or closing 'ul' tags.

<?php if ( $the_query->have_posts() ) : ?>

<ul>

<?php while( $the_query->have_posts() ) : $the_query->the_post() ; ?>

<li>
 <a href="<?php the_permalink(); ?>">
     <p><?php the_title(); ?></p>
     <p><?php the_field('description'); ?></p>
</a>
</li>

<?php  endwhile; ?>

</ul>

<?php endif; ?>
Adam Smallman
Adam Smallman
4,182 Points

That worked! thank you very much