I have a taxonomy-taxonomy.php page that needs to look like so:
CUSTOM POST TYPE TITLE (RESOURCES)
Custom Taxonomy 1 (Resource Types)
Resource Type Term 1 (White Papers)
White Paper post 1
White Paper post 2
White Paper post 3
Resource Type Term 2 (Videos)
Videos post 1
Videos post 2
Videos post 3
Tried to make sense of all the new documentation for Wordpress 3.0, but it only made me more confused as it seems to be mixed up with 2.8.
It's not necessary to transform the object to an array, you can perfectly work with the object without too much hassle. What is curious (at least for me), is that you get something like this:
Array
(
[0] => stdClass Object
(
[term_id] => 7
[name] => Magister comunicaciones aplicadas
[slug] => magister-comunicaciones-aplicadas
[term_group] => 0
[term_taxonomy_id] => 7
[taxonomy] => linea-de-estudio
[description] =>
[parent] => 0
[count] => 4
)
[1] => stdClass Object
(
[term_id] => 8
[name] => Engagement marketing
[slug] => engagement-marketing
[term_group] => 0
[term_taxonomy_id] => 8
[taxonomy] => linea-de-estudio
[description] =>
[parent] => 0
[count] => 5
)
)
It's basically, an array of objects, so you've to treat them that way. For example if I want the name of the the first one:
$myterms = get_terms('taxonomy-name', 'orderby=none&hide_empty');
echo $myterms[0]->name;
If you need to iterate through the elements, you still can use foreach();
.
foreach ($myterms as $term) { ?>
<li><a href="<?php echo $term->slug; ?>"><?php echo $term->name; ?></a></li> <?php
} ?>
That way you can post the articles from your taxonomy.
For the custom post types, you'll have to create a loop like this:
$args = array(
'post_type' => 'post-type-name',
'taxonomy' => 'term'
//for example
//'resources' => 'videos'
);
// assigning variables to the loop
global $wp_query;
$wp_query = new WP_Query($args);
// starting loop
while ($wp_query->have_posts()) : $wp_query->the_post();
the_title();
blabla....
endwhile;
Then you can create multiple loops each of one for each taxonomy/term :).
If you want to get even more fancy (don't want to repeat yourself a hundred times) you can include the second loop inside the first one and assign variables to the taxonomy (resources ie) and the terms it has (videos) (from your example only the last one). The idea is that you would have a normal (typical) wordpress loop restricted to the custom post-type and each one of the terms.
foreach ($myterms as $term) : ?>
<li><a href="<?php echo $term->slug; ?>"><?php echo $term->name; ?></a></li> <?php
$term_name = $term->slug;
$args = array(
'post_type' => 'post-type-name',
'taxonomy' => "$term_name"
);
// assigning variables to the loop
global $wp_query;
$wp_query = new WP_Query($args);
// starting loop posting only
while ($wp_query->have_posts()) : $wp_query->the_post();
the_title();
blabla....
endwhile;
endforeach; ?>
Obviously you can do the inverse thing too, create the normal loop for a single-template custom type (it's looks like you have only one), and inside includes all the custom terms.
Not very elegant, but that's the best way I can came up with it :P. Hope that someone can understand this, sounds confusing.
Maybe could it be possible with some callback function?.
Hey manon1165 , I actually just accomplished this. Was a huge pain, hopefully my code snippet will help!
I made a custom page template. And did something along the lines of
<?php $categories = get_terms('taxonomy-name', 'orderby=name&hide_empty=0'); $cats = object_to_array($categories); ?>
Now, just print_r($cats)
and you will see the array of the categories.
You will need to convert the object to an array, I did so with.
function object_to_array($data)
{
if(is_array($data) || is_object($data))
{
$result = array();
foreach($data as $key => $value)
{
$result[$key] = object_to_array($value);
}
return $result;
}
return $data;
}
I did
<ul id="cat-list">
<?php foreach($cats as $cat) { ?>
<li><a href="/taxonomy-name/<?php echo $cat['slug']; ?>"><?php echo $cat['name']; ?> (<?php echo $cat['count']; ?>)</a><br><?php echo $cat['description']; ?></li>
<?php } ?>
</ul>
Hope that helps!
This worked fine for me:-
<?php
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
endwhile;
}
}
>?
来源:https://stackoverflow.com/questions/3358049/wordpress-custom-taxonomy-page-of-custom-post-type-listing-by-terms