按分类/类别wordpress获取图像发布

时间:2019-01-25 09:02:43

标签: wordpress

我有这些数据:

mysql> select  * from wp_term_taxonomy;
+------------------+---------+----------+------------------------------------------------+--------+-------+
| term_taxonomy_id | term_id | taxonomy | description                                    | parent | count |
+------------------+---------+----------+------------------------------------------------+--------+-------+
|                1 |       1 | category |                                                |      0 |     0 |
|                3 |       3 | nav_menu |                                                |      0 |     4 |
|                4 |       4 | category | Berita tentang KPU Pemko Padangsidimpuan       |      0 |     3 |
|                5 |       5 | category | Berita Nasional Tentang Pemilu                 |      0 |     3 |
|                6 |       6 | category | Pengumuman                                     |      0 |     0 |
|                7 |       7 | category | Agenda Tahunan KPU Pemkab Tapanuli Selatan     |      0 |     0 |
|                8 |       8 | category | Produk Hukum yang telah dibuat oleh KPU Tapsel |      0 |     0 |
|                9 |       9 | category |                                                |      0 |     0 |
|               10 |      10 | category | Iklan Layanan Masyarakat                       |      0 |     0 |
|               11 |      11 | category | Jadwal Kegiatan Pencalonan                     |      0 |     1 |
+------------------+---------+----------+------------------------------------------------+--------+-------+
10 rows in set (0.01 sec)

mysql> select  * from wp_terms;
+---------+-------------------------------+-------------------------------+------------+
| term_id | name                          | slug                          | term_group |
+---------+-------------------------------+-------------------------------+------------+
|       1 | Tak Berkategori               | tak-berkategori               |          0 |
|       3 | Menu Utama                    | menu-utama                    |          0 |
|       4 | Berita                        | berita                        |          0 |
|       5 | Berita Nasional               | berita-nasional               |          0 |
|       6 | Pengumuman                    | pengumuman                    |          0 |
|       7 | Agenda                        | agenda                        |          0 |
|       8 | Produk Hukum                  | produk-hukum                  |          0 |
|       9 | Partai Politik Peserta Pemilu | partai-politik-peserta-pemilu |          0 |
|      10 | Iklan Layanan Masyarakat      | iklan-layanan-masyarakat      |          0 |
|      11 | Jadwal Kegiatan Pencalonan    | jadwal-kegiatan-pencalonan    |          0 |
+---------+-------------------------------+-------------------------------+------------+
10 rows in set (0.00 sec)

I want to get the image post attachments from term_id = 11 (jadwal-kegiatan-pencalonan slug). 
My code is here:

function getImagePostByCategory( $atts, $content = null ) {
    @extract($atts);

    $args = array(
        'post_type' => 'post', 
        'post_status' => 'publish',
        'tax_query' => array(
            array (
                'taxonomy' => 'category',
                'field' => 'term_id',
                'terms' => 11
            )
        )
    );

    $query = new WP_Query( $args );

    while ( $query->have_posts() ) {
        $query->the_post();

        // now $query->post is WP_Post Object, use:
        // $query->post->ID, $query->post->post_title, etc.

        $attachments = get_posts( array(
            'post_type' => 'attachment',
            'posts_per_page' => -1,
            'post_parent' => $query->$post->ID,
            'exclude'     => get_post_thumbnail_id()
        ) );

        if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
                $class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
                $thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
                echo '<li class="' . $class . ' data-design-thumbnail">' . $thumbimg . '</li>';
            }
        }
    }

    wp_reset_postdata();
}

add_shortcode('ImagePostByCategory', 'getImagePostByCategory');

结果是所有图像帖子,包括已加载的另一个分类法/类别。 我的代码有什么问题?请帮我吗?

0 个答案:

没有答案
相关问题