• Resolved Noisie

    (@noisie)


    Hello,

    I have a custom template loaded on a wordpress page. The Custom template loads in a custom post type query loop:

    [please review http://codex.wordpress.org/Forum_Welcome#Posting_Code for posting code in the forum]

    <?php
      $temp = $wp_query;
      $wp_query = null;
      $wp_query = new WP_Query();
      $wp_query->query('showposts=10&post_type=test'.'&paged='.$paged); 
    
      while ($wp_query->have_posts()) : $wp_query->the_post();
    ?>
    
    <li>
    <?php the_title( '<a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a>' ); ?>
    </li>
    <?php endwhile; wp_reset_query(); ?>

    In the functions.php is a special custom post type based on test created.

    The problem is: When I fill in the page title and content WYSIWYO editor it doesn’t loop this content.

    Im using a general page loop to show this content after the custom post type loop has ended:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
     <h1><?php the_title() ;?></h1>
     <?php the_content(); ?>
    <?php endif; ?>

    The second general page loop will still take the loop content of the first loop with content of test instead of the content I typed in the general page.

    I hope someone can help me with this issue.

Viewing 3 replies - 1 through 3 (of 3 total)
  • please post the full code of the template.

    try to use wp_reset_postdata(); instead of wp_reset_query(); and don’t forget to restore the $temp back into $wp_query after the end of the first loop;

    <?php $wp_query = $temp; ?>

    http://codex.wordpress.org/Class_Reference/WP_Query

    Thread Starter Noisie

    (@noisie)

    Thanks for the fast reply alchymyth. The wp_reset_postdata(); did not work. I will look into how to restore $temp back to wp_query later since it is very late here and custom PHP is still hard to understand for me.

    This is the full relevant code I used:

    In test.php template:

    <?php get_header(); /* Template Name: test template */ ?>

    <ul class="submenu">
    <?php
      $temp = $wp_query;
      $wp_query = null;
      $wp_query = new WP_Query();
      $wp_query->query('showposts=10&post_type=test'.'&paged='.$paged); 
    
      while ($wp_query->have_posts()) : $wp_query->the_post();
    ?>
    <li>
    <?php the_title( '<a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a>' ); ?>
    </li>
    <?php endwhile; wp_reset_postdata();  ?>
    </ul>

    and after the first loop:

    <div class="contentbox main">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <h1><?php the_title(); ?></h1>
    <?php the_content(); ?>
    <?php endwhile; endif; ?>
    </div>

    In the functions file:

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
    	register_post_type( 'test',
    array(
        'labels' => array(
    'name' => __( 'test' ),
    'singular_name' => __( 'test' )
    ),
    'public' => true,
    'has_archive' => true,
    'menu_position'=>4,
    'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' )
    		)
    	);
    }

    What I did was create a page: test and connected the test template with the template function to connect the first query with custom post type tab with the normal page. I think I did something wrong with the flushing of the query. I used the query since it was the only one working with a numeric pagination on custom post types.

    Thread Starter Noisie

    (@noisie)

    Problem solved. My query was bad written what caused the problem.

    <?php wp_reset_postdata();
    $loop = new WP_Query( array( 'post_type' => 'posttypename', 'posts_per_page' => 20 ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    Content..
    
    <?php endwhile; ?>

    This code will make the loop work perfect after a normal query. Be sure you dont name the post_type the same as the page slug else it will bug out.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Problem with loop’ is closed to new replies.