显示标记为页面标题的帖子

时间:2013-06-14 08:24:31

标签: php wordpress

我有一个页面模板,对于网站的所有页面都是相同的。 就像现在一样,它显示类别中与页面同名的帖子。 有没有办法只显示与页面标题具有相同标签的帖子? 这是我的代码

<?php $catname = wp_title('', false); ?>
<?php query_posts("category_name=$catname&showposts=10"); ?>
<?php $posts = get_posts("category_name=$catname&numberposts=3&offset=0");
foreach ($posts as $post) : the_post(); ?>


<div class="entry3 ey3">        

             <h2><span><?php the_title(); ?> </span></h2>
                <p><?php the_content(); ?></p>

            </div>

         <?php endforeach; ?>  <?php else : ?>  
          </div>
 

1 个答案:

答案 0 :(得分:0)

要按标记发布帖子(从此处:http://codex.wordpress.org/Template_Tags/get_posts),请在您的参数列表中添加tax_query

<?php $catname = wp_title('', false); ?>
<?php 
$args = array(
'tax_query' => array(
    array(
        'field' => 'slug',
        'terms' => $catname
    )
),
'numberofposts'=>3,
'offset'=>0
);
query_posts($args); ?>
<?php $posts = get_posts("category_name=$catname&numberposts=3&offset=0");
foreach ($posts as $post) : the_post(); ?>


<div class="entry3 ey3">        

         <h2><span><?php the_title(); ?> </span></h2>
            <p><?php the_content(); ?></p>

        </div>

     <?php endforeach; ?>  <?php else : ?>  
      </div>
相关问题