當您在每個類別中循環查看帖子時,您並未找到與該類別相關的帖子,而是循環遍歷每個帖子。下面(未經測試,如果發生錯誤,請發表評論)應對您的問題進行排序。
但請注意,如果帖子被分配到多個類別,您可能仍會得到一些重複。
檢查每個帖子,以確保它在所需的類別。好點 - 只有一個查詢。壞點 - 循環通過每隻貓的所有職位。
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php if($my_query->have_posts()) : ?>
<ul>
<?php if(in_category($term->slug)) : ?>
<?php while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
<?php endif; ?>
</div>
</div>
<?php
endforeach;
endif;
?>
重新查詢帖子並輸出所有抓取的帖子。好點 - 只拉取類別所需的帖子。不好的一點 - 很多查詢,所以很多查詢可能會減慢你的網站。
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php
$args = array(
'cat' => $term->term_id
);
$my_query = new WP_Query($args);
?>
<ul>
<?php if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</ul>
</div>
</div>
<?php
endforeach;
endif;
?>
嘿Himanshu,你現在用什麼函數輸出存檔? – BarryWalsh 2013-03-21 11:25:56
嗨巴里, 我剛剛編輯的問題,並粘貼的PHP代碼.. – 2013-03-21 11:39:45