Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Blog model for interfacing with a Wordpress backend
- *
- * @author Alex Crooks http://alexcrooks.me
- **/
- // Load the wordpress bootstrap and it's helpers
- define('WP_USE_THEMES', false);
- require_once(APP_PATH.BLOG_FOLDER.'wp-blog-header.php');
- class Blog
- {
- function __construct()
- {
- // STUB
- }
- // Get our latest blog posts
- function getLatest($numOfPosts = BLOG_NUM_POSTS, $offset = 0)
- {
- return wp_get_recent_posts(array(
- 'numberposts' => $numOfPosts,
- 'offset' => $offset,
- 'post_status' => 'publish'
- ));
- }
- // Get post by slug ("post_name" in wp db)
- function getPost($slug = FALSE)
- {
- if(!$slug)
- return FALSE;
- $getPost = get_page_by_path($slug, 'ARRAY_A', 'post');
- if($getPost)
- {
- if($getPost['post_status'] == "publish")
- return $getPost;
- else
- return false;
- }
- else
- {
- return false;
- }
- }
- // Get preview to a post
- function getPreview($id = FALSE)
- {
- if(!$id)
- return FALSE;
- return get_post($id, 'ARRAY_A');
- }
- // Get related posts based on tags used in original post
- function getRelated($postID, $numResults = 3)
- {
- $related = array();
- $tags = array();
- $getTags = wp_get_post_tags($postID);
- if($getTags && $postID)
- {
- foreach($getTags as $tag)
- {
- $tags[] = $tag->name;
- }
- $getRelated = get_posts(array(
- 'numberposts' => $numResults,
- 'tag_slug__in' => $tags,
- 'post__not_in' => array($postID)
- ));
- foreach($getRelated as $relatedPost)
- {
- $related[] = array(
- 'ID' => $relatedPost->ID,
- 'image' => $this->getFeaturedImageUrl($relatedPost->ID),
- 'post_title' => $relatedPost->post_title,
- 'post_name' => $relatedPost->post_name
- );
- }
- }
- return $related;
- }
- function getFeaturedImageUrl($postID, $size = "large")
- {
- $image = DEFAULT_BLOG_IMAGE_URL;
- $getImage = wp_get_attachment_image_src(get_post_thumbnail_id($postID), $size);
- if($getImage)
- $image = $getImage[0];
- return $image;
- }
- function search($term)
- {
- return get_posts(array('s' => $term, 'numberposts' => -1));
- }
- function cleanOut($content)
- {
- return tag_escape($content);
- }
- }
- $blog = new Blog();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement