【问题标题】:WordPress: Display Other Posts From Current CategoryWordPress:显示当前类别的其他帖子
【发布时间】:2010-08-08 20:26:23
【问题描述】:

我有一个功能可以显示同一自定义分类下的所有帖子,称为“问题”。我需要对其进行调整,使其进一步缩小范围,使其仅显示同一类别下的帖子。

我查看了 WordPress 的 get_the_category() 函数,但运气不佳。

代码如下:

<?php
$issueid = get_the_term_list( $post->ID, 'issue', '', ', ', '' );
$postslist = get_posts("numberposts=100&issue=$issueid");
 foreach ($postslist as $post) : 
 setup_postdata($post); ?>

<div class="sidebar-box">

<div class="sidebar-left">

<p><a href="<?php echo get_page_link($page->ID) ?>"><?php the_title(); ?></a></p>

<p><?php the_date(); ?></p>

</div>

<div class="sidebar-right">

<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>

</div>

</div>

<?php endforeach; ?>

这将正确显示当前的类别 ID:

<?php
$category = get_the_category(); 
echo $category[0]->cat_id;
?>

所以我尝试编辑第一批代码以仅显示当前类别 ID 内的帖子,但它仍然返回所有内容:

$category = get_the_category(); 
$categoryid = $category[0]->cat_id; 
$issueid = get_the_term_list( $post->ID, 'issue', '', ', ', '' );
$postslist = get_posts("numberposts=100&issue=$issueid&category=$categoryid");
 foreach ($postslist as $post) : 
 setup_postdata($post); ?>

这是 get_the_category 函数参考:http://codex.wordpress.org/Function_Reference/get_the_category

任何帮助将不胜感激。

谢谢,

韦德

【问题讨论】:

  • 我对您的要求有点困惑。您是要显示自定义分类中的所有帖子,还是要显示只有一个指定问题的帖子。

标签: php wordpress


【解决方案1】:

get_the_term_list() 返回一个 html 字符串,而不是相关类别的 ID。因此,当您将$issueid 传递给get_posts() 时,您包含的是一个html 字符串,而不是一个整数。我相信您返回所有帖子的原因是因为 WP 忽略了该查询变量,因为它不是它所期望的。

您想使用 get_posts() 并包含“问题”的 ID 以获取分配给“问题”类别的所有帖子。

您想使用get_the_category() 获取与帖子相关的所有类别。

您能否说明是否要显示与当前帖子属于“问题”类别的同一类别下的所有帖子?您想在当前帖子之后立即列出相关帖子,还是要将所有相关帖子显示到侧边栏中的所有“问题”帖子?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多