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

发布于 2024-09-11 20:30:45 字数 341 浏览 5 评论 0原文

我有一个taxonomy-taxonomy.php页面,需要如下所示:

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

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

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

  • 白皮书帖子 1

    白皮书帖子 2

    白皮书帖子 3

资源类型术语 2(视频)

  • 视频帖子 1

    视频发布 2

    视频帖子 3

试图理解 WordPress 3.0 的所有新文档,但这只会让我更加困惑,因为它似乎与 2.8 混淆了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灰色世界里的红玫瑰 2024-09-18 20:30:45

没有必要将对象转换为数组,您可以完美地使用该对象,而无需太多麻烦。奇怪的是(至少对我来说),你会得到这样的东西:

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

它基本上是一个对象数组,所以你必须这样对待它们。例如,如果我想要第一个的名称:

$myterms = get_terms('taxonomy-name', 'orderby=none&hide_empty');    
echo  $myterms[0]->name;

如果您需要迭代元素,您仍然可以使用 foreach();

foreach ($myterms as $term) { ?>
    <li><a href="<?php echo $term->slug; ?>"><?php echo $term->name; ?></a></li> <?php
} ?>

这样您就可以发布分类中的文章。

对于自定义帖子类型,您必须创建一个如下所示的循环:

$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;

然后您可以为每个分类/术语创建多个循环,每个循环:)。

如果您想变得更奇特(不想重复一百次),您可以在第一个循环中包含第二个循环,并将变量分配给分类法(资源即)及其具有的术语(视频)(来自你的例子只是最后一个)。这个想法是,您将拥有一个仅限于自定义帖子类型每个术语的正常(典型)wordpress 循环。

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

显然,您也可以做相反的事情,为单模板自定义类型创建正常循环(看起来您只有一个),并且内部包含所有自定义术语。

不是很优雅,但这是我能想到的最好方法:P。希望有人能理解这一点,听起来很混乱。

也许可以通过一些回调函数来实现?

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

撩发小公举 2024-09-18 20:30:45

嘿 manon1165 ,我实际上刚刚完成了这个。非常痛苦,希望我的代码片段能有所帮助!

我制作了一个自定义页面模板。并做了一些类似于现在的事情

<?php $categories = get_terms('taxonomy-name', 'orderby=name&hide_empty=0'); $cats = object_to_array($categories); ?>

,只需print_r($cats),您就会看到类别数组。

您需要将对象转换为数组,我就是这样做的。

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;
}

我做了

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

希望有帮助!

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!

画骨成沙 2024-09-18 20:30:45

这对我来说效果很好:-

<?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;
     }
}
>?

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;
     }
}
>?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文