从自定义分类中的自定义帖子类型获取所有帖子

时间:2015-07-06 作者:Zamalek Man

我试图显示具有自定义分类法的自定义帖子类型,但我没有任何运气。什么都没有出现。我感谢你的帮助。

帖子类型=横幅

自定义分类法=国家

我要显示的“国家”=阿尔及利亚

我的代码:-

<?php
$slide = new wp_query( array ( \'post_type\' => \'banner\' , \'taxonomy\' => \'country\', \'term\' => \'algeria\', 
                            \'meta_query\' => array( \'relation\' => \'AND\',
                                                                        array(\'key\' => \'page_ads\',\'value\' => \'home\'),
                                                                        array(\'key\' => \'position_ads\',\'value\' => \'top\'))
                            ));

if ( $slide->have_posts() ) : while ( $slide->have_posts() ) : $slide->the_post(); 
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'single-post-thumbnail\' ); 
?>   
<div class="item"><a href="<?php echo get_option("siteurl")?>/order-ads">    
<img src="<?php echo $image[0]; ?>" width="1350" height="515">   
                                </a>
                                </div> 
<?php
                            endwhile; endif;
                            wp_reset_query();
                            echo "</div><br>";
     }
                            ?>
注册分类法

// hook into the init action and call create_book_taxonomies when it fires
add_action( \'init\', \'create_banner_taxonomies\', 0 );

// create two taxonomies, genres and writers for the post type "book"
function create_banner_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        \'name\'              => _x( \'Country\', \'taxonomy general name\' ),
        \'singular_name\'     => _x( \'Country\', \'taxonomy singular name\' ),
        \'search_items\'      => __( \'Search Country\' ),
        \'all_items\'         => __( \'All Country\' ),
        \'parent_item\'       => __( \'Parent Country\' ),
        \'parent_item_colon\' => __( \'Parent Country:\' ),
        \'edit_item\'         => __( \'Edit Country\' ),
        \'update_item\'       => __( \'Update Country\' ),
        \'add_new_item\'      => __( \'Add New Country\' ),
        \'new_item_name\'     => __( \'New Country Name\' ),
        \'menu_name\'         => __( \'Country\' ),
    );

    $args = array(
        \'hierarchical\'      => true,
        \'labels\'            => $labels,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'country\' ),
    );

    register_taxonomy( \'country\', array( \'banner\' ), $args );
}

1 个回复
SO网友:terminator

我想你之前发布了不同的代码。但是如果这是您现在使用的代码,那么您应该这样做

$slide = new wp_query( array ( \'post_type\' => \'banner\' ,
                                \'tax_query\' =>
                                          array(
                                            array(
                                              \'taxonomy\' => \'country\', 
                                              \'field\'    => \'name\',
                                              \'terms\'    => \'algeria\'
                                            ),
                                          ),                               
                               \'meta_query\' => array( 
                                                    \'relation\' => \'AND\',                                                                        
                                                    array(\'key\' => \'page_ads\',\'value\' => \'home\'),
                                                     array(\'key\' => \'position_ads\',\'value\' => \'top\')
                                                     )
                            ));

结束

相关推荐