个性化阅读
专注于IT技术分析

如何在WordPress中根据类别(分类)检索帖子(书)?

我正在尝试显示特定类别(分类)为” Book1″的帖子。

我试图用以下代码显示它。

                    $args = array(
                        'post_type' => 'book', 'posts_per_page' => 6, 'tax_query' => array(
                            array(
                                'taxonomy' => 'Book1', 'field' => 'id', 'terms' => 1
                            )
                        )
                    );
                    echo '<br>';
                    $postss = get_posts( $args );

                    if ( ! empty( $postss ) && is_array( $postss ) ) {
                        // Run a loop and print them all
                        $i=1;
                        foreach ( $postss as $termm ) { 
                                echo ' '.$i.' '.$termm->post_title. '<br>';
                                $i++;
                        }
                    }
               ?>

在输出中, 不显示任何项目。


#1


$custom_terms = get_terms('Book1');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array(
        'post_type' => 'book', 'posts_per_page' => 6, 'tax_query' => array(
            array(
                'taxonomy' => 'Book1', 'field' => 'slug', 'terms' => $custom_term->slug, ), ), );

    $loop = new WP_Query($args);
    if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
        endwhile;
    }
}

试试这个代码


#2


$args = array(
        'post_type' => 'book', 'posts_per_page' => 6, 'tax_query' => array(
            array(
                'taxonomy' => 'Book1', 'field' => ''term_id', // here you are worng name too
                'terms' => 1
            )
        )
    );
    echo '<br>';
    $postss = get_posts( $args );

    if ( ! empty( $postss ) && is_array( $postss ) ) {
        // Run a loop and print them all
        $i=1;
        foreach ( $postss as $termm ) { 
                echo ' '.$i.' '.$termm->post_title. '<br>';
                $i++;
        }
    }

?>

//最佳解决方案

<?php

$query = new WP_Query( array(
    'post_type' => 'book', // name of post type.
    'tax_query' => array(
        array(
            'taxonomy' => 'Book1', // taxonomy name
            'field' => 'term_id', // term_id, slug or name
            'terms' => 1, // term id, term slug or term name
        )
    )
) );

while ( $query->have_posts() ) : $query->the_post();
    // do stuff here....
endwhile;

/**
 * reset the orignal query
 * we should use this to reset wp_query
 */
wp_reset_query();
?>

#3


我是这样做的。谢谢大家

        $args = array(
            'post_type' => 'book', 'cat' => '35'
        );
        echo '<br>';
        $postss = query_posts($args);
        if ( ! empty( $postss ) && is_array( $postss ) ) {
            // Run a loop and print them all
            ?><?php $i=1;
            foreach ( $postss as $termm ) { ?>

                    <?php echo ' '.$i.' '.$termm->post_title. '<br>';$i++;?>
                <?php
            }
        }
赞(0)
未经允许不得转载:srcmini » 如何在WordPress中根据类别(分类)检索帖子(书)?

评论 抢沙发

评论前必须登录!