WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Page loop with one page with different template (10 posts)

  1. Corneliatt
    Member
    Posted 1 year ago #

    Hi all!
    I'm pretty new to WordPress so maybe my question is really dumb. Well.. I give it a try anyways.

    I found a script to loop out all the pages instead of all the posts. Right now it looks like this:

    <?php $pages = get_pages('sort_column=menu_order');
    foreach ($pages as $page_data) {
        $content = apply_filters('the_content', $page_data->post_content);
        $title = $page_data->post_title;
        //echo $content;
    	?>
        <div class="section">
    			<h1><?php echo $title; ?></h1>
    			<span class="ingress"><?php the_field('ingress'); ?></span>
    			<div class="entry-content">
                 <?php echo $content; ?>
    			</div><!-- .entry-content -->
    	</div>
    <?php
    }
    ?>

    no problem at all, works perfect.

    But here's the problem: One of the pages contain blog posts from a specific category, through this script:

    <?php
    if (is_page() ) {
    $category = get_post_meta($posts[0]->ID, 'category', true);
    }
    if ($category) {
     $cat = get_cat_ID($category);
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
     $post_per_page = 10; // -1 shows all posts
     $do_not_show_stickies = 1; // 0 to show stickies
     $args=array(
      'category__in' => array($cat),
      'orderby' => 'date',
      'order' => 'ASC',
      'paged' => $paged,
      'posts_per_page' => $post_per_page,
      'ignore_sticky_posts' => $do_not_show_stickies
     );
     $temp = $wp_query; // assign orginal query to temp variable for later use
     $wp_query = null;
     $wp_query = new WP_Query($args);
     if( have_posts() ) :
    		while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
         <?php the_content(); ?>
      <?php endwhile; ?>
     <?php else : ?>
    		<h2 class="center">No posts</h2>
    		<p class="center">Sorry, but right now I don't have any posts :(</p>
    		<?php get_search_form(); ?>
    
    	<?php endif; 
    
    	$wp_query = $temp; //reset back to original query
    
    } // if ($category)
    ?>

    Now these blog posts doesn't show up in the first loop. And I can see why, because I just call the content from the page and not the content from these posts.

    So the question is: How can I add the content from the second piece of code to the first loop?

    I would be soo grateful if someone could help me out on this one.

    Thanks in advance!

  2. Corneliatt
    Member
    Posted 1 year ago #

    Hi again!
    Ok, so now I got it all to work with another script, and I just check if there's any titles on the pages that matches the categories of the blog posts. If there are a match, the stuff loops out:

    <?php
    
    	  // The Query
    	  query_posts( array ( 'category_name' => $title, 'posts_per_page' => -1 ) );
    
    	  // The Loop
    	 while ( have_posts() ) : the_post(); ?>
    	    <li>
            <?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); }?>
    	  	<h2><?php the_title(); ?></h2>
            <p><?php the_content(); ?></p>
    	  </li>
    
    	  <?php endwhile;
    
    	  // Reset Query
    	  wp_reset_query();
    	  ?>

    So far, so good. The annoying thing is that the whole page never seem to be loaded completely. The loading icon in the browser window keeps on turning and turning. Weird, because I can't see anything that's missing.

    Is there anybody who knows why it's doing like this?

    Thanks in advance!

  3. Jose Castaneda
    Member
    Posted 1 year ago #

    The one thing I can see that might be an issue is that you don't have an if statement. All you are really doing is running the loop regardless of whether or not there are posts.

  4. Corneliatt
    Member
    Posted 1 year ago #

    Ok cool! so I added an if statement to the loop.

    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	    <li>
            <?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); }?>
    	  	<h2><?php the_title(); ?></h2>
            <p><?php the_content(); ?></p>
    	  </li>
    
    	  <?php
    	  endwhile;
    	  endif;
    	  ?>

    The downside is that the neverending loading continues..
    Soo annoying, and I don't know what to do about it. Any other ideas maybe? :S

  5. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    can you paste the full code of the template into a pastebin and post the link to it here?
    see http://codex.wordpress.org/Forum_Welcome#Posting_Code

    possibly use WP_Query() instead of query_posts()

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

  6. Corneliatt
    Member
    Posted 1 year ago #

    Hi there!
    The thing is, there's not much more to show.. here's the code for loop.php:

    <?php $pages = get_pages('sort_column=menu_order');
         foreach ($pages as $page_data) {
         $content = apply_filters('the_content',
         $page_data->post_content);
         $title = $page_data->post_title;
    	?>
        <div class="section">
    			<h1><?php echo $title; ?></h1>
    			<span class="ingress"><?php the_field('ingress'); ?></span>
    			<div class="entry-content">
                 <?php echo $content; ?>
          <ul>
    	  <?php
    
    	  // The Query
    	  query_posts( array ( 'category_name' => $title, 'posts_per_page' => -1 ) );
    
    	  // The Loop
    	 if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	    <li>
            <?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); }?>
    	  	<h2><?php the_field('date'); ?>, <?php the_title(); ?></h2>
            <p><?php the_content(); ?></p>
    	  </li>
    
    	  <?php
    	  endwhile;
    	  endif;
    	  ?>
    </ul>
    
    </div><!-- .entry-content -->
    </div>
    <?php
    }
    ?>

    Btw. Just realized I have to do something about the the_field "ingress" because it isn't going through the page_data as the title and content. How do I manage to do this? Could this be part of the problem?

  7. Jose Castaneda
    Member
    Posted 1 year ago #

    I have to go with alchymyth on this one. I would use the WP_Query instead.

    // Create a new instance of the query
    	$my_query = new WP_Query ( 'post_type=page' );
    		// The Loop
    		while ( $my_query->have_posts() ) : $my_query->the_post();?>
    			<h3><?php the_title(); ?></h3>
    		<?php endwhile;
    
    		// Reset Post Data
    		wp_reset_postdata();
  8. Corneliatt
    Member
    Posted 1 year ago #

    Ok! So now I'm using the wp_query query instead and the pages shows up just as before. But to be honest I've been reading through the page alchymyth sent me without being able to understand how the heck I can implement the second loop into it. And also, how I can manage to sort the output :S Would be awesome if you could try to explain in a newbie kind of way :)

    Thanks!

  9. Corneliatt
    Member
    Posted 1 year ago #

    Hi again!
    Now everything is how it should be, except for one "small" thing.
    I want to sort the blog posts after a custom date picker field. I found this script which should do exactly what I want.

    In functions:

    // CREATE UNIX TIME STAMP FROM DATE PICKER
    function custom_unixtimesamp ( $post_id ) {
        if ( get_post_type( $post_id ) == 'events' ) {
    	$startdate = get_post_meta($post_id, 'startdate', true);
    
    		if($startdate) {
    			$dateparts = explode('-', $startdate);
    			$newdate1 = strtotime(date('Ymd', strtotime($dateparts[1].'/'.$dateparts[2].'/'.$dateparts[0])));
    			update_post_meta($post_id, 'unixstartdate', $newdate1  );
    		}
    	}
    }
    add_action( 'save_post', 'custom_unixtimesamp', 100, 2);

    In the loop:

    <?php
    	 $title = get_the_title();
    
    	 if ($title == 'Upcoming') {  
    
    	 $today = time();
    
             $args = array(
    	'category_name' => 'events',
    	'posts_per_page' => 5,
    
    	'meta_query' => array(
    		array(
    			'key' => 'unixstartdate',
    			'compare' => '>=',
    			'value' => $today,
    		)
    	),
    
    	'meta_key' => 'startdate',
    	'orderby' => 'meta_value',
    	'order' => 'ASC',
    );
    
    $post_query = new WP_Query( $args );
    
    	  // The Loop
    	 if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
         <li>
         <h2>
    	  	<span class="ingress"><?php echo date('d F', custom_unixtimestamp($startdate )); ?></span><br />
    		<?php the_title(); ?></h2>
            <?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); }?>
            <p><?php the_content(); ?></p>
    	  </li>
    
    	  <?php
    	  endwhile;
    	  endif;
    	  }
    	  ?>

    But I can't get it to work.
    First of all, the script doesn't seem to catch the dates (1 january 1970 on all the posts) and well.. I guess that's also why it doesn't sort it correctly. Could anyone please help me out on this one? :)

  10. Corneliatt
    Member
    Posted 1 year ago #

    Ok, thanks for your help. I've solved it!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.