WP Query taxonomy.php to display posts from current term

前提是你 提交于 2019-12-25 17:14:34

问题


I am using the following wp_query within my taxonomy.php file to try and display the list of posts from within a term from that taxonomy.

I also have 6 taxonomies that I would like this to apply to, and so I would like to make the $args 'taxonomy' => 'topic' and 'terms' => 'arabisation' dynamic, to relate to the current page the user is viewing.

Thanks

<?php get_header(); ?>
<?php get_sidebar(); ?>

<section id="hero-image">
    <div class="gradient-overlay">
        <?php 
        // vars
        $queried_object = get_queried_object(); 
        $taxonomy = $queried_object->taxonomy;
        $term_id = $queried_object->term_id;
        $image = get_field('image', $taxonomy . '_' . $term_id);

        // load image for this taxonomy term (term object)
        echo '<img src="'.$image['sizes']['large'].'" />';
        ?>
    </div>
    <div class="grid">
        <header class="unit full-width">
            <a href="<?php echo home_url(); ?>/" title="Kurdistan Memory Programme" class="logo"><?php bloginfo( 'name' ); ?></a>
        </header>
        <footer class="unit one-half">
            <h1><?php single_cat_title(); ?></h1>
            <h4 class="scroll-down">Scroll down to continue</h4>
        </footer>
    </div>
</section>

<main class="grid">  
<? if ( have_posts() ) : ?>
<? while ( have_posts() ) : the_post(); ?>
    <div class="unit col-6-12">
        <figure class="thumbnail">
            <? if ( has_post_thumbnail() ) { 
                  the_post_thumbnail();
            } ?>
            <figcaption>
                <h4><? the_title(); ?></h4>
                <h5><? the_excerpt(); ?></h5>
                <h6><a href="<? the_permalink(); ?>">View Project</a></h6>
            </figcaption>
        </figure>
    </div>
<? endwhile; ?>
<? endif; ?>   
</main>

<?php get_footer(); ?>

回答1:


Taxonomy.php files already run a query that returns the posts with that taxonomy term applied, so try this:

<?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <div class="unit col-6-12">
            <figure class="thumbnail">
                <?php if ( has_post_thumbnail() ) { 
                      the_post_thumbnail();
                } ?>
                <figcaption>
                    <h4><? the_title(); ?></h4>
                    <h5><? the_excerpt(); ?></h5>
                    <h6><a href="<? the_permalink(); ?>">View Project</a></h6>
                </figcaption>
            </figure>
        </div>
    <?php endwhile; ?>
<?php endif; ?>

The taxonomy.php file will auto-fill your loop, so you just need to use a standard wordpress loop to loop through all of your posts that match the chosen taxonomy term.



来源:https://stackoverflow.com/questions/34159550/wp-query-taxonomy-php-to-display-posts-from-current-term

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!