Archived topic
How to display content of page/post in sidebar?
7 replies · Started by Andrew on July 26, 2021
How can I display the content of a page or post in a sidebar?
The use case is that I have some custom content I display in sidebars. Currently I have one element for each sidebar, on the "generate_before_left_sidebar_content" and "generate_before_right_sidebar_content" hooks. When I want to edit the content, I hard-code the appropriate HTML in the element editor. But this is cumbersome and error prone.
It would be much more convenient to store the content as a page or post, and then use a PHP call or shortcode in an element (or some other mechanism) to display the content of the page/post in the sidebars. That way, when I want to update the content, I can edit it in the block editor.
Is this possible? If so, how?
Note: By "content," I mean just the blocks themselves without any of the wrappers (i.e., no header, footer, sidebars, meta, etc.).
Hi Andrew,
Not 100% sure if I understand but does this help?
https://docs.generatepress.com/article/dynamic-blocks/#post-content
Thanks. That might do the trick. I need to play with it a bit to see.
No problem :)
Actually it looks like this solution won't work for me.
The reason is a small incompatibility with another plugin that I use, which I rely on to add the "rel='nofollow'" tag to links. This plugin works perfectly well when I am editing blocks in the post editor, but it does not work when I am editing blocks in the element editor.
So back to my original question, which I have edited to (hopefully) make it clearer. How do I display the content of a post in the sidebar? I'm looking for something like a PHP or shortcode call that would grab the content of a specific post.
Thanks.
Hmm unfortunately this will require a custom solution that is outside the scope of this forum.
Since it's not a theme-specific question either, you could try asking on a WordPress general forum like this:
https://wordpress.stackexchange.com/
Or you can try asking WordPress support team as well. If they are able to provide a solution for you with a Twenty series theme, the exact the method should work in GP.
Ha ha thanks. Anyway, I figured it out:
<?php
$post = get_post ( $post = 9999 );
return $post->post_content;
?>
Where 9999 = the post ID. And return may have to be echo, depending on context.
Glad to hear :)