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 trialAdam Smallman
815 Pointsmultiple Custom posts not working
Hi guys,
I am having some issues with getting more than one Custom Post on the website.
I have used
<?php rewind_posts(); ?>
and
<?php wp_reset_postdata(); ?>
with no success, I get an internal error if the custom field has any content. The default text does not show up either.
If I add in some text for the custom post then I get the internal error.
Thanks for any help guys!
3 Answers
Zac Gordon
Treehouse Guest TeacherOkay, then it sounds like you want to focus on trying to get that single loop to work.
Some suggestions:
- Make sure the custom post type slug is correct
- Make sure you have content added in the admin area
- Try echoing out the title instead of a custom field to start
On a side note, also, read this article on resetting the loop if you haven't already.
Zac Gordon
Treehouse Guest TeacherPost your code please!
Adam Smallman
815 PointsHi Zac,
So here is the code,
This is the first
<?php
$args = array (
'post_type' => 'welcome-text'
);
$the_query = new WP_Query($args);
?>
<?php if (have_posts() ) : while ($the_query->have_posts() ) : $the_query->the_posts(); ?>
<h1><?php the_field('text-top') ;?></h1>
<?php wp_reset_postdata(); ?>
<?php endwhile; endif; ?>
and the other
<?php
$args = array(
'post_type' => 'work'
);
$the_query = new WP_Query($args);
?>
<?php if (have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href='<?php the_permalink(); ?>'><li><img class='image-area' src='<?php the_field('image'); ?>'></li></a>
<?php wp_reset_postdata(); ?>
<?php endwhile; endif; ?>
I have tried a few different ways of doing it from the Wordpress codex
Zac Gordon
Treehouse Guest TeacherWhat happens when you just try one without the other?
Adam Smallman
815 PointsThe bottom one works fine either way, but the top one does not work. The default text that I have put in does not show up either. I see if it works without the bottom post.
Adam Smallman
815 PointsAdam Smallman
815 PointsOh my goodness! mega embarrassing! I have found the problem, I had a rouge 's' in the code
$the_query->the_posts();
changing it to
$the_query->the_post();
fixes the issue at it works great now.
Thank you so much for all the help, I will take a look at that link too.
and Thank you for the great video lessons on Wordpress!