【发布时间】:2016-10-29 16:58:23
【问题描述】:
我们知道 Wordpress 中的 single.php 在循环内,因此我可以直接使用 the_title() 或 the_permalink() 而无需创建自定义查询。
我已经这样做了,但最重要的是,我有一个侧边栏,显示最新帖子(自定义帖子类型)及其标题、链接和类别。
我能够检索除类别链接之外的所有相关信息。
我现在的代码为所有帖子返回类别uncategorized,即使它们都属于特定类别。
这是我正在使用的自定义查询,它从 single.php 内的自定义帖子类型 cards 获取帖子
注意$categories = get_categories(); - foreach 循环显示所有帖子的以下 URL,这根本不是真的。
http://localhost/wonderhive/category/uncategorized/
如何解决该问题并检索正确的类别 URL?因为我已经在检索正确的类别名称。
<?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 }
}
?>
【问题讨论】: