按类别呼叫自定义帖子类型

时间:2018-11-27 09:32:43

标签: php wordpress custom-post-type

我正在为杂志风格的WP网站建立自定义主题。首页是静态的,我想调用10个随机文章,将其标记为“功能”作为列表,然后再调用6个类别的6个随机文章。所有帖子都是称为“主要文章”的自定义帖子类型

目前,正确调用了前10篇文章,它们是随机排序的,并按照我的意愿显示。我遇到的问题是第二组6篇文章都在调用自定义帖子类型,但按类型而不是类别

我已经在我的functions.php中注册了自定义帖子类型。

如果任何人都可以指出我的相关文章或告诉我我哪里出问题了,那就太好了。代码在下面,我可以使用帮助我的任何其他代码来编辑此帖子。

<div class="featured-header"><h1>The Guide: Must Reads</h1></div>  
                <div class="featured">

                    <?php
                    $args = array(
                    'post_type' => 'main_article',
                    'orderby' => 'rand',
                    'tag' => 'featured',
                    'posts_per_page' => 10,
                    );

                    $query = new WP_Query($args);
                    if ($query->have_posts()) :
                    while ($query->have_posts()) : $query->the_post();

                    ?>

               <div class="featured-article" >
                   <div class="featured-image">
                       <a href="<?php the_permalink(); ?>"><img src="<?php the_field('main_image'); ?>"></a>
                   </div>
                   <div class="featured-title">
                       <a href="<?php the_permalink(); ?>"><h3><?php the_field('article_title'); ?></h3></a>
                   </div>
                   <div class="featured-excerpt">
                        <?php the_excerpt(); ?>
                   </div>                      
                </div>

                    <?php endwhile; else: ?>
                    <p>Sorry, no posts with this tag!</p>
                    <?php endif; wp_reset_query(); ?>
                </div>



                <div class="lowdown-header"><h1>The Guide: Lowdown</h1></div>  
                    <div class="lowdown">
                        <div class="lowdown-wrapper">

                                <?php
                            $args = array(
                            'post_type' => 'main_article',
                            'taxonomy' => 'category',
                            'field'    => 'slug',
                            'terms'    => 'skiing-in-the-three-valleys',
                            'orderby' => 'rand',    
                            'posts_per_page' => 1,
                            );

                            $query = new WP_Query($args);
                            if ($query->have_posts()) :
                            while ($query->have_posts()) : $query->the_post();

                            ?>

                            <div class="lowdown-piste">
                                <div class="lowdown-section-header">
                                    <h2>On The Slopes</h2>
                                </div>    

                                <div class="lowdown-article" >
                                    <div class="lowdown-image-wrapper">
                                        <div class="lowdown-image">
                                            <a href="<?php the_permalink(); ?>"><img src="<?php the_field('main_image'); ?>"></a>
                                        </div>
                                    </div>    
                                    <div class="lowdown-title">
                                        <a href="<?php the_permalink(); ?>"><h3><?php the_field('article_title'); ?></h3></a>
                                    </div>
                                    <div class="lowdown-excerpt"><?php the_excerpt(); ?></div>   

                                </div>
                        </div>


                        <?php endwhile; else: ?>
                        <p>Sorry, no posts with this tag!</p>
                        <?php endif; wp_reset_query(); ?><!-- END OF ON THE PISTE SECTION  -->

0 个答案:

没有答案