Wordpress 尝试在循环数组中使用 get_title() 作为类别名称


Wordpress Trying to use get_title() in loop array as the category name

在我的WordPress页面模板上,我试图在我的参数数组中使用get_title()进行循环,但我无法让它返回任何帖子。

这就是我要去的地方;

<?php 
            $args = array( 
                      'category_name' => 'the_title();'
                   );
            $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <div class="entry"><?php the_content(); ?></div>
<?php endwhile; else: ?>
    <p>Sorry, there are no posts to display</p>
<?php endif; ?>
除非我手动输入我要使用的类别的 slug,

否则我无法在此页面上返回任何帖子,这并不理想,因为页面 slug 将始终与帖子类别 slug 相同。

背后的逻辑是,我不想为不同的猫的每个页面都有一个单独的页面寺庙,因为我最终会得到数百个页面寺庙。

任何帮助表示赞赏干杯乔恩

您正在将the_title();作为字符串传递给' ' .此外,the_title()获取循环中对象的标题,而不是类别的标题。
您可以执行以下操作:

 global $post; 
 $args = array('category_name' => $post->post_name);

您已将the_title();作为字符串传递,因此将被视为字符串,而不是函数,因此它不会返回标题。你可以使用全局变量$cat,所以你的代码将是,

<?php
global $cat;
$args = array('cat' => $cat);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <div class="entry"><?php the_content(); ?></div>
<?php endwhile; else: ?>
    <p>Sorry, there are no posts to display</p>
<?php endif; ?>