handling empty relatedPostID

This commit is contained in:
Antoine M 2024-07-03 10:51:54 +02:00
parent 915f0aca68
commit 9b451fbaf3
2 changed files with 16 additions and 8 deletions

View File

@ -1,9 +1,13 @@
<?php <?php
$relatedPostId = $attributes['relatedPostId'] ?? null;
$relatedPost = get_post($relatedPostId);
$anchor = !empty($attributes['anchor']) ? esc_html($attributes['anchor']) : "post-content-container-" . $attributes['relatedPostId'];
$content = get_the_content(null, null, $relatedPost);
$relatedPostId = isset($attributes['relatedPostId']) ? $attributes['relatedPostId'] : null;
if (!$relatedPostId) return;
$relatedPost = get_post($relatedPostId) ?? null;
$anchor = isset($attributes['anchor']) && !empty($attributes['anchor']) ? esc_html($attributes['anchor']) : "post-content-container-" . $attributes['relatedPostId'];
$anchor = isset($attributes['anchor']) && !empty($attributes['anchor']) ? esc_html($attributes['anchor']) : ($relatedPostId ? "post-content-container-" . $relatedPostId : '');
$content = get_the_content(null, null, $relatedPost);
if ($relatedPost) { if ($relatedPost) {
// Temporarily set up the global post variable so the_content works as expected // Temporarily set up the global post variable so the_content works as expected

View File

@ -1,9 +1,13 @@
<?php <?php
$relatedPostId = $attributes['relatedPostId'] ?? null;
$relatedPost = get_post($relatedPostId);
$anchor = !empty($attributes['anchor']) ? esc_html($attributes['anchor']) : "post-content-container-" . $attributes['relatedPostId'];
$content = get_the_content(null, null, $relatedPost);
$relatedPostId = isset($attributes['relatedPostId']) ? $attributes['relatedPostId'] : null;
if (!$relatedPostId) return;
$relatedPost = get_post($relatedPostId) ?? null;
$anchor = isset($attributes['anchor']) && !empty($attributes['anchor']) ? esc_html($attributes['anchor']) : "post-content-container-" . $attributes['relatedPostId'];
$anchor = isset($attributes['anchor']) && !empty($attributes['anchor']) ? esc_html($attributes['anchor']) : ($relatedPostId ? "post-content-container-" . $relatedPostId : '');
$content = get_the_content(null, null, $relatedPost);
if ($relatedPost) { if ($relatedPost) {
// Temporarily set up the global post variable so the_content works as expected // Temporarily set up the global post variable so the_content works as expected