オブジェクトを配列に変換する必要はありません。あまり手間をかけずにオブジェクトを完全に操作できます。(少なくとも私にとって)興味深いのは、次のようなものが得られることです。
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;
次に、分類法/用語ごとに1つずつ複数のループを作成できます:)。
さらに派手にしたい場合 (何百回も繰り返したくない場合) は、最初のループの中に 2 番目のループを含めて、分類法 (リソース、つまり) とそれが持つ用語 (ビデオ) (から) に変数を割り当てることができます。あなたの例は最後のものだけです)。アイデアは、カスタム投稿タイプと各用語に制限された通常の (典型的な) 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; ?>
明らかに、逆のこともできます。単一テンプレートのカスタム タイプの通常のループを作成し (1 つしかないように見えます)、内部にすべてのカスタム用語を含めます。
あまりエレガントではありませんが、それが私が思いついた最善の方法です:P. 誰かがこれを理解できることを願っています。
たぶん、いくつかのコールバック関数で可能でしょうか?.