如何将帖子链接到WordPress中的类别

问题描述 投票:0回答:1

我想在我的帖子被点击时添加一个链接。链接必须转到帖子的类别。我已经走了这么远,但现在我被困住了。谁能告诉我我该怎么做?

<?php
$args = array(
    'category_name'  => 'portriats',
    'posts_per_page' => 1
);
$qry = new WP_Query($args);

if ( $qry->have_posts() ) :
    while ( $qry->have_posts() ) : $qry->the_post();
        $postcat = get_the_category( $post->ID );
        ?>

        <div class="hometile">
            <a href="<?php get_category_link( $postcat) ?>">
                ==> **I need to get the category of the post and then let PHP print the link of the categorey in the href**
                <?php the_post_thumbnail(); ?>
            </a>
        </div>

        <?php
    endwhile;
endif;
?>
wordpress post hyperlink categories
1个回答
0
投票

它将显示带有链接的帖子的主要类别

<?php $qry = new WP_Query($args ); ?>
<?php if ( $qry->have_posts() ) : ?>

  <?php while ( $qry->have_posts() ) : $qry->the_post(); $postcat = get_the_category();?>
 <div class="hometile">
  <a href="<?php echo get_category_link( $postcat[0]->term_id ); ?>">
   <?php echo $postcat[0]->name; ?>
   <?php the_post_thumbnail(); ?>
  </a>

 </div>

 <?php endwhile; ?>
<?php endif; ?>
© www.soinside.com 2019 - 2024. All rights reserved.