Archived topic

customize sidebar based on post

1 reply · Started by Conor on October 31, 2019

Viewing posts 1–2 of 2

Hi,

I plan to talk a lot about different books in my blog, and I am wondering if I can customize my sidebar based on the post. So if I was talking about a certain book, that book would be on the sidebar.

In other words, can I create a customized sidebar for each post? At this point, I want the popular post widget I have to be there every post, and I want the tag cloud there, but I also want to add an image of a book to the sidebar for certain posts.

Cheers,
C

Hi there,

you could use WordPress Custom Fields to add the Image URL and display it in the sidebar using a Shortcode. So something like this:

1. Add this PHP snippet to create the shortcode function:

function db_show_book_cover() {
    global $post;
    $book = get_post_meta($post->ID, "book", true);
    if ( $book ) {
        ob_start();
        printf( '<img class="book-cover" src="%s">',
            $book
        );
        return ob_get_clean();
    }
}
add_shortcode('book_cover', 'db_show_book_cover');

Adding PHP: https://docs.generatepress.com/article/adding-php/

2. Add the shortcode [book_cover] to a HTML widget in your sidebar.

3. In the post editor create new custom field with a Name of book and then add the full URL to the image in the Value field.

This archived topic is closed to new replies.