简体   繁体   English

尝试获取当前帖子的 Wordpress 中的自定义帖子类型类别链接作为帖子的元数据

[英]Trying to get the custom Post type category link in Wordpress for the current post as meta data of the post

I am trying to place a link to the Artist of a video in a custom post type in WordPress.我正在尝试在 WordPress 的自定义帖子类型中放置指向视频艺术家的链接。 Everything is working but the term_id does not iterate.一切正常,但 term_id 没有迭代。

<?php
$args = array(
    'post_type'      => 'video',
    'posts_per_page' => 4,
    'taxonomy' => $term->name,
    'number' => $term->term_id,
);
        
$q = new WP_Query( $args);

if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
    $q->the_post();        
        //Your template tags and markup like:
        ?>
        
      <div class="card">
          <div class="bg-img"><img  alt="Norway" style="width:100%" src="<?php the_post_thumbnail( 'thumbnail' );?></div>
          <div class="content">
            <h4 style="color: green;"><?php the_title( ); ?></h4>
             <h5 class="artist-name"><?php
  
  $terms = get_terms( $args );
    foreach( $terms as $term ){
  
        echo '<div style="color: black;"><a href="' . esc_url( get_term_link( $term->slug, 'band' ) ) . '">' . esc_html( $term->name ) . " ". $term->term_id . '</a></div>'; 
            
    }  ?>

As per your code, it seems that you are using the posts arguments for "get_terms" which is not correct and that's why you are not getting the correct term's data.根据您的代码,您似乎正在使用帖子 arguments 来表示“get_terms”,这是不正确的,这就是您没有获得正确术语数据的原因。

You can check the query parameter of terms over here to get the idea.您可以在此处检查术语的查询参数以了解想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM