.= 'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>wp query - Carousel Loop only duplicating
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Carousel Loop only duplicating

programmeradmin1浏览0评论

I have problem with looping and WP_Query. Any idea how i can implement the multiple carouselitems please check what html output should be in html

So basically one of my goal is each div have a pages

<div class="col-sm-6 col-md-3"></div>

should have get all pages from parents which is "Destinations".

So i have an class CarouselItem active. (Actually this is only a slider style for changing the contents purposes).

And here the previous what html code should be in wordpress i mean the output in html format.

<section class="carouselItem active">
      <div class="col-sm-6 col-md-3">
        <span>
          <div class="col-xs-4 col-sm-12">
          <-- thumbnail -->
          </div>
          <div class="col-xs-8 col-sm-12">
          <-- content -->
          </div>
        </span>
      </div>
      <div class="col-sm-6 col-md-3">
        <span>
          <div class="col-xs-4 col-sm-12">
          <-- thumbnail -->
          </div>
          <div class="col-xs-8 col-sm-12">
          <-- content -->
          </div>
        </span>
      </div>
      <div class="col-sm-6 col-md-3">
        <span>
          <div class="col-xs-4 col-sm-12">
          <-- thumbnail -->
          </div>
          <div class="col-xs-8 col-sm-12">
          <-- content -->
          </div>
        </span>
      </div>
      <div class="col-sm-6 col-md-3">
        <span>
          <div class="col-xs-4 col-sm-12">
          <-- thumbnail -->
          </div>
          <div class="col-xs-8 col-sm-12">
          <-- content -->
          </div>
        </span>
      </div>
</section>

<section class="carouselItem"></section>
<section class="carouselItem"></section>

And here what i do with wordpress. Yes i'm a little bit new with wordpress.

Here what i do i use post_per_page to 4, post_type to pages since i want to query all pages under destinations. And post_parent to a variable of $destination with ->ID

 <?php 
  $destination = get_page_by_title( 'Destinations');
  // WP_Query arguments
  $args = array (
    'post_parent'            => $destination->ID,
    'post_type'              => 'page',
    'posts_per_page'         => '4',
  );

  // The Query
  $query = new WP_Query( $args );
?>  
<section class="carouselItem active">
    <?php 
        if($query-> have_posts()): while($query->have_posts()): $query->the_post(); 
        $location = get_post_meta($post->ID, 'Location', $single = true);
    ?>  
     <div class="col-sm-6 col-md-3">
        <span>
          <div class="col-xs-4 col-sm-12">
            <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
              <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                <?php the_post_thumbnail('homepage'); // Declare pixel size you need inside the array ?>
              </a>
            <?php else :?>
                <img src="<?php echo get_template_directory_uri(); ?>/assets/images/others/placeholder2.jpg" alt="">
            <?php endif; ?>
          </div>
          <div class="col-xs-8 col-sm-12">
            <h5><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h5>
            <small><?php echo $location; ?> </small>
            <p><?php the_excerpt(); ?></p>
          </div>
        </span>
      </div>  
    <?php endwhile; endif; ?>   
</section>

Result on me is are they only loop the content and the carouselItem is not duplicating for another post

Thank you for help. i'm very new with WP_Query.

Update after the update for diego: The carousel is now working but the pages seems only same. like this one

slider 1 = A A A A slider 2 = B B B B slider 3 = C C C C slider 4 = D D D D

instead of slider 1 = A B C D slider 2 = E F G H slider 3 = I J K L

I have problem with looping and WP_Query. Any idea how i can implement the multiple carouselitems please check what html output should be in html

So basically one of my goal is each div have a pages

<div class="col-sm-6 col-md-3"></div>

should have get all pages from parents which is "Destinations".

So i have an class CarouselItem active. (Actually this is only a slider style for changing the contents purposes).

And here the previous what html code should be in wordpress i mean the output in html format.

