2015-02-05 35 views
1

我正在使用Anchor CMS。使用Anchor CMS显示草稿和存档帖子通过“相关帖子”显示

除了标有'草稿'的帖子仍然在“相关帖子”部分显示在网站上的帖子之外,一切都很好,但我不知道为什么,从我所看到的情况来看,只有已发布的帖子应该显示出来。

这是我用来显示在每篇文章的底部相关职位代码:

的functions.php

function related_posts($n) { 
$posts = Post::get(Base::table('posts'), '=', 'published'); 
$postarr = array(); 
foreach($posts as $post) : 
if($post->id != article_id()) { 
    if($post->category == article_category_id()) { 
     array_push($postarr, $post); 
    } 
} 
endforeach;  
shuffle($postarr); 
$postarr = array_slice($postarr, 0, $n); 
return $postarr; 
} 

function article_category_id() { 
if($category = Registry::prop('article', 'category')) { 
$categories = Registry::get('all_categories'); 
return $categories[$category]->id; 
} 
} 

Article.php

<?php foreach(related_posts(3) as $post) : ?> 
<div class="similar-posts"> 
<div class="simi-alt"> 
<a href="<?= $post->slug; ?>"><?= $post->title; ?></a> 
</div> 
<p class="sim-desc"><?= $post->description; ?> <a href="<?= $post->slug; ?>">Read more..</a></p> 
</div> 
<?php endforeach; ?> 

回答

0

我不不知道这个CMS是什么,但documentation on the website不太好。

我刚下载去调查Post课程。

class Post extends Base { 
... 
private static function get($row, $val) { 
... 
->where(Base::table('posts.'.$row), '=', $val) 

从我的角度来看,这意味着你应该发送2个参数 - 一个是字段名和第二个字段值。

所以我猜,但你可以尝试在你的代码改变这一行:

$posts = Post::get(Base::table('posts'), '=', 'published'); 

这一个:

$posts = Post::get(Base::table('posts'), 'status', 'published'); 

或甚至:

$posts = Post::get('status', 'published'); 
+0

不幸,既不工作,但谢谢你的尝试!这是一个棘手的问题。 – 2015-02-05 21:11:17

+0

如果发生'$ posts = Post :: get('status','published');'??它是否会返回相同的帖子集? – Alex 2015-02-05 21:17:32

+0

是的,没有改变! – 2015-02-10 13:05:41