How can I list all posts from a custom taxonomy category into a custom template? Below is the code.
So I`ve created a custom taxonomy like this:
register_taxonomy('miniblog_category', 'post', array(
// Hierarchical taxonomy (like categories)
'hierarchical' => true,
// This array of options controls the labels displayed in the WordPress Admin UI
'labels' => array(
'name' => _x( 'MiniBlog Categories', 'taxonomy general name' ),
'singular_name' => _x( 'MiniBlog Category', 'taxonomy singular name' ),
'search_items' => __( 'Search MiniBlog Categories' ),
'all_items' => __( 'All MiniBlog Categories' ),
'parent_item' => __( 'Parent MiniBlog Category' ),
'parent_item_colon' => __( 'Parent MiniBlog Category:' ),
'edit_item' => __( 'Edit MiniBlog Category' ),
'update_item' => __( 'Update MiniBlog Category' ),
'add_new_item' => __( 'Add New MiniBlog Category' ),
'new_item_name' => __( 'New MiniBlog Category Name' ),
'menu_name' => __( 'MiniBlog Categories' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'miniblog',
'with_front' => false,
),
));
After that I also created a custom widgets that is displayed on the author`s page (right sidebar). This widget displays all current author's categories with the post numbers (the code below):
...
$current_author_id = get_the_author_meta('ID');
$categories_by_post_no = array();
foreach (get_terms('miniblog_category') as $mbc) {
// metadata term( blogger_id )
$taxonomy_blogger_id = get_term_meta($mbc->term_id, 'blogger_id', true) ;
if ($taxonomy_blogger_id == $current_author_id) {
$arg_author_posts = array(
'post_type' => 'post',
'post_status' => 'published',
'miniblog_category' => $mbc->slug,
'numberposts' => -1,
);
$term_link = get_term_link($mbc->term_id, $mbc->slug);
$categories_by_post_no[] = array(
'name' => $mbc->name,
'post_count' => count( get_posts( $arg_author_posts ) ),
'term_link' => get_term_link($mbc),
);
}
}
if (! empty($categories_by_post_no) ) {
echo '<ul class="menu">';
foreach ($categories_by_post_no as $cbpn) {
echo "<li><a href='" . $cbpn['term_link'] . "'>$cbpn[name] ($cbpn[post_count])<a/></li>";
};
echo'</ul>';
} else {
echo '<span>No articles!</span>';
}
...
In my case the links ($cbpn['term_link']) are like this:
- miniblog/culture
- miniblog/nature
- miniblog/south-america
...
but when I click on those links I received a 404 not found error.
I tried to create in the theme folder files like this (taxonomy-miniblog.php, taxonomy-miniblog-category.php) but it doesn't work. Do I have to create files for each category, e.g. taxonomy-miniblog-culture.php, taxonomy-miniblog-nature.php ...(no way!!)?
Related
I am trying to get posts related to custom taxonomy.
I have a new CPT called article.
I have two custom taxonomies related to the CPT:tags and cats
I can see the cpt items in a custom archive page called archive-article.php.
the problem is this, I created two new files: taxonomy-cats.php and 'taxonomy-tags.php' the content of this files are exacley as the archive-article.php. file.
I get a massage that nothing was found. I checked and I know that there are custom posts related to this taxonomy value.
This is the url I receive the empty archive:
'http://localhost/broker/cats/sales/'
What can be the problem?
Thank you!
This is the taxonomy code:
function create_cats_taxonomy() {
$labels = array(
'name' => _x( 'cats', 'taxonomy general name' ),
'singular_name' => _x( 'cat', 'taxonomy singular name' ),
'search_items' => __( 'search items' ),
'all_items' => __( 'all items' ),
'parent_item' => __( 'parent item' ),
'parent_item_colon' => __( 'parent item colon:' ),
'edit_item' => __( 'edit item' ),
'update_item' => __( 'update item' ),
'add_new_item' => __( 'add new item' ),
'new_item_name' => __( 'new cat' ),
'menu_name' => __( 'cat'),
);
// Now register the taxonomy
register_taxonomy('cats',array('article'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'cats' ),
));
}
add_action( 'init', 'create_cats_taxonomy' );
I have created a custom posts with the following code:
// Our custom post type function
function create_posttype() {
register_post_type( 'tours',
// CPT Options
array(
'labels' => array(
'name' => __( 'Tours' ),
'singular_name' => __( 'Tour' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'tours'),
'hierarchical' => true
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
function my_taxonomies_tours() {
$labels = array(
'name' => _x( 'Tour Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Tour Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Tour Categories' ),
'all_items' => __( 'All Tour Categories' ),
'parent_item' => __( 'Parent Tour Category' ),
'parent_item_colon' => __( 'Parent Tour Category:' ),
'edit_item' => __( 'Edit Tour Category' ),
'update_item' => __( 'Update Tour Category' ),
'add_new_item' => __( 'Add New Tour Category' ),
'new_item_name' => __( 'New Tour Category' ),
'menu_name' => __( 'Tour Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
//'rewrite' => array('slug' => 'tours')
);
//register_taxonomy( 'tour_category', 'tours', $args );
register_taxonomy( 'tour_category', 'tours', $args );
}
add_action( 'init', 'my_taxonomies_tours', 0 );
In my permalink settings I have setup:
http://mydomainname.com.au/%category%/%postname%/
I want to display the URLs like so:
display posts in categories
http://mydomainname.com.au/tours/gbr/
http://mydomainname.com.au/tours/whitehaven-tours/
http://mydomainname.com.au/tours/day-tours/
Display posts
http://mydomainname.com.au/tours/gbr/name-of-post
http://mydomainname.com.au/tours/whitehaven-tours/name-of-post
http://mydomainname.com.au/tours/day-tours/name-of-post
But I'm currently getting the following URLs:
display posts in categories
http://mydomainname.com.au/tour_category/gbr/
http://mydomainname.com.au/tour_category/whitehaven-tours/
http://mydomainname.com.au/tour_category/day-tours/
Display posts
http://mydomainname.com.au/tours/name-of-post
http://mydomainname.com.au/tours/name-of-post
http://mydomainname.com.au/tours/name-of-post
I am not familiar enough with URL rewrites to make the necessary changes. I have read many articles but getting nowhere fast. Any help in the right direction would be greatly appreciated.
I'm feeling close to finishing this. In short, I can't seem to display posts.
For example I have:
http://somedomain.org/tours //Displays all categories
http://somedomain.org/tours/gbr //Displays all posts in category
http://somedomain.org/tours/gbr/my-post //Should display post in category
http://somedomain.org/tours/gbr/my-post doesn't display, I get a there's nothing here, 404 error. But if I go to:
http://somedomain.org/toursgbr/my-post
It does in fact, display the post ("toursgbr" has no forward slash if you're looking for the difference)
I am using the latest WordPress. I am using the plugin:
Custom Post Type Permalinks
I have the following code in the functions.php file:
// Our custom post type function
function create_posttype() {
register_post_type( 'tours',
// CPT Options
array(
'labels' => array(
'name' => __( 'Tours' ),
'singular_name' => __( 'Tour' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'tours', 'with_front' => TRUE),
'cptp_permalink_structure' => '%tour_category%/%postname%',
'hierarchical' => true //A added
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
function my_taxonomies_tours() {
$labels = array(
'name' => _x( 'Tour Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Tour Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Tour Categories' ),
'all_items' => __( 'All Tour Categories' ),
'parent_item' => __( 'Parent Tour Category' ),
'parent_item_colon' => __( 'Parent Tour Category:' ),
'edit_item' => __( 'Edit Tour Category' ),
'update_item' => __( 'Update Tour Category' ),
'add_new_item' => __( 'Add New Tour Category' ),
'new_item_name' => __( 'New Tour Category' ),
'menu_name' => __( 'Tour Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'rewrite' => array('slug' => 'tours')
);
register_taxonomy( 'tour_category', 'tours', $args );
}
add_action( 'init', 'my_taxonomies_tours', 0 );
The line of code:
'cptp_permalink_structure' => '%tour_category%/%postname%',
If I add the forward slash, so it's:
'cptp_permalink_structure' => '/%tour_category%/%postname%',
That's when I can't view posts. Any help in the right direction would be fantastic. I do have other categories other than gbr and posts can have multiple categories. I thought that might be important information
I'm learning php & wordpress but this escapes from my knowledge. I have a portfolio theme that I want to separate for SEO pourposes.
I have this. (productos is the current portfolio slug)
website.com/productos/portfoliopost1 (category-a slug)
website.com/productos/portfoliopost2 (category-b slug)
But this is that I want to show:
website.com/category-a/portfoliopost1 (category-a slug)
website.com/category-b/portfoliopost2 (category-b slug)
It's possible? Thanks for all.
This is the current portfolio.php that generate the slug.
if ( !function_exists( 'pexeto_register_portfolio_post_type' ) ) {
/**
* Registers the portfolio custom type.
*/
function pexeto_register_portfolio_post_type() {
//the labels that will be used for the portfolio items
$labels = array(
'name' => _x( 'Portfolio', 'portfolio name', 'pexeto_admin' ),
'singular_name' => _x( 'Portfolio Item', 'portfolio type singular name', 'pexeto_admin' ),
'add_new' => _x( 'Add New', 'portfolio', 'pexeto_admin' ),
'add_new_item' => __( 'Add New Item', 'pexeto_admin' ),
'edit_item' => __( 'Edit Item', 'pexeto_admin' ),
'new_item' => __( 'New Portfolio Item', 'pexeto_admin' ),
'view_item' => __( 'View Item', 'pexeto_admin' ),
'search_items' => __( 'Search Portfolio Items', 'pexeto_admin' ),
'not_found' => __( 'No portfolio items found', 'pexeto_admin' ),
'not_found_in_trash' => __( 'No portfolio items found in Trash', 'pexeto_admin' ),
'parent_item_colon' => ''
);
//register the custom post type
register_post_type( PEXETO_PORTFOLIO_POST_TYPE,
array( 'labels' => $labels,
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array( 'slug'=> 'productos', 'with_front' => false ),
'taxonomies' => array( PEXETO_PORTFOLIO_TAXONOMY ),
'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'page-attributes' ) ) );
}}
You can use post_type_link hook for this.
Here you can find explanation:
WordPress Exchange
WordPress Exchange2
I have a custom post type Event. I also have custom meta boxes (e.g. a dropdown list for the speaker of that event). However, the list of speakers are hard coded by me, and the client wants to be able to add, edit, and remove the speakers from the admin part.
How do I do this?
$items = get_posts( array (
'post_type' => YOUR_POST_TYPE,
'posts_per_page' => -1,
'post_status' => 'publish'
));
<select name="get-posts" id="get-posts">
<option value="">Choose A Page</option>
<?php
foreach($items as $item) {
echo '<option value="'.$item->ID.'"',$meta == $item->ID ? ' selected="selected"' : '','>'.$item->post_title.'</option>';
} // end foreach ?>
</select>
you can do it by custom metabox, here is the code form codex
register_taxonomy(
'speakers',
array( 'YOUR_POST_TYPE' ),
array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Speakers', 'taxonomy general name' ),
'singular_name' => _x( 'Speaker', 'taxonomy singular name' ),
'search_items' => __( 'Search Speakers' ),
'all_items' => __( 'All Speakers' ),
'parent_item' => __( 'Parent Speakers' ),
'parent_item_colon' => __( 'Parent Speakers:' ),
'edit_item' => __( 'Edit Speakers' ),
'update_item' => __( 'Update Speakers' ),
'add_new_item' => __( 'Add New Speakers' ),
'new_item_name' => __( 'New SpeakersName' ),
'menu_name' => __( 'Speakers' ),
),
'rewrite' => true
)
);