2017.7.14 wordpress 通过条件检索

通过页面ID
<?php $posts = query_posts('page_id=2'); ?>
//query_posts('cat=1,2,3');根据ID显示多个分类
//query_posts('cat=-3');排除某一分类中的文章
//query_posts(array('category__and' => array(1,5)));既是分类ID1,也是分类ID5的文章
//query_posts(array('category__in' => array(1,5)));属于分类ID1或分类ID5的文章
//query_posts(array('category__not_in' => array(1,5)));不属于分类ID1,且不属于分类ID5的文章
<?php while (have_posts()) : the_post(); ?>
<?php global $post; echo $post->post_name; ?>
<?php $category = get_the_category(); echo $category[0]->slug;?>
<?php the_content(); ?>
<?php endwhile;wp_reset_query(); ?>
通过页面ID获取文章时间
<div class="notes-date">最終更新日:
<?php $id= 100;
$post_modified = get_post($id)->post_modified;
$d=strtotime($post->post_modified);echo date('Y/m/d',$d);?>
</div>
<div class="notes-date">最終更新日:
<?php $id= 100;
$post_modified = get_post($id)->post_modified;
echo $post_modified;?>
</div>
父级id并获取文章
<?php wp_reset_query();
$categories = get_the_category();
foreach($categories as $category) {
$output = $category->term_id;
}
query_posts('&cat='. $output .'&orderby=date&showposts=4'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; endif; ?>
通过分类获取文章
<?php wp_reset_query();query_posts($query_string.'&cat=10&orderby=date&showposts=3') ?>
<?php while ( have_posts() ): the_post();?>
<?php the_content(); ?>
<?php endwhile; wp_reset_query(); ?>
自定义文章类型 输出的是分类 指定分类下的子分类(page页面使用)
<?php if ( have_posts() ):?>
<?php
$term_id = 21;
$taxonomy_name = 'commodity-category';
$term_children = get_term_children( $term_id, $taxonomy_name );
if($term_children){
foreach ( $term_children as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
?>
<article>
<a href="<?php echo esc_url( get_term_link( $child, $taxonomy_name ) ); ?>" class="">
<img src="<?php
$large_image_url = z_taxonomy_image_url($child);
if(!empty($large_image_url)){if (function_exists('z_taxonomy_image_url')) echo z_taxonomy_image_url($child);}
else{
echo get_template_directory_uri().'/images/item/cov.png';
} ?>" alt="<?php bloginfo('name');
?>"/><span class="brk"></span><em><?php echo $term->name; ?></em></a>
</article>
<?php
}
}
else {echo 'The term has no children to display.';}
?>
<?php endif; wp_reset_query();?>
自定义文章类型获取当前分类的id 然后进行检索 分类下的内容标题
<?php wp_reset_query(); $term = get_queried_object()->term_id;$posts =query_posts('term='.$term.'&post_type=commodity&orderby=date&showposts=-1'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<article>
<div><img src="<?php echo get_template_directory_uri().'/images/item/cov.png';?>" alt="<?php bloginfo('name');?>"/>
<span class="brk"></span><em><?php the_title(); ?></em>
</div>
</article>
<?php endwhile; ?>
普通和自定义都可以用
<?php
$args = array(
'post_type' => 'service',
'tax_query' => array(
array(
'taxonomy' => 'service-category',
'field' => 'id',
'terms' => 4,
),
),
);
$query_news = new WP_Query($args);
if($query_news->have_posts()):
while($query_news->have_posts()):
$query_news->the_post();?>
<div class="news-container">
<div class="news-image">
<div class="news-image-container" style="background-image:url(<?php echo get_attachement(); ?>)"></div>
</div>
<div class="news-content">
<span><?php the_time('Y.n.d') ?></span><span class="news-title"><?php the_title(); ?></span>
<p><?php the_content();?></p>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
普通文章检索
<ul>
<?php
$args = array('cat' => 4,'posts_per_page' => 3);
$query_news = new WP_Query($args);
if($query_news->have_posts()):
while($query_news->have_posts()):
$query_news->the_post();?>
<li>
<a href="<?php the_permalink(); ?>">
<div class="post-thumbnail">
<div class="img_bg">
<?php $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
if(!empty($large_image_url)){ ?><img src="<?php echo $large_image_url[0]; ?>" alt="<?php bloginfo('name');
?>" />
<?php } else {?><img src="<?php bloginfo('template_url');?>/img/img_20.jpg" alt="">
<?php ;}
?>
</div>
</div>
<div class="post-excerpt">
<div class="post-title">
<?php the_title(); ?>
</div>
<div class="post-in">
<?php the_excerpt(); ?>
</div>
</div>
</a>
</li>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
</ul>
置顶文章
<?php $query_post = array(
'posts_per_page' => 3,
'post__in' => get_option('sticky_posts'),
'caller_get_posts' => 1
);
query_posts($query_post);
?>
<?php while(have_posts()):the_post(); ?>
<div class="pick-cell"><a href="<?php the_permalink(); ?>"><span class="cell-brn">MOJI<br>PICK</span><span class="cell-img"><img src="<?php
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
if(!empty($large_image_url)){echo $large_image_url[0];}
else{
echo get_template_directory_uri().'/img/img.jpg';
} ?>" alt="<?php bloginfo('name');
?>" /></span></a></div>
<?php endwhile; ?>
<?php
wp_reset_query(); ?>
排除置顶文章
<?php
$args = array('posts_per_page' => 5,'post__not_in' => get_option( 'sticky_posts' ) );
$query_news = new WP_Query($args);
if($query_news->have_posts()):
while($query_news->have_posts()):
$query_news->the_post();?>
<li>
<div class="large-row">
<div class="large_inner">
<div class="large_img"><img src="<?php
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail');
if(!empty($large_image_url)){echo $large_image_url[0];}
else{
echo get_template_directory_uri().'/img/img.jpg';
} ?>" alt="<?php bloginfo('name');
?>" /></div>
<div class="large_r">
<div class="large_meta"><span class="posted-on"><time class="entry-date published"datetime="<?php the_time('Y.n.d') ?>"><?php the_time('Y.n.d') ?></time></span></div>
<div class="large_ttl">
<?php the_title(); ?>
</div>
</div>
</div>
<div class="large_cat"><div class="cat_i"><?php
$category = get_the_category();
echo $category[0]->cat_name;
?></div></div>
</div>
<a href="<?php the_permalink(); ?>"></a></li>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
包括置顶文章的所有文章正常排序
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('ignore_sticky_posts' => 1,'paged' => $paged );
$query_news = new WP_Query($args);
if($query_news->have_posts()):
while($query_news->have_posts()):
$query_news->the_post();?>
<article>
<div class="large-row">
<div class="large_ttl">
<?php the_title(); ?>
</div>
<div class="large_meta">
<span class="posted-on"><time class="entry-date published"datetime="<?php the_time('Y.n.d') ?>"><?php the_time('Y.n.d') ?></time></span><span class="cat-links">カテゴリー:<em>ゲストハウス</em></span> </div>
<div class="large_inner">
<div class="large_img"><img src="<?php
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
if(!empty($large_image_url)){echo $large_image_url[0];}
else{
echo get_template_directory_uri().'/img/img.jpg';
} ?>" alt="<?php bloginfo('name');
?>" /></div>
<div class="large_txt">
<?php the_excerpt();?>
</div>
</div>
<div class="large_entry">続きを読む</div>
</div>
<a href="<?php the_permalink(); ?>"></a>
</article>
<?php
endwhile;
the_posts_pagination( array(
'prev_text' => __( '‹‹最初', '' ),
'next_text' => __( '最後››', '' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( '', '' ) . ' </span>',
) );
wp_reset_postdata();
endif;
?>
放在首页
<?php
$args = array(
'post_type' => 'work',
'tax_query' => array(
array(
'taxonomy' => 'work-category',
'field' => 'id',
'terms' => 16,
),
),
);
$query_news = new WP_Query( $args );
if ( $query_news->have_posts() ):
$i=1;
while ( $query_news->have_posts() ):
$query_news->the_post();
$works1 = get_post_meta( $post->ID, '_my_meta_works_key1', true );
$works2 = get_post_meta( $post->ID, '_my_meta_works_key2', true );
$works3 = get_post_meta( $post->ID, '_my_meta_works_key3', true );
$taxonomies = get_object_taxonomies( 'work', 'objects' );
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$termname = esc_html( $terms[0]->name );
}
}
?>
<article>
<div class="post-nub">
<?php echo $i;$i++; ?>
</div>
<div class="post-cat">
<?php echo $termname;?>
</div>
<div class="post-title">
<?php the_title() ;?>
</div>
</article>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
相关文章