2016-08-18 80 views
1

在wordpress中,我正在努力檢查頁面標題,然後輸出與我設置的頁面標題類別相匹配的帖子。查詢頁面標題以獲取類別帖子

因此,例如,頁面標題是'伯明翰'我也有一個類別名爲'伯明翰',如果兩者匹配,然後輸出帖子。

這裏是我的代碼,對於我的生活,我已經使用它並嘗試了很多東西,但我明顯錯過了一些非常明顯的東西。

 <?php 
     // the query 

     $thetitle = get_the_title(); 

     $the_query = new WP_Query(array('category_name' => '$page_title')); ?> 


     <?php if ($the_query->have_posts()) : ?> 

     <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
     <?php if (has_post_thumbnail()) : ?> 
     <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" > 
     <?php the_post_thumbnail(array(1000, 315)) ?></a> 
     <?php endif; ?> 
     <h2><?php the_title(); ?></h2> 
     <p><?php the_content(); ?></p> 
     <?php endwhile; ?> 

     <?php wp_reset_postdata(); ?> 

     <?php else : ?> 
     <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
     <?php endif; ?> 

回答

2

category_name是誤導性的。它期望的不是真正的類別名稱,而是類別slu。。因此,如果您的頁面標題是「foo bar」,那麼您在提供此查詢時需要查詢「foo-bar」。

要更改標題爲蛞蝓,使用sanitize_title

$thetitle = sanitize_title(get_the_title()); 
+0

這工作就像一個魅力,我非常接近!謝謝。 – PhpDude