我有这样的代码:
<div class="featured-title"><h3>Paket Lainnya</h3></div>
<div class="boxer2">
<?php $this_post = $post;
$category = get_the_category();
$category = $category[0];
$category = $category->cat_ID;
$posts = get_posts('numberposts=7&offset=0&orderby=rand&order=DESC&category='.$category);
$count = 0;
foreach ($posts as $post) {
if ($post->ID == $this_post->ID || $count == 6) {
unset($posts[$count]);
} else {
$count++;
}
}
?>
<?php if ( $posts ) : ?>
<?php foreach ( $posts as $post ) : ?>
<div class="col-md-4"><?php get_template_part('thumb'); ?></div>
<?php endforeach ?>
<?php endif ?>
<?php wp_reset_query(); ?>
</div>
我期待什么?
我想通过输入category id
更改代码以显示特定类别的结果。
我已设置类别ID 9
。
完整代码:
<?php get_header(); ?>
<?php
if ((is_home())&& ($paged < 1)) {
get_template_part('home-featured');
}
?>
<div class="wisata-konten">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php
$query = new WP_Query(array('cat' => 9));
if ($query->have_posts()) {
while ($query->have_posts()) {
<div class="featured-title"><h3>Paket Lainnya</h3></div>
<div class="boxer2">
<?php
$this_post = $post;
$category = get_the_category(); $category = $category[0]; $category = $category->cat_ID;
$posts = get_posts('numberposts=7&offset=0&orderby=rand&order=DESC&category='.$category);
$count = 0;
foreach ($posts as $post) {
if ($post->ID == $this_post->ID || $count == 6) {
unset($posts[$count]);
} else {
$count++;
}
}
?>
<?php if ( $posts ) : ?>
<?php foreach ( $posts as $post ) : ?>
<div class="col-md-4"><?php get_template_part('thumb'); ?></div>
<?php endforeach ?>
<?php endif ?>
<?php wp_reset_query(); ?>
</div>
</div>
</div>
</div>
}
}
?>
<div class="wisata-testimoni">
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="testimoni" class="carousel slide">
<div class="carousel-inner">
<?php fastestwp_comments(); ?>
</div>
</div>
<div class="tombol"><a class="medium beli blue pull-right" href="<?php echo home_url(); ?>/testimoni">Lihat Semua Testimoni <span class="glyphicon glyphicon-thumbs-up"></span></a></div>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
&#13;
答案 0 :(得分:0)
通过以下代码,这可能会对您有所帮助
<?php
$query = new WP_Query( array( 'cat' => 9 ) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
//Your code here
}
}
?>
答案 1 :(得分:0)
我已清理您的代码检查
<?php get_header(); ?>
<?php if((is_home())&& ($paged < 1)) { ?>
<?php get_template_part( 'home-featured' ); ?>
<?php } ?>
<div class="wisata-konten">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php
$query = new WP_Query( array( 'cat' => 9 ) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) { ?>
<div class="featured-title"><h3>Paket Lainnya</h3></div>
<div class="boxer2">
<?php $this_post = $post;
$category = get_the_category(); $category = $category[0]; $category = $category->cat_ID;
$posts = get_posts('numberposts=7&offset=0&orderby=rand&order=DESC&category='.$category);
$count = 0;
foreach ( $posts as $post ) {
if ( $post->ID == $this_post->ID || $count == 6) {
unset($posts[$count]);
}else{
$count ++;
}
}
?>
<?php if ( $posts ) : ?>
<?php foreach ( $posts as $post ) : ?>
<div class="col-md-4"><?php get_template_part( 'thumb' ); ?></div>
<?php endforeach; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
</div>
</div>
<?php }
}
?>
<div class="wisata-testimoni">
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="testimoni" class="carousel slide">
<div class="carousel-inner">
<?php fastestwp_comments(); ?>
</div>
</div>
<div class="tombol"><a class= "medium beli blue pull-right" href="<?php echo home_url() ; ?>/testimoni" >Lihat Semua Testimoni <span class="glyphicon glyphicon-thumbs-up"></span></a></div>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>