/*
* Morocco Private Tours Blog
* Shortcode: [mpt_blog]
*/
if (!defined('ABSPATH')) {
exit;
}
/* Image: featured image, then first image in article. */
function mpt_blog_thumbnail($post_id) {
$featured = get_the_post_thumbnail_url($post_id, 'large');
if ($featured) {
return $featured;
}
$content = get_post_field('post_content', $post_id);
if (!$content) {
return '';
}
/* Gutenberg / WordPress image attachment. */
if (preg_match('/wp-image-([0-9]+)/i', $content, $match)) {
$image = wp_get_attachment_image_url((int) $match[1], 'large');
if ($image) {
return $image;
}
}
/* Image URL inside the article, including lazy-loaded images. */
if (preg_match_all('/
Explore our latest articles, destination guides and
practical tips for your next journey through Morocco.
post_count) {
echo 'Our travel articles are coming soon.';
}
?>
]*>/i', $content, $images)) {
foreach ($images[0] as $tag) {
foreach (array('data-src', 'data-lazy-src', 'src') as $attribute) {
$pattern = '/\b' . preg_quote($attribute, '/') .
'\s*=\s*(["\'])(.*?)\1/is';
if (preg_match($pattern, $tag, $match)) {
$image = html_entity_decode(
$match[2],
ENT_QUOTES,
'UTF-8'
);
if (strpos($image, '//') === 0) {
$image = 'https:' . $image;
} elseif (strpos($image, '/') === 0) {
$image = home_url($image);
}
if (preg_match('~^https?://~i', $image)) {
return esc_url_raw($image);
}
}
}
}
}
return '';
}
/* Fetch published articles, newest first. */
function mpt_blog_query($page = 1) {
return new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => max(1, (int) $page),
'orderby' => array('date' => 'DESC', 'ID' => 'DESC'),
'ignore_sticky_posts' => true,
'has_password' => false,
));
}
/* Generate the article cards. */
function mpt_blog_cards($query) {
ob_start();
foreach ($query->posts as $article) {
$post_id = $article->ID;
$title = get_the_title($post_id);
$link = get_permalink($post_id);
$image = mpt_blog_thumbnail($post_id);
$excerpt = $article->post_excerpt;
if (!$excerpt) {
$excerpt = strip_shortcodes($article->post_content);
}
$excerpt = wp_trim_words(
wp_strip_all_tags($excerpt),
24,
'…'
);
?>
Morocco Travel Journal
Your Morocco travel inspiration
Latest articles