繁体   English   中英

WordPress-按网址中的类别名称显示帖子

[英]Wordpress - Display posts by category name from url

我正在努力使我的category.php能够按照URL中的子类别按类别获取并显示正确的帖子。 因此,例如www.example.com/category/news应该显示所有标记为“新闻”类别的帖子-但当前它显示的所有帖子都是奇怪的...

我如何获取类别ID,因为我知道那是要传递到循环中的值,而不是实际名称,然后仅显示与url中的类别ID相匹配的帖子?

这捕获了类别名称-但我需要ID

<?php $page_object = get_queried_object(); ?>
<h1><?php echo $page_object->cat_name; ?></h1>

然后我可以传递给:

<?php query_posts('cat=ID HERE'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>

因此,寻找捕获/ category / name的ID并将其传递给代码

有任何想法吗?

自行解决-以下解决方案:

<?php $page_object = get_queried_object(); ?>
<?php $categoryID= $page_object->cat_ID; ?>

<?php query_posts("cat=$categoryID"); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>

自行解决-以下解决方案:

<?php $page_object = get_queried_object(); ?>
<?php $categoryID= $page_object->cat_ID; ?>

<?php query_posts("cat=$categoryID"); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>

暂无
暂无

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

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