使用自定义分类显示自定义帖子

时间:2013-07-25 15:41:01

标签: wordpress

我使用分类“region_country”

创建了一个自定义帖子类型“region”
add_action('init', '_init_region_post_type');

function _init_region_post_type() {
    register_taxonomy(
        'region_country',
        array( 'region' ),
        array(
            'labels' => array(
                'name' => __( 'Countries' ),
                'singular_name' => __( 'Country' ),
                'search_items' => __( 'Search Countries' ),
                'popular_items' => __( 'Popular Countries' ),
                'all_items' => __( 'All Countries' ),
                'parent_item' => __( 'Parent Countrie' ),
                'parent_item_colon' => __( 'Parent Countries:' ),
                'edit_item' => __( 'Edit Countrie' ),
                'update_item' => __( 'Update Countrie' ),
                'add_new_item' => __( 'Add New Countrie' ),
                'new_item_name' => __( 'New Countrie' ),
            ),
            'public' => true,
            'show_in_nav_menus' => true,
            'show_ui' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'hierarchical' => true,
            'query_var' => true,
        )
    );  


    register_post_type( 'region',
        array(
            'capability_type' => 'post',
            'hierarchical' => false,
            'public' => true,
            'show_ui' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'menu_position' => 30,
            'labels' => array(
                'name' => __( 'Regions' ),
                'singular_name' => __( 'Region' ),
                'add_new' => __( 'Add New' ),
                'add_new_item' => __( 'Add New Region' ),
                'edit' => __( 'Edit Region' ),
                'edit_item' => __( 'Edit Region' ),
                'new_item' => __( 'New Region' ),
                'view' => __( 'View Region' ),
                'view_item' => __( 'View Region' ),
                'search_items' => __( 'Search Region' ),
                'not_found' => __( 'No regions found' ),
                'not_found_in_trash' => __('No regions found in Trash')
            ),
            'has_archive' => true,
            'supports' => array('title', 'page-attributes', 'editor', 'excerpt', 'thumbnail', 'custom-fields'),
            'query_var' => true,

        )
    );

}

在分类学“region_country”中,我创建了“china”和“laos”作为类别或标签(不知道?)

我想只显示来自分类“地区”的帖子,其中我有标签/类别“中国”

这就是我所做的:

 <?php

$type = 'region';
$args=array(
  'post_type' => $type,
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1,
  'cat' => 'China' 
  );

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post();

    if (has_post_thumbnail()) {
        echo "<a href=\""; echo the_permalink(); echo "\" title=\""; echo the_title_attribute(); echo "\" rel=\"external\" >";
            echo "<div class=\"thumbnail_image_related_project visible\">";
                echo "<div class=\"image\">";
                    echo the_post_thumbnail();
                echo "</div>";
                echo "<h2 class=\"transparent_font\">";
                    echo "<div id=\"indent_text\">";
                        echo the_title(); 
                    echo "</div>";
                echo "</h2>";
            echo "</div>";
        echo "</a><div style=\"height:50px;\"></div>";
    }
    else
    {
        echo "<a href=\""; echo the_permalink(); echo "\" title=\""; echo the_title_attribute(); echo "\" rel=\"external\" >";
            echo "<div class=\"thumbnail_image_related_project visible\">";
                echo "<div class=\"image\"><img width=\"624\" height=\"468\" src=\"http://www.cvdfk.com/images/missing_image.gif\" class=\"attachment-post-thumbnail wp-post-image\" alt=\"china-5-624x468\">";
                echo "</div>";
                echo "<h2 class=\"transparent_font\">";
                    echo "<div id=\"indent_text\">";
                        echo the_title(); 
                    echo "</div>";
                echo "</h2>";
            echo "</div>";
        echo "</a><div style=\"height:50px;\"></div>";                      
    }
    endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

由于

1 个答案:

答案 0 :(得分:0)

我认为您必须尝试使用​​此代码。

$args = array(
     'post_type' => 'region',
     'orderby' => 'rand',
     'posts_per_page' =>-1,
     'post_status' => 'publish',
     'order' => 'ASC',
     'tax_query' => array(
       'relation' => 'AND',
          array(
             'taxonomy' => 'region_country',
             'field' => 'slug',
             'terms' => 'china'
          )
     )
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();

if (has_post_thumbnail()) {
    echo "<a href=\""; echo the_permalink(); echo "\" title=\""; echo the_title_attribute(); echo "\" rel=\"external\" >";
        echo "<div class=\"thumbnail_image_related_project visible\">";
            echo "<div class=\"image\">";
                echo the_post_thumbnail();
            echo "</div>";
            echo "<h2 class=\"transparent_font\">";
                echo "<div id=\"indent_text\">";
                    echo the_title(); 
                echo "</div>";
            echo "</h2>";
        echo "</div>";
    echo "</a><div style=\"height:50px;\"></div>";
}
else
{
    echo "<a href=\""; echo the_permalink(); echo "\" title=\""; echo the_title_attribute(); echo "\" rel=\"external\" >";
        echo "<div class=\"thumbnail_image_related_project visible\">";
            echo "<div class=\"image\"><img width=\"624\" height=\"468\" src=\"http://www.cvdfk.com/images/missing_image.gif\" class=\"attachment-post-thumbnail wp-post-image\" alt=\"china-5-624x468\">";
            echo "</div>";
            echo "<h2 class=\"transparent_font\">";
                echo "<div id=\"indent_text\">";
                    echo the_title(); 
                echo "</div>";
            echo "</h2>";
        echo "</div>";
    echo "</a><div style=\"height:50px;\"></div>";                      
}
endwhile;

}