I am trying to display a blog page's featured image in a dynamic sidebar widget.
I've created a content template to display a post/page featured image (and title) as the page header image/title, using the widget shortcode:
[wpv-post-body view_template="Page Title Template"]
This works perfectly on all pages EXCEPT for the blog page.
The blog page is displaying the dynamic widget correctly, but it is using image and title for the FIRST BLOG POST instead.
Strangely, when I edit the dynamic widget, and choose the blog page in the live preview, it DOES display correctly - but not on the live blog page (see attached).
I'm pretty sure I need to add a conditional somewhere and manually set the image somehow:
if get_option('page_for_posts')
...but I'm not sure where or how to do this.
Hi,
Thank you for contacting us and I'd be happy to assist.
To troubleshoot and suggest the best way to achieve this, I'll need to see how this dynamic sidebar widget is set up.
Can you please share temporary admin login details, along with the link to the blog page in question?
Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.
regards,
Waqar
Thank you for sharing these details.
The challenge here is that the blog page acts differently than regular pages and by the time the code in your content template executes, the loop for the blog/posts page is started.
This is how, I would overcome this:
1. I would register a new widget area "title-widget-area-blog", the same way the "title-widget-area" widget area is registered in the active child theme's "functions.php" file.
( screenshot: hidden link )
2. Next, in the child theme's "header.php" file, the code that loads this widget area can be changed, so that it loads existing "title-widget-area" on pages other than blog and new "title-widget-area-blog" on the blog page.
Current code:
<?php if (is_active_sidebar( 'title-widget-area' ) ) : ?>
<?php if (!is_archive()) : ?>
<div id="page-title-sidebar" class="title-widget-area">
<?php dynamic_sidebar( 'title-widget-area' ); ?>
</div>
<?php endif; ?>
<?php endif; ?>
Updated code:
<?php if (is_active_sidebar( 'title-widget-area' ) ) : ?>
<?php if ( (!is_archive()) && (!is_home()) ) : ?>
<div id="page-title-sidebar" class="title-widget-area">
<?php dynamic_sidebar( 'title-widget-area' ); ?>
</div>
<?php endif; ?>
<?php endif; ?>
<?php if (is_active_sidebar( 'title-widget-area-blog' ) ) : ?>
<?php if ( is_home() ) : ?>
<div id="page-title-sidebar" class="title-widget-area title-widget-area-blog">
<?php dynamic_sidebar( 'title-widget-area-blog' ); ?>
</div>
<?php endif; ?>
<?php endif; ?>
3. In the last step, I'll keep the content in the existing widget area "title-widget-area", as it is, but in the new "title-widget-area-blog", I'll add the content template shortcode with item="11" attribute, so that it loads the template's content from the page with ID "11" (Wholehearted Blog):
[wpv-post-body view_template="Page Title Template" item="11"]