Skip to content

Instantly share code, notes, and snippets.

@bugsysop
Created May 25, 2013 22:56
Show Gist options
  • Save bugsysop/5651053 to your computer and use it in GitHub Desktop.
Save bugsysop/5651053 to your computer and use it in GitHub Desktop.
Anchor CMS. Display only selected categories on a page. Code to add in the custom page template.
<?php
// allowCat contains all catergory ID that you want on your page
$allowCat = array(2,3,4,5,6);
function article_category_id() {
if($category = Registry::prop('article', 'category')) {
$categories = Registry::get('all_categories');
return $categories[$category]->id;
}
}
function custom_posts_page() {
// only run on the first call
if( ! Registry::has('rwar_post_archive')) {
// capture original article if one is set
if($article = Registry::get('article')) {
Registry::set('original_article', $article);
}
}
if( ! $posts = Registry::get('rwar_post_archive')) {
$posts = Post::where('status', '=', 'published')->get();
Registry::set('rwar_post_archive', $posts = new Items($posts));
}
if($result = $posts->valid()) {
// register single post
Registry::set('article', $posts->current());
// move to next
$posts->next();
}
else {
// back to the start
$posts->rewind();
// reset original article
Registry::set('article', Registry::get('original_article'));
// remove items
Registry::set('rwar_post_archive', false);
}
return $result;
}
?>
<?php while(custom_posts_page()): ?>
<?php foreach($allowCat as $cat): ?>
<?php if(article_category_id() == $cat): ?>
<li>
<article class="wrap">
<h1><a href="<?php echo article_url(); ?>" title="<?php echo article_title(); ?>"><?php echo article_title(); ?></a></h1>
<p>
<?php echo article_description(); ?>
</p>
</article>
</li>
<?php endif; ?>
<?php endforeach; ?>
<?php endwhile; ?>
@pragmaticus
Copy link

Hey bugsysop,
I'm using your custom_posts_page Function to retrieve a list of posts on a specific page. So far i'm getting older posts first. I want to return the latest post first but can't figure out how to reverse the order. Can you help me on this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment