简体   繁体   English

WordPress的发布双循环

[英]wordpress posts double loops

hello thank for the helpers 你好,谢谢帮手

i want to get the latest six posts but my code isn't not working for me please help heres my code 我想获取最新的六个帖子,但是我的代码对我不起作用,请帮助我的代码

$home_query = new WP_Query( 'posts_per_page=6' );
$post_count = 0;
if ( $home_query -> have_posts() ) : while ( $home_query -> have_posts() ) : $home_query ->     the_post();?>

<?php $args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
 );

$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$alt = trim(strip_tags( get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) ));
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $image->post_content;
$default_attr = array(
'src'   => $src,
'class' => "attachment-$size",
'alt' => trim(strip_tags( get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) )),
);?>
 <article <?php post_class(); ?> id="post-<?php the_ID(); ?>">
 <div class="post-thumbnail">
 <?php if( has_post_thumbnail($post->ID)){?>
 <a href="<?php echo wp_get_attachment_url( $attachment->ID);?>" data-lightbox="<?php            $category = get_the_category(get_the_ID());
 if($category[0]){echo ($category[0]->cat_name);}?>" data-title="<?php echo $image_title;?>"><?php echo wp_get_attachment_image( $attachment->ID, array(220,300), $default_attr ); ?> </a>

and so on 等等

im traing to build boxs of posts with thumbnails each row hes 3 boxs and i wont the last six it working with category but not witeout 我训练建立每箱缩略图的帖子盒,他3盒,我不会最后六个它与类别一起工作,但不是witeout

$home_query = new WP_Query( 'category_name=front-page&&posts_per_page=6' ); //this is working //这正在工作

thank for answers 谢谢答案

hello to all i manged to resolve my problem here the working code : 您好,我想在这里解决我的问题的工作代码:

$home_query = new WP_Query('category_name=Uncategorized&&posts_per_page=6');
$post_count = 0;
if ( $home_query -> have_posts() ) : while ( $home_query -> have_posts() ) : $home_query -> the_post();?>
<?php if( !( $post_count % 3 )){ ?> <div class="posts-wrapper row"> <?php }/*end if $post_count*/ $post_count++; ?>
<article <?php post_class(); ?> id="post-<?php the_ID(); ?>"> <div class="post-thumbnail">
<?php if( !(has_post_thumbnail())){?>
/images/shop.png" data-lightbox="<?php $category = get_the_category(get_the_ID());
if($category[0]){echo ($category[0]->cat_name);}?>" data-title="NO IMAGE"><img class="attachment-250x250 wp-post-image img-responsive img-thumbnail" width="250" height="250" alt="there is no thumbnail for this post" src="<?php echo get_template_directory_uri()?>/images/shop.png">
<?php } else{ ?>
<?php $args = array('post_type' => 'attachment', 'numberposts' => 1, 'posts_per_page' => 1,
'post_status' =>'any', 'post_parent' => $post->ID );?>
<?php $attachments = get_posts($args);
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$alt = trim(strip_tags( get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) ));
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $attachment->post_content;
$default_attr = array(
'src'   => $src,
'class' => "attachment-$size",
'alt' => trim(strip_tags( get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) )),
);?>
ID;?>" href="<?php echo wp_get_attachment_url( $attachment->ID);?>" data-lightbox="<?php $category = get_the_category(get_the_ID());
if($category[0]){echo ($category[0]->cat_name);}?>" data-title="<?php echo $image_title;?>"><?php echo wp_get_attachment_image( $attachment->ID, array(220,300), $default_attr ); ?><?php
} //end foreach
} // end if ( $attachments )
} // end has_post_thumbnail?>
<p class="img-meta clearfix">
<em class="date"><?php the_time('F jS, Y'); ?>
</p>
</div><!-- end .post-thumbnail -->
<div class="post-content">
<div class="entry row">
<span class="col-xs-9">
<p class="post-title"><?php the_title(); ?></p>
<p class="post-tags">
<?php $tags = wp_get_post_tags(get_the_ID());
if($tags)
for($i=0 ; $i < count($tags) ; $i++){
if($tags[$i+1] -> name) echo $tags[$i] -> name . ' , ' ;
else echo $tags[$i] -> name;
} // end for?>
</p>
<?php if (get_option('first')){ ?>
<div id="telno">
<p>Tel: <?php echo get_option('first'); ?></p>
</div> <!-- end telno -->
<?php } //end if get_option ?>
</span><!-- col-xs-10 -->
<span class="col-xs-3">" class="btn btn-success" ><i class="content content_small"></i></span>
<style>
#recent-post-category .btn-success i {
background: url("<?php echo get_template_directory_uri()?>/images/Chevron-right.png") no-repeat scroll center center;}
</style>
</div><!-- end .entry row -->
</div><!-- end .post-content -->
</article>
<?php if( !( $post_count % 3 )){ ?> </div><!-- posts-wrapper row --><?php } // end if $post_count?>
<?php endwhile;
endif;?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM