I'm having hard time with this problems since I cant figure it out. I'm using 2 WP_Query loops for custom post types (slider and portfolio) on the same page. I also created a custom meta box for both custom post types.
So here is the code for index.php which Im using as Home template for displaying slider and portfolio items:
<?php
/*
Template Name: Home
*/
?>
<?php get_header(); ?>
<div id="header-container">
<div id="header">
<?php rm_slider(); ?> // This is where Im calling slider function to display the slider.
</div>
</div>
<div id="content">
<div class="container">
<?php $loop = new WP_Query(
array(
'post_type' => 'portfolio',
'posts_per_page' => -1
));
?>
<?php if ($loop->have_posts()) { ?>
<ul class="services">
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<li>
<?php if (has_post_thumbnail()) : the_post_thumbnail(); ?>
<?php else: ?>
<p>No portfolio image</p>
<?php endif; ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<p>Client: <?php echo get_post_meta($post->ID, '_project_client', true); ?></p>
<p>Client website: <?php echo get_post_meta($post->ID, '_project_client_url', true); ?></p>
</li>
<?php endwhile; } ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
And here is the code for slider.php:
<?php
// create slider markup
function rm_slider() {
$slider_loop = new WP_Query(
array(
'post_type' => 'slider',
'posts_per_page' => -1
));
if ($slider_loop->have_posts()) { ?>
<div id="slider">
<div class="slider-container">
<?php while ($slider_loop->have_posts()) : $slider_loop->the_post(); ?>
<div>
<?php if (has_post_thumbnail()) : the_post_thumbnail(); ?>
<?php else: ?>
<p>No slider image</p>
<?php endif; ?>
<div class="slide-info">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php
$slide_url = get_post_meta($post->ID, '_slide_url', true);
if ($slide_url != '') { ?>
<a href="<?php echo $slide_url; ?>" class="more-info"><?php echo $slide_url; ?></a>
<?php } else { echo 'empty?'; ?>
<?php
}
?>
</div>
<?php endwhile; ?>
</div><!-- .slider-container -->
</div><!-- #slider -->
<?php }
wp_reset_query();
}
?>
Im sure that the actual content from custom meta boxes is there, because when I only use 1 loop, it displays perfectly. But when using both loops, it only displays custom post meta only for the portfolio section. Im struggling with this problem whole day, please help me! Thanks :)
Strange, try changing this:
$slide_url = get_post_meta($post->ID, '_slide_url', true);
echo get_post_meta($post->ID, '_project_client', true);
for this:
$slide_url = get_post_meta(get_the_ID(), '_slide_url', true);
echo get_post_meta(get_the_ID(), '_project_client', true);
You could also try getting all post meta just to see if its all there.
$meta = get_post_meta( get_the_ID( ) );
print_r($meta); // prints the meta array to the screen, check your data is there.
As far as I know, after every WP_Query()
you should use:
wp_reset_postdata();
NOT . Have a try with this. wp_reset_query();
wp_reset_query()
restores the$wp_query
and global post data to the original main query. This function should be called afterquery_posts(
), if you must use that function. As noted in the examples below, it's heavily encouraged to use thepre_get_posts
filter to alter query parameters before the query is made.
and
wp_reset_postdata()
is used to restore the global$post
variable of the main query loop after a secondary query loop using newWP_Query
. It restores the $post variable to the current post in the main query.
And I also suggest you to try changing the possible redundant variable name like $loop
to something like $portfoliowLoop
etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.