【问题标题】:Drafts and Archived posts are showing through 'Related posts' using Anchor CMS使用 Anchor CMS 通过“相关帖子”显示草稿和存档帖子
【发布时间】:2015-04-05 11:03:33
【问题描述】:

我正在使用 Anchor CMS。

一切都很好,除了标记为“草稿”的帖子仍在网站上的“相关帖子”部分实时显示,但我不知道为什么据我所见,只有已发布的帖子应该显示。

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

函数.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;
}
}

文章.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; ?>

【问题讨论】:

    标签: php html anchor-cms


    【解决方案1】:

    我不知道这个 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');
    

    【讨论】:

    • 不幸的是,两者都不起作用,但感谢您的尝试!这是一个棘手的问题。
    • 如果$posts = Post::get('status', 'published'); 会发生什么?它会返回相同的帖子集吗?
    猜你喜欢
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多