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.
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.
This is the custom query that I'm using which is fetching posts from a custom post type cards
inside single.php
Notice $categories = get_categories();
- the foreach loop displays the following URL for all posts which is simply not true.
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 }
}
?>