![](/img/trans.png)
[英]Wordpress > Custom post type with Custom taxonomy > terms not linking to custom archive page
[英]Wordpress - Custom taxonomy page of custom post type listing by terms
我有一个taxonomy-taxonomy.php页面,需要如下所示:
自定义帖子类型标题(资源)
自定义分类1(资源类型)
资源类型第1期(白皮书)
白皮书帖子1
白皮书帖子2
白皮书帖子3
资源类型第2期(视频)
视频发布1
视频帖子2
视频发布3
试图理解Wordpress 3.0的所有新文档,但它只是让我更加困惑,因为它似乎与2.8混在一起。
没有必要将对象转换为数组,您可以完美地处理对象而不会有太多麻烦。 什么是好奇(至少对我来说),是你得到这样的东西:
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循环限制在自定义的post-type 和每个术语之间。
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。 希望有人能理解这一点,听起来令人困惑。
也许有可能使用一些回调函数?
嘿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>
希望有所帮助!
这对我很好: -
<?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;
}
}
>?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.