如何根据id显示某些类别

问题描述 投票:0回答:2

我有这样的代码:

<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(); ?>
php wordpress
2个回答
0
投票

通过以下代码,这可能会对您有所帮助

    <?php 

    $query = new WP_Query( array( 'cat' => 9 ) );

    if ( $query->have_posts() ) {

        while ( $query->have_posts() ) {

            //Your code here

        }

    }

    ?>

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(); ?>
© www.soinside.com 2019 - 2024. All rights reserved.