按类别列出与页面标题匹配的 WordPress 帖子

问题描述 投票:0回答:1

我正在尝试列出共享页面名称的类别中的帖子。 IE。如果您在“服务”页面上,它应该显示“服务”类别中的帖子。我意识到使用条件语句很容易做到,例如:

<?php if ( (is_page('Groups')) ) { query_posts('category_name=groups'); 
while (have_posts()) { the_post();?>
<h2 class="title" id="sub">Upcoming Group Programs</h2>
<a href="<?php the_permalink() ?>">
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry"><?php the_content(); ?></div>
</div>
<?php } wp_reset_query(); //Restores Global Post Data }?>

但是我想这样做而不必设置多个特定条件,例如:

<?php //global $wp_query; // Uncomment if necessary, shouldn't be
$test = the_title();
$args = array( 'cat_name' => $test // ARGS HERE );
$args = array_merge( $args , $wp_query->query );
query_posts( $args ); while (have_posts()) { the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<div class="entry"><?php the_content(); ?></div></div>
<?php } ?>

有什么想法!显然,我可以将页面和类别“名称”与“slug”或任何最有效的内容互换。谢谢!

谢谢!我改变了一些事情并使其按照您的建议工作。

<?php
$catmatch = get_the_title();
//The Query
query_posts('category_name=' . $catmatch ); ?>

希望在最后一行我正确地进行了串联,它似乎可以工作,但如果这不是正确的方式,请告诉我!

php wordpress match categories title
1个回答
0
投票

尝试改变: $test = the_title();

至:

$test = get_the_title();

您可能还需要使用 array_merge(); 删除该行;

© www.soinside.com 2019 - 2024. All rights reserved.