<section class="carouselItem active">
      <div class="col-sm-6 col-md-3">
        <span>
          <div class="col-xs-4 col-sm-12">
          <-- thumbnail -->
          </div>
          <div class="col-xs-8 col-sm-12">
          <-- content -->
          </div>
        </span>
      </div>
      <div class="col-sm-6 col-md-3">
        <span>
          <div class="col-xs-4 col-sm-12">
          <-- thumbnail -->
          </div>
          <div class="col-xs-8 col-sm-12">
          <-- content -->
          </div>
        </span>
      </div>
      <div class="col-sm-6 col-md-3">
        <span>
          <div class="col-xs-4 col-sm-12">
          <-- thumbnail -->
          </div>
          <div class="col-xs-8 col-sm-12">
          <-- content -->
          </div>
        </span>
      </div>
      <div class="col-sm-6 col-md-3">
        <span>
          <div class="col-xs-4 col-sm-12">
          <-- thumbnail -->
          </div>
          <div class="col-xs-8 col-sm-12">
          <-- content -->
          </div>
        </span>
      </div>
</section>

<section class="carouselItem"></section>
<section class="carouselItem"></section>

And here what i do with wordpress. Yes i'm a little bit new with wordpress.

Here what i do i use post_per_page to 4, post_type to pages since i want to query all pages under destinations. And post_parent to a variable of $destination with ->ID

 <?php 
  $destination = get_page_by_title( 'Destinations');
  // WP_Query arguments
  $args = array (
    'post_parent'            => $destination->ID,
    'post_type'              => 'page',
    'posts_per_page'         => '4',
  );

  // The Query
  $query = new WP_Query( $args );
?>  
<section class="carouselItem active">
    <?php 
        if($query-> have_posts()): while($query->have_posts()): $query->the_post(); 
        $location = get_post_meta($post->ID, 'Location', $single = true);
    ?>  
     <div class="col-sm-6 col-md-3">
        <span>
          <div class="col-xs-4 col-sm-12">
            <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
              <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                <?php the_post_thumbnail('homepage'); // Declare pixel size you need inside the array ?>
              </a>
            <?php else :?>
                <img src="<?php echo get_template_directory_uri(); ?>/assets/images/others/placeholder2.jpg" alt="">
            <?php endif; ?>
          </div>
          <div class="col-xs-8 col-sm-12">
            <h5><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h5>
            <small><?php echo $location; ?> </small>
            <p><?php the_excerpt(); ?></p>
          </div>
        </span>
      </div>  
    <?php endwhile; endif; ?>   
</section>

Result on me is are they only loop the content and the carouselItem is not duplicating for another post

Thank you for help. i'm very new with WP_Query.

Update after the update for diego: The carousel is now working but the pages seems only same. like this one

slider 1 = A A A A slider 2 = B B B B slider 3 = C C C C slider 4 = D D D D

instead of slider 1 = A B C D slider 2 = E F G H slider 3 = I J K L

Share Improve this question edited Sep 25, 2014 at 11:45 Eric Luciano asked Sep 25, 2014 at 1:40 Eric LucianoEric Luciano 14 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

You should reset the query after your active slider. So place this code after your first loop (after endwhile; endif; ):

//Reset Query
wp_reset_postdata();

Also try setting the paged atribute:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
// WP_Query arguments
$args = array (
    'post_parent'            => $destination->ID,
    'post_type'              => 'page',
    'posts_per_page'         => '4',
    'paged'                  => $paged,
);

Hope that helps,
Cheers

What about trying a different approach using get_pages() ?

  $destination = get_page_by_title( 'Destinations');
  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  $num_pages_per_page = 4;

  $args = array(
    'sort_order' => 'ASC',
    'child_of' => $destination->ID,
    'number' => $num_pages_per_page,
    'offset' => ( $paged - 1 ) * $num_pages_per_page,
    'post_type' => 'page',
    'post_status' => 'publish'
    ); 
    $pages = get_pages($args); 

This will get an array with your pages... On page 2 you will get 5,6,7,8 and so on...

Hope that solves your problem,

before the slider

//<?php $i = 0; ?>
<section class="carouselItem <?php if($i == 0 ) : echo "active"; ?>">

//start loop here

<section class="carouselItem <?php if($i == 0 ) : echo "active"; ?>">

 </section>
 //$i++; end loop

Hope this will solve your problem

发布评论

评论列表(0)

  1. 暂无评论