Wordpress - Custom taxonomy page of custom post type listing by terms

五迷三道 提交于 2019-12-04 03:56:34

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;
     }
}
>?
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!