我有一個自定義帖子類型的硬件,附加了一個名為 hardware_categories 的自定義分類法。我有一段代碼,將每個分類法輸出為 H3,并在每個分類法下輸出該分類法內的硬件項目。我在圍繞分類名稱添加永久鏈接時遇到問題。我正在嘗試的代碼在這里:<div class="container hardware-archive-container"><?php // Output all Taxonomies names with their respective items$terms = get_terms('hardware_categories');foreach( $terms as $term ): $term_link = get_term_link( $term );?> <h2><a href="<?php esc_url( $term_link ) ?>"><?php echo $term->name; ?></a></h2> <div class="row justify-content-center hardware-archive-row"> <?php $posts = get_posts(array( 'post_type' => 'hardware', 'taxonomy' => $term->taxonomy, 'term' => $term->slug, 'nopaging' => true, // to show all posts in this taxonomy, could also use 'numberposts' => -1 instead )); foreach($posts as $post): // begin cycle through posts of this taxonmy setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID) ?> <div class="col-md-3"> <?php $image = get_field( 'hardware_main_image'); if( !empty( $image ) ): ?> <img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" /> <?php endif; ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </div> <?php endforeach; ?> </div> <a href="#"><p>See all products from: <?php echo $term->name;?></p></a><?php endforeach; ?></div>我添加的用于嘗試生成鏈接的部分是這兩行:$term_link = get_term_link( $term );和<h2><a href="<?php esc_url( $term_link ) ?>"><?php echo $term->name; ?></a></h2>我嘗試了很多其他的東西,但我似乎無法獲得類別鏈接......誰能告訴我哪里錯了?感謝您的關注。
1 回答

神不在的星期二
TA貢獻1963條經驗 獲得超6個贊
你能試試這個嗎:
$term_link = get_term_link( $term->term_id, "hardware_categories" );
還可以嘗試修改您的條款:
$terms = get_terms( array(
'taxonomy' => 'taxonomy_name',
'hide_empty' => false
) );
您使用 get_terms 的方式已被棄用,請參閱文檔
你的代碼應該是這樣的
$terms = get_terms( array(
'taxonomy' => 'hardware_categories',
'hide_empty' => false
) );
foreach( $terms as $term ):?>
<h2><a href="<?php echo get_term_link( $term->term_id, 'hardware_categories');?>"><?php echo $term->name; ?></a></h2>
<?php endforeach;?>
- 1 回答
- 0 關注
- 122 瀏覽
添加回答
舉報
0/150
提交
取消