简体   繁体   English

获取Wordpress中single.php中帖子列表的类别链接

[英]Get category link to a posts list in single.php in Wordpress

We know that single.php in Wordpress is inside the loop so I can directly use the_title() or the_permalink() without having to create a custom query. 我们知道Wordpress中的single.php在循环内部,因此我可以直接使用the_title()the_permalink()而无需创建自定义查询。

I am doing that already, but on top of it, I have a sidebar that displays the latest posts (custom post type) with their titles, links and categories. 我已经这样做了,但最重要的是,我有一个侧边栏,显示最新的帖子(自定义帖子类型)及其标题,链接和类别。

I am able to retrieve all their related infor except the category link. 我能够检索除类别链接之外的所有相关信息。

The code I have now returns category uncategorized for all the posts even though they are each in a specific category. 我现在的代码返回所有帖子的uncategorized类别,即使它们都属于特定类别。

This is the custom query that I'm using which is fetching posts from a custom post type cards inside single.php 这是我正在使用的自定义查询,它从single.php的自定义帖子类型cards获取帖子

Notice $categories = get_categories(); 注意$categories = get_categories(); - the foreach loop displays the following URL for all posts which is simply not true. - foreach循环显示所有帖子的以下URL,这是不正确的。

http://localhost/wonderhive/category/uncategorized/

How can I fix that and retrieve the correct category URL? 我该如何解决这个问题并检索正确的类别网址? Since I'm already retrieving the correct category name. 因为我已经检索到了正确的类别名称。

<?php
            $queryObject = new WP_Query( 'post_type=cards&posts_per_page=-1' );
            if ($queryObject->have_posts()) {

                while ($queryObject->have_posts()) {
                    $queryObject->the_post(); ?>
                <div class="vista bg-black p-12 h-60 black">
                    <a href="<?php the_permalink(); ?>">
                        <img src="<?php the_post_thumbnail_url('small'); ?>" alt="gian" class="f-left foto r-100 ">
                        <div class="f-left">
                            <h5 class="gray2">
                                <?php
                                $thetitle = $post->post_title;
                                $getlength = strlen($thetitle);
                                $thelength = 45;
                                echo substr($thetitle, 0, $thelength);
                                if ($getlength > $thelength) echo "...";
                                ?>
                            </h5>
                    </a>
                        <h6>
                            <?php
                            $categories = get_categories();
                            foreach ($categories as $cat) {
                                $category_link = get_category_link($cat->cat_ID);
                                echo $category_link;
                            }
                            ?>
                            <a href="">
                                <?php $terms = wp_get_post_terms($post->ID,'categories');
                                foreach ($terms as $term) {
                                    echo $term->name;
                                }
                                ?>
                            </a>
                        </h6>
                    </div>
                    <span class="f-right"><?php echo get_the_date(); ?></span>
                </div>
                <?php }
            }
            ?>

You should use $categories = get_the_category() instead of $categories = get_categories() . 您应该使用$categories = get_the_category()而不是$categories = get_categories() This function will get the categories for the current post instead. 此函数将获取当前帖子的类别。

For more information see the WordPress Codex on get_the_category() . 有关更多信息,请参阅get_the_category()上WordPress Codex Good luck! 祝好运!

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

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