Skip to content

Commit

Permalink
PLANET-7268 Add relevant meta fields to all listing pages
Browse files Browse the repository at this point in the history
These are needed for better SEO
  • Loading branch information
mleray committed Oct 18, 2023
1 parent ad5bf81 commit 8b04b03
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions author.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
'width' => '300',
'height' => '300',
];
$context['og_type'] = 'profile';

$author_share_buttons = new stdClass();
$author_share_buttons->title = $author->name;
Expand Down
9 changes: 6 additions & 3 deletions category.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
$templates = [ 'taxonomy.twig', 'index.twig' ];

$context = Timber::get_context();
$context['taxonomy'] = get_queried_object();
$context['wp_title'] = $context['taxonomy']->name;
$taxonomy = get_queried_object();
$context['taxonomy'] = $taxonomy;
$context['wp_title'] = $taxonomy->name;
$context['canonical_link'] = home_url($wp->request);
$context['og_type'] = 'website';
$context['og_description'] = $taxonomy->description;

if (!empty(planet4_get_option('new_ia'))) {
$view = ListingPageGridView::is_active() ? 'grid' : 'list';
Expand All @@ -34,7 +37,7 @@
}

$post_args = [
'cat' => $context['taxonomy']->term_id,
'cat' => $taxonomy->term_id,
'posts_per_page' => 10,
'post_type' => 'post',
'paged' => 1,
Expand Down
1 change: 1 addition & 0 deletions src/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ public function get_og_title(): string
public function get_og_description(): string
{
$og_desc = get_post_meta($this->id, 'p4_og_description', true);

if ('' === $og_desc) {
return $this->post_excerpt;
}
Expand Down
3 changes: 2 additions & 1 deletion tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@
$context = Timber::get_context();
if ($post instanceof \WP_Post) {
$post = new Post($post->ID);
Context::set_og_meta_fields($context, $post);
}

Context::set_og_meta_fields($context, $post);
$context['tag'] = $tag;
$context['tag_name'] = single_tag_title('', false);
$context['tag_description'] = wpautop($context['tag']->description);
$context['canonical_link'] = home_url($wp->request);
$context['og_type'] = 'website';

if (!empty(planet4_get_option('new_ia'))) {
// Temporary fix with rewind, cf. https://github.com/WordPress/gutenberg/issues/53593
Expand Down
13 changes: 8 additions & 5 deletions taxonomy.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* The template for displaying Taxonomy pages.
* The template for displaying Taxonomy pages (Post types, Action types).
*
* Used to display taxonomy-type pages
*
Expand All @@ -19,9 +19,12 @@
$templates = [ 'taxonomy.twig', 'index.twig' ];

$context = Timber::get_context();
$context['taxonomy'] = get_queried_object();
$context['wp_title'] = $context['taxonomy']->name;
$taxonomy = get_queried_object();
$context['taxonomy'] = $taxonomy;
$context['wp_title'] = $taxonomy->name;
$context['canonical_link'] = home_url($wp->request);
$context['og_type'] = 'website';
$context['og_description'] = $taxonomy->description;

if (!empty(planet4_get_option('new_ia'))) {
$context['page_category'] = 'Listing Page';
Expand Down Expand Up @@ -51,9 +54,9 @@
'has_password' => false, // Skip password protected content.
'tax_query' => [
[
'taxonomy' => $context['taxonomy']->taxonomy,
'taxonomy' => $taxonomy->taxonomy,
'field' => 'slug',
'terms' => $context['taxonomy']->slug,
'terms' => $taxonomy->slug,
],
],
];
Expand Down
2 changes: 1 addition & 1 deletion templates/base.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% block html_head_container %}

{% include 'html-header.twig' with { 'canonical_link': canonical_link } %}
{% include 'html-header.twig' %}

{% endblock %}

Expand Down
17 changes: 15 additions & 2 deletions templates/blocks/meta_fields.twig
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{% if ((post and (1 != fn('post_password_required', post))) or author is defined) or tag_name is defined %}
{% set is_listing_page = tag_name is defined or taxonomy is defined or author is defined %}
{% set is_news_page = page_category == 'News' %}

{% if (post and (1 != fn('post_password_required', post))) or is_listing_page or is_news_page %}
{% if (author.description is defined and author.description) %}
{% set meta_description = author.description|striptags %}
{% elseif (post.post_excerpt is defined and post.post_excerpt) %}
{% set meta_description = post.post_excerpt|striptags %}
{% elseif (tag_description is defined and tag_description) %}
{% set meta_description = tag_description|striptags %}
{% elseif (taxonomy is defined and taxonomy.description) %}
{% set meta_description = taxonomy.description|striptags %}
{% else %}
{% set meta_description = site.description %}
{% endif %}
Expand All @@ -25,10 +32,16 @@

<meta name="title" content="{{ title|e('html_attr')|raw }}"/>
<meta property="og:title" content="{{ title|e('html_attr')|raw }}" />
<meta property="og:type" content="article" />
<meta property="og:url" content="{{ current_url }}" />
{% if (og_type) %}
<meta property="og:type" content="{{ og_type }}" />
{% else %}
<meta property="og:type" content="article" />
{% endif %}
{% if (og_description) %}
<meta property="og:description" content="{{ og_description|striptags }}" />
{% elseif (tag_description) %}
<meta property="og:description" content="{{ tag_description|striptags }}" />
{% endif %}
{% if (og_image_data) %}
<meta property="og:image" content="{{ og_image_data['url'] }}" />
Expand Down

0 comments on commit 8b04b03

Please sign in to comment.