首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在WordPress中将帖子链接到其类别

如何在WordPress中将帖子链接到其类别
EN

Stack Overflow用户
提问于 2019-09-28 20:52:23
回答 1查看 40关注 0票数 0

当我的帖子被点击时,我想添加一个链接。该链接必须转到帖子的类别。我已经走了这么远,但现在我被卡住了。有人能教我怎么做吗?

代码语言:javascript
复制
<?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;
?>
EN

回答 1

Stack Overflow用户

发布于 2019-09-28 21:36:01

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

代码语言:javascript
复制
<?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; ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58146498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档