如何在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
            }
        }

来源:

https://www.srcmini02.com/63472.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_41825.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?