我有一個模板文件顯示我的帖子從某一年,它正在工作,但現在有一些帖子在我的網站dublicates,我不允許刪除它們。所以我的問題是,生成的列表顯示兩個不同的職位,但具有相同的內容。我有一個理想,它應該有可能測試post_title是否已經打印到頁面,但我沒有成功。所以,現在我正在尋求幫助。以下是我的模板文件,謝謝。如何在wordpress中刪除重複帖子,帖子裏有不同的id?
<?php
/* Template Name: 2012 */
get_header();
?>
<?php
$post_titles = array();
$year = isset($_GET['y']) ? $_GET['y'] : date('Y');
if($year == date('Y')){
$posts = get_newest_posts();
}
else{
$posts = get_yearly_posts($year);
} ?>
<div class="site-content-wrapper site-content-blog-wrapper container columns clearfix archive">
<article id="primary-wrapper" class="eight column">
<div class="post-blog inner">
<h2 class="entry-title"><a href="#">News <?= $year ?></a></h2>
<?php foreach($posts as $post): ?>
/* here i would like a test to see if a post with a similar title already has
printet to page. if it has not the code below should run and print post to page. */
<div class="archive-post">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody><tr>
<td width="100%"><span class="STRONG"><a href="<?= bloginfo('url') ?>/?p=<?= $post->ID ?>" target="_top"><?= $post->post_title ?></a></span></td>
</tr>
<tr>
<td><span class="norm">
<?php if($year != date('Y')): ?>
<?= $post->search_plus_description ?>
<?php else: ?>
<?= $post->post_excerpt ?>
<?php endif ?>
</span></td>
</tr>
<tr>
<td>
<span class="norm">
<?= $year == date('Y') ? date('d F Y', strtotime($post->post_date)) : date('d F Y', strtotime($post->publication_date)) ?>
</span>
</td>
</tr>
<tr>
<td><img src="x.gif" width="1" height="10" alt=""></td>
</tr>
</tbody></table>
</div>
<?php endforeach ?>
感謝您的回答。我做了這樣的函數,它通過$ post數組運行,並返回一個沒有重複的數組。似乎這樣做,我不會從數據庫中刪除。下面是代碼
function remove_duplicates(array $array){
$tmp_array = array();
$title_array = array();
$date_array = array();
foreach($array as $post)
{
$title = $post->post_title;
$date = $post->post_date;
if (!(in_array($title, $title_array) && in_array($date, $date_array)))
{
$tmp_array[] = $post;
$title_array[] = $title;
$date_array[] = $date;
}
}
return $tmp_array;
}
我會建議從數據庫中刪除重複的條目,而不是使用解決方法。 – Mark
我來,但我不能這樣做 –