0

分類法「region_country」でカスタム投稿タイプ「地域」を作成しました

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().
?>

ありがとう

4

1 に答える 1

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;

}

于 2013-07-26T13:14:08.697 に答える