且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Wordpress-按条款列出的自定义帖子类型的自定义分类页面

更新时间:2022-11-19 10:59:14

不必将对象转换为数组,只会让我更加困惑。 ,您可以轻松处理对象。奇怪的是(至少对我来说),是这样的:

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;

如果需要遍历元素,仍然可以使用 foreach( );

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 :).

如果您想花更多的钱(不想重复一百遍),可以在第一个循环中包含第二个循环,并将变量分配给分类法(资源即)及其包含的术语(视频)(在您的示例中,仅最后一个)。这个想法是,您将有一个普通的(典型的)wordpress循环被限制为自定义帖子类型每个条款。

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.

不太优雅,但这是我想出它的***方法:P。希望有人能理解这一点,听起来令人困惑。

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