Site logo

Archived topic

Can a Page featured image show at the top of the Right Sidebar?

11 replies · Started by Jonathon on March 23, 2022

Viewing posts 1–12 of 12

I'm trying to find a way to display a pages assigned featured image not at the top of the page, but inside the right sidebar. I love the separated containers in the sidebar and would like put a portrait there based on the page being visited. Can that be done with a child theme? I couldn't find any plugins that would do it for me.

Jonathon

It is actually just a local copy at the moment. I am attaching a dropbox housed screenshot of what a page looks like. I want the picture to be displayed in the sidebar area instead of inside the page.

Hi Jonathon,

You can use a block element - hook to achieve this, make sure you have GenerateBlocks plugin installed.
https://docs.generatepress.com/article/block-element-overview/
https://docs.generatepress.com/article/block-element-hook/

1. You can add a Dynamic image block (the icon is GP logo) to the block element, set it to dynamically show the featured image.

2. Select before_right_sidebar_content as hook name.

3. Select location depends on your needs.

4.Disable the default featured image at customizer > layout > blog.

Thank you. That is perfect. Is there a way to lock this into a subset of pages? Say all pages under the Speaker page for example?

Jonathon

Hi there,

are these Child Pages ? And do each of them have their own featured image ?

Hi David,

Yes these will be child pages of a page called Speakers which will hopefully have a grid of all the children on it. Each child speaker page will have a featured image of the speaker.

I was using a sidebar plugin, but your elements are really close to replacing it. The only trick is figuring out a clean way of setting a lot of pages to use an element but not all or one.

Jonathon

So for now you need some code to set an element to appear on children pages. We're working on that as an option in the display locations. And this is the PHP Snippet you will need:

add_filter( 'generate_element_display', function( $display, $element_id ) {
    global $post;
    if ( 100 === $element_id && 123 == $post->post_parent ) {
        $display = true;
    }

    return $display;
}, 10, 2 );

the 100 is the ID of the Element and the 123 is the ID of the parent page. You need to update those accordingly.

This snippet seems to break the backend with calls for a post_parent that doesn't exist. I copied it into the functions.php of my child theme. Do I need to wrap it in another function that checks where it is? Or put it somewhere else? Or do you all use code snippets for these adds instead of directly in a child theme?

( ! ) Warning: Attempt to read property "post_parent" on null in sitename/app/public/wp-content/themes/generatepress_child/functions.php on line 22

Sorry for all the questions and troubles.

Jonathon

Try this:

add_filter( 'generate_element_display', function( $display, $element_id ) {
    
    if ( is_page() && 100 === $element_id ) {
        global $post;
        if ( 123 == $post->post_parent ) {
            $display = true;
        }
    }
    return $display;

}, 10, 2 );

Thank you. That worked great. When you all add this as a built in option will I need to remove this code?

It will be an option in the Elements Display Rules - so once we add it, you could remove that code and enable it in the element.

Glad to be of help

This archived topic is closed to new replies.