doumiang2297 2013-11-07 19:00
浏览 72
已采纳

使用'new WP_Query'调出单个类别

I excluded a category in a loop (which worked great) and now I want to call out that same category in this loop below. (So that it will display at the bottom of the page.) I'm hoping a fresh pair of eyes can help tell me what's going on here. It's not showing anything. I'm using a 'new WP_Query' to bring in the single category... here it is:

<?php // Begin Recent Sold loop of 3 ?>
<?php $args = new WP_Query('cat=367'); // category 367
    while($args->have_posts()) : $args->the_post(); ?>

<div class="category">
    <h2 class="artist-name">
        <?php echo '<a href="' . get_category_link( $category->cat_ID ) . '">' . $category->name . '</a> <a class="viewall" href="' . get_category_link( $category->cat_ID ) . '">VIEW ALL</a>'; ?></h2>

    <ul class="subcats">
    <?php
        $cats = wp_list_categories('orderby=name&title_li=&use_desc_for_title=0&depth=1&echo=0&show_count=1&child_of='.$category->cat_ID);
        if (!strpos($cats,'No categories') ){
        echo $cats;
        }
    ?>
    </ul>


    <div class="row clearfix">


    <?php $query = array(
        'post_type' => 'work',
        'posts_per_page' => '3',
        'orderby' => 'post_title',
        'order' => 'ASC',
        'cat' => $category->cat_ID,
            );
        query_posts($query);
    if ( have_posts() ) while ( have_posts() ) : the_post(); 

        $rows = get_field('images');
        if(get_the_post_thumbnail()){

        echo '<div class="four column work"><a href="'.get_permalink().'">';
        echo get_the_post_thumbnail($post_id, 'medium');
        echo '</a></div>';
        } else {

            if($rows)
                {
                foreach($rows as $row)
                    {
                    echo '<div class="four column work"><a href="'.get_permalink().'">';
                    echo '<img src="'. $row['image']['sizes']['medium'] . '" class="shadowed forced" alt="'.$row['image']['alt'].'">';
                    echo '</a></div>';
                    } ?>


                <?php }
            }
            ?>

    <?php endwhile; wp_reset_query(); ?>

    </div><!-- .row -->
</div><!-- .category -->

<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>

    <?php // End Recent Sold loop ?>
  • 写回答

1条回答 默认 最新

  • dqgxazo4483 2013-11-08 14:59
    关注

    Figured it out -

    I 'excluded' the Category ID in the array of the first loop to remove it, so in the second loop, I 'included' that same Category ID. Too simple... I though it'd include that ID along with the rest, thinking it'd be redundant. Anyway, thanks for the help!

    <?php   
    $args = array(
    'include'   => '367'
    );
    
    $categories = get_categories( $args );
    $parent_categories = '';
    foreach ( $categories as $category ) { ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法