Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在wordpress cpt的子分类法模板页面上获取父分类法术语id_Php_Wordpress_Custom Wordpress Pages - Fatal编程技术网

Php 在wordpress cpt的子分类法模板页面上获取父分类法术语id

Php 在wordpress cpt的子分类法模板页面上获取父分类法术语id,php,wordpress,custom-wordpress-pages,Php,Wordpress,Custom Wordpress Pages,我有一个带有分类法“部分”的自定义帖子类型,还有一个带有查询循环的模板页面,用于显示带有缩略图的类别列表。我只想显示当前父分类法的子类别,并希望能够在子页面上获取父类别的ID 我目前已在代码中将父项设置为ID40,但需要动态设置。如何将40动态更改为当前父id 这是我在分类法模板页面中的代码 <?php $terms = get_terms( [ 'taxonomy' => 'section', 'parent'

我有一个带有分类法“部分”的自定义帖子类型,还有一个带有查询循环的模板页面,用于显示带有缩略图的类别列表。我只想显示当前父分类法的子类别,并希望能够在子页面上获取父类别的ID

我目前已在代码中将父项设置为ID40,但需要动态设置。如何将40动态更改为当前父id

这是我在分类法模板页面中的代码

<?php
      $terms  = get_terms( [
          'taxonomy'      => 'section',
          'parent'        => 40,
          'hide_empty'    => true,
          'relationship'  => [
              'id' => 'categories_to_posts',
              'to' => get_the_ID(), // You can pass object ID or full object
          ],
      ] );
      if ( $terms && ! is_wp_error( $terms ) ) {
      foreach ( $terms as $term ) {
        $term_link = get_term_link( $term->term_id );
        $term_name = $term->name;
        $url = get_term_meta( $term->term_id, 'kte_sec_thumbnail_image', true );
          echo '<img src="' . esc_url( $url ) . '">';
           $term = get_term_by( 'id', $child, $taxonomy_name );
           echo '<a href="' . esc_url( $term_link ) . '">' . $term_name . '</a>';
  }
  }

据我所知,您希望在父分类法页面上显示当前父分类法的子术语。因此,请使用此代码实现您的功能

<?php 
$term123 = get_queried_object();
$slug=$term123->slug;
$parent_id  =$term123->parent;
$child_id=$term123->term_id;


$taxonomy_name ='section';
$termchildren  = get_term_children(  $child_id, $taxonomy_name  );
echo "<ul>";
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
?>
  <?php if ($term->parent == $child_id) { ?>

    <li><a href="<?php echo get_term_link($term->term_id); ?>"><?php echo $term->name; ?></a></li>
  <?php } 

  ?>

<?php } 
echo "</ul>";

?>


  • 谢谢您的回复,这很有效。当我第一次将代码粘贴到我的模板中时,我没有得到任何结果,但我检查了使用var_dump进行调试,并且返回的$parent_id正确地返回了parent id。然后,我将其与原始代码一起使用,使用$parent_id替换了原始示例中的id 40,现在我的模板工作正常。非常感谢你的帮助。