Tag Archives: post_by_category

Taxonomy and Tags for Custom post type

Tags and Taxonomy both are using register_taxonomy() function.

if you want to display Taxonomy so ‘hierarchical’      => true,

if you want to display Tags so ‘hierarchical’      => false,


/***** Adding custom post type, Texonomys, Tags - Our Team *****/
function ourteams_custom_init() {
$labels = array(
'name' => 'Our Team',
'singular_name' => 'ourteams',
'add_new' => 'Add New',
'add_new_item' => 'Add New Our Team',
'edit_item' => 'Edit Our Team',
'new_item' => 'New Our Team',
'all_items' => 'All Our Team',
'view_item' => 'View Our Team',
'search_items' => 'Search Our Team',
'not_found' => 'No plans found',
'not_found_in_trash' => 'No plans found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Our Team'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'ourteams' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title' ,'thumbnail' , 'editor' ),

'taxonomies' => array( 'members')
);

register_post_type( 'ourteams', $args );

// team Tags
$members_labels = array(
'name'              => _x( 'Members', 'taxonomy general name' ),
'singular_name'     => _x( 'Members', 'taxonomy singular name' ),
'search_items'      => __( 'Search Members' ),
'all_items'         => __( 'All Members' ),
'parent_item'       => __( 'Parent Members' ),
'parent_item_colon' => __( 'Parent Members:' ),
'edit_item'         => __( 'Edit Members' ),
'update_item'       => __( 'Update Members' ),
'add_new_item'      => __( 'Add New Members' ),
'new_item_name'     => __( 'New Members' ),
'menu_name'         => __( 'Members' ),
);

$members_labels_args = array(
'hierarchical'      => true,
'labels'            => $members_labels,
'show_ui'           => true,
'show_admin_column' => true,
'query_var'         => true,
'show_in_nav_menus' => true,
'show_tagcloud'     => true,
'rewrite'           => array( 'slug' => 'members' ),
);
register_taxonomy( 'members', array( 'ourteams' ), $members_labels_args );
}
add_action( 'init', 'ourteams_custom_init' );

Display Post by Category

/* Show posts associated with certain categories.
cat (int) – use category id.
category_name (string) – use category slug (NOT name).
category__and (array) – use category id.
category__in (array) – use category id.
category__not_in (array) – use category id. */

// If You are Using Custom post type so you have to use ‘tax_query’


$args_ourteams=array(
'post_type' => 'ourteams',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'members',
//'field' => 'term_id',
//'terms' => 10,
'field' => 'slug',
'terms' => 'attorneys'
)));

$loop_ourteams = new WP_Query($args_ourteams);

while ($loop_ourteams->have_posts()) : $loop_ourteams->the_post();

the_title();

endwhile;

Advertisements

Display Posts by Category


$lattest_args7 = array(
'post_type' => 'post',
'posts_per_page' => 5,
'cat' => 6
);
$lattest_loop7 = new WP_Query($lattest_args7);

while($lattest_loop7->have_posts()):$lattest_loop7->the_post();
echo get_the_title();

endwhile;

%d bloggers like this: