2014-12-02 79 views
1

在我的網站我充滿了公司的目錄(自定義後型),各自有各自的清單頁面。現在在頁面的底部,我想顯示所有貼有該公司標題的帖子,如其目錄列表頁面所示。顯示帖子基於稱號,自定義文章類型

目錄頁

示例公司
示例公司

爲了測試相關的帖子,我手動把 'rennicks' 作爲標記。然後在大約5個帖子中添加了'rennicks'作爲標籤,並且它們都顯示在列表頁面中。但顯然我需要它動態檢索標題並根據該變量中的數據搜索標籤。

$original_query = $wp_query; 
$wp_query = null; 
$args=array('tag' => 'rennicks'); 
$wp_query = new WP_Query($args); 
if (have_posts()) : 
?> 
    <?php 
     while (have_posts()) : the_post(); ?> 
      <div class="small-12 medium-6 columns content-excerpt"> 
       <div class="thumbnail medium-6 columns nopm"> 
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a> 
       </div> 
       <div class="content"> 
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
        <p><?php //the_excerpt(); ?></p> 
        <div class="related-stats-info"> 
         <ul> 
          <!-- <li><?php //the_author(); ?></li> --> 
          <li><i class="fa fa-clock-o"></i> <?php the_date('Y-m-d') ?></li> 
          <li><i class="fa fa-comments"></i> <?php comments_number('0 comments', '1 comment', '% comments'); ?></li> 
         </ul> 
        </div> 
       </div> 
      </div> 
     <?php endwhile; ?> 
     <div class="clearboth"></div> 
<?php 
endif; 
$wp_query = null; 
$wp_query = $original_query; 
wp_reset_postdata(); 
+0

我不知道最終目標是什麼。您是否想要動態更改正在引入的標記,而不是硬編碼「rennicks」?所以,也許你正試圖讓自定義類型的帖子使用所有標籤? – 2014-12-02 17:55:59

+0

知道了!那麼,這不會是一個問題,但只有我們需要首先理清。標籤(AFAIK)不能有空格,並且有些標題有空格。如果我們用slu as作爲標籤而不是標題,你會怎麼想?或者,我們可以考慮使用像高級自定義字段或類型這樣的wordpress擴展名來獲取多個帖子之間的鏈接,而不使用tags字段。 – 2014-12-02 17:59:59

+0

Slug會做得很好。我也有ACF。如果可能的話,S would對我來說是最好的。 – JordanC26 2014-12-02 18:02:34

回答

2

你需要得到後期slu so,所以首先我會閱讀該評論,並確認你得到了slu。。嘗試可能做一個var_dump($ slug);出口;緊接在slug變量之後,以驗證您的值是否正確。一旦你這樣做,試試這個鏡頭:

// Get the slug (This is assuming you 
// have the current post in a $post 
// variable. Otherwise, load the post 
// with the post ID like so: 
// $post = get_post($post_id); 
$slug = $post->post_name; 

// Build args array for query, replacing hyphens with nothing on the slug. 
$args = array('tag' => str_replace('-', '', $slug)); 

// Set wp_query with tag args 
$wp_query = new WP_Query($args); 

// If we get result posts from our query... 
if (have_posts()) :?> 
    <?php while (have_posts()) : the_post(); ?> 
     <div class="small-12 medium-6 columns content-excerpt"> 
      <div class="thumbnail medium-6 columns nopm"> 
       <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a> 
      </div> 
      <div class="content"> 
       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
       <p><?php //the_excerpt(); ?></p> 
       <div class="related-stats-info"> 
        <ul> 
         <!-- <li><?php //the_author(); ?></li> --> 
         <li><i class="fa fa-clock-o"></i> <?php the_date('Y-m-d') ?></li> 
         <li><i class="fa fa-comments"></i> <?php comments_number('0 comments', '1 comment', '% comments'); ?></li> 
        </ul> 
       </div> 
      </div> 
     </div> 
    <?php endwhile; ?> 
    <div class="clearboth"></div> 
<?php endif; ?> 
<?php 
// Reset wordpress... 
wp_reset_query(); 
wp_reset_postdata(); 
+0

是的,它肯定會得到正確的slu,,'rennicks-mts'就是這個例子。給標籤'rennicks-mts'設置3條更多文章,但是沒有任何內容返回到商品頁面上。嘗試在另一個列表頁面上的標籤也是一樣的。嗯。 – JordanC26 2014-12-02 18:45:16

+0

這很奇怪,如果我迅速改變$塞一個靜態的標籤,只是一個字似乎工作。 rennicks的作品,但rennicks-mts即使不是靜態的。 另外,如果我改變它使用POST_ID相反,它會忽略標籤,並帶來了最新的10個職位。 – JordanC26 2014-12-02 18:55:17

+0

我想知道在查詢標籤時連字符是否會引起問題?也許嘗試用'rennicksmts'標記幾個帖子並將其硬編碼嵌入slu。中。如果這樣的作品,我們可以嘗試從蛞蝓刪除連字符和走這條路線... – 2014-12-02 18:58:48

相關問題