What's causing an infinite loop?
I have to use parent posts as categories.
So I'm checking if current post has childs and show them or else show te content.
I'm trying like this (in single-practice.php):
?php
$has_children = false;
$children = get_children(
array(
'post_parent' = get_the_ID()
)
);
wp_reset_postdata();
if ( ! empty($children) ) {
$has_children = true;
}
if ($has_children) {
$args = array(
'post_type' = 'practice',
'orderby' = 'menu_order',
'post_parent' = get_the_ID(),
'paged' = false
);
$query = new WP_Query( $args );
/* var_dump($query); */
?
div class="services"
div data-isotope-options='{"itemSelector" : ".item","layoutMode" : "masonry","transitionDuration":"0.7s","masonry" : {"columnWidth":".item"}}' class="grid isotope clearfix portfolio-holder"
?php while ( $query-have_posts() ) : $query-the_post();
?
div class="item item-col col-1 "
a href="?php the_permalink(); ?" class="service-item"
span class="overlay"/span
figureimg width="320" height="305" src="?php echo wp_get_attachment_image_url(get_post_thumbnail_id(get_the_ID())); ?" class="attachment-320x305-center-center size-320x305-center-center wp-post-image" alt="" //figure
span class="service-title"?php the_title(); ?/span
/a
/div
?php
endwhile;
wp_reset_postdata();
?/div
/div!--/ .entry-box--?php
} else {
/* this is the original template code. */
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/post/content', get_post_format() );
endwhile;
the_posts_pagination( array(
'prev_text' = 'span class="screen-reader-text"' . esc_html__( 'Previous page', 'hickshynson' ) . '/span',
'next_text' = 'span class="screen-reader-text"' . esc_html__( 'Next page', 'hickshynson' ) . '/span',
'before_page_number' = 'span class="meta-nav screen-reader-text"' . esc_html__( 'Page', 'hickshynson' ) . ' /span'
) );
the_content();
wp_link_pages( array(
'before' = 'div class="page-links"' . esc_html__( 'Pages:', 'hickshynson' ),
'after' = '/div',
'link_before' = 'span class="page-number"',
'link_after' = '/span',
) );
}
?
Note that the else is the same code that was in the template before I added the if.
For some reason, this code never finishes if the current post does not have children.. so I cannot see the page fully loaded,
What might be causing this? I'm using wp_reset_postadata()
but the infinite loop still there,
Any idea?
-this is the $args how registered the post type-
$args = array(
'labels' = hh_get_labels(
__( 'Area', 'hh_app_textdomain' ),
__( 'Areas', 'hh_app_textdomain' )
),
'public' = true,
'archive' = true,
'exclude_from_search' = false,
'publicly_queryable' = true,
'show_ui' = true,
'query_var' = true,
'capability_type' = 'post',
'has_archive' = true,
'hierarchical' = true,
'menu_position' = null,
'supports' = array( 'title', 'editor', 'thumbnail', 'excerpt', 'page-attributes' ),
'rewrite' = array( 'slug' = self::$slug ),
'show_in_admin_bar' = true,
'taxonomies' = array('post_tag'),
'menu_icon' = 'dashicons-portfolio'
);
-UPDATE-
By removing this block
/* this is the original template code. */
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/post/content', get_post_format() );
endwhile;
The problem is no longer there. That's ok because the_content()
has all I need, but still curious. Any thoughts?
Topic wp-reset-postdata loop wp-query Wordpress
Category Web