Airship\Cabin\Hull\Blueprint\Blog::listByTag PHP Method

listByTag() public method

Return an array of the most $num recent posts, including URL and title
public listByTag ( integer $tagId, integer $num = 20, integer $offset ) : array
$tagId integer
$num integer
$offset integer
return array
    public function listByTag(int $tagId, int $num = 20, int $offset = 0) : array
    {
        $posts = $this->db->run('SELECT
                p.*
            FROM
                view_hull_blog_post p
            LEFT JOIN
                hull_blog_post_tags t
                ON p.postid = t.postid
            WHERE
                p.status
                AND p.published <= current_timestamp
                AND t.tagid = ?
            ORDER BY published DESC
            OFFSET ' . $offset . '
            LIMIT ' . $num, $tagId);
        if (empty($posts)) {
            return [];
        }
        foreach ($posts as $i => $post) {
            $posts[$i] = $this->postProcess($post);
        }
        return $posts;
    }

Usage Example

Example #1
0
 /**
  * List all of the blog posts for a given tag
  *
  * @route blog/tag/{slug}/{page}
  * @param string $slug
  * @param string $page
  */
 public function listByTag(string $slug, string $page = '')
 {
     list($offset, $limit) = $this->getOffsetAndLimit($page);
     $tag = $this->blog->getTag($slug);
     $count = $this->blog->countByTag((int) $tag['tagid']);
     $blogRoll = $this->blog->listByTag((int) $tag['tagid'], $limit, $offset);
     $mathJAX = false;
     foreach ($blogRoll as $i => $blog) {
         $blogRoll[$i] = $this->blog->getSnippet($blog);
         if (Binary::safeStrlen($blogRoll[$i]['snippet']) !== Binary::safeStrlen($blog['body'])) {
             $blogRoll[$i]['snippet'] = \rtrim($blogRoll[$i]['snippet'], "\n");
         }
         $mathJAX = $mathJAX || \strpos($blog['body'], '$$') !== false;
     }
     $args = ['blogroll' => $blogRoll, 'pageTitle' => \__('Blog Posts Tagged "%s"', 'default', Util::noHTML($tag['name'])), 'mathjax' => $mathJAX, 'pagination' => ['base' => \Airship\LensFunctions\cabin_url() . 'blog/tag/' . $slug, 'count' => $count, 'page' => (int) \ceil($offset / ($limit ?? 1)) + 1, 'per_page' => $limit]];
     $this->config('blog.cachelists') ? $this->stasis('blog/tag', $args) : $this->lens('blog/tag', $args);
 }