Wordpress-按条款列出的自定义帖子类型的自定义分类页面 [英] Wordpress - Custom taxonomy page of custom post type listing by terms

查看:112
本文介绍了Wordpress-按条款列出的自定义帖子类型的自定义分类页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个taxonomy-taxonomy.php页面,看起来应该像这样:

I have a taxonomy-taxonomy.php page that needs to look like so:

自定义帖子类型标题(资源)

CUSTOM POST TYPE TITLE (RESOURCES)

自定义分类法1(资源类型)

资源类型术语1(白皮书)

Resource Type Term 1 (White Papers)


  • 白皮书后1

  • White Paper post 1

白皮书后2

白皮书后3

资源类型术语2(视频)

Resource Type Term 2 (Videos)


  • 发布视频1

  • Videos post 1

发布视频2

视频发布3

试图理解Wordpress 3.0的所有新文档,但是

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;

如果需要遍历元素,仍然可以使用 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?.

这篇关于Wordpress-按条款列出的自定义帖子类型的自定义分类页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