![](/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.