我有一个模板文件,其中显示了我某年的帖子,它正在工作,但现在我的网站上有一些帖子的重复,我不允许删除它们。所以我的问题是生成的列表显示了两个不同的帖子,但内容相同。我有一个想法,应该可以测试 post_title 是否已经打印到页面,但我没有成功。所以现在我正在寻求帮助。下面是我的模板文件。
<?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;
}