Support » Developing with WordPress » Reusable Block in single.php

  • Resolved WorkspacePros

    (@michael-barker)


    I’ve created a reusable block that I’d like to display at the bottom of every article on a website. I’ve been trying to add it to the single.php template but I’ve had no luck. I’ve tried several methods with wp_query but none of it has worked. Does anyone have insight as to how to call a reusable block in a theme / template?

Viewing 8 replies - 1 through 8 (of 8 total)
  • If it’s specific content, a widget area works well.
    But if you really want to use a reusable block, you can call get_posts with the appropriate arguments, or get_post if you know which one you want.
    https://developer.wordpress.org/reference/functions/get_posts/

    Thread Starter WorkspacePros

    (@michael-barker)

    Thanks for the lead, Joy.

    I solved this with the following php:
    $reuse_block = get_post( 123 );
    $reuse_block_content = $reuse_block->post_content;
    echo $reuse_block_content;

    I obtained the post id (‘123’) for the reusable block from the “Manage All Reusable Blocks” screen.

    That seems like a strange thing to hard code into a theme. And it doesn’t run the the filters for the content, so it might not display correctly(no texturized quotes or server side rendering of blocks?) and plugins can’t add to it (not that they should, just saying…).

    Thread Starter WorkspacePros

    (@michael-barker)

    Ah thanks for pointing that out. The reusable block already exists and is used by my client on various pages as a call to action. I don’t need nor want plugins to add to it. The client updates it approx monthly. We decided we wanted to put the same block at the halfway point of each article to drive more conversions. So I would go with “creative solution” as opposed to strange. I made the following correction. Does that ease your concerns?

    corrected:
    $reuse_block_content = apply_filters( 'the_content', $reuse_block->post_content);

    Thanks!
    That’s the final code:

    $reuse_block = get_post( 123 ); // Where 123 is the post id
    $reuse_block_content = apply_filters( 'the_content', $reuse_block->post_content);
    echo $reuse_block_content;

    You have to create a page or post with the blocks you want and insert it on your single.php where you want.

    In my case I have a page named: Single Header and another page called Single Footer

    • This reply was modified 10 months, 3 weeks ago by gtamborero.
    Thread Starter WorkspacePros

    (@michael-barker)

    Not necessarily.

    You can call the actual block by it’s post id without the need for an additional page.

    To get the post id for a reusable block, edit any post (or create a new post) and click the add block button (+). Navigate to reusable blocks and in that menu there is a link to “Manage All Reusable Blocks”. Or you can go directly to the reusable block listing at site.com/wp-admin/edit.php?post_type=wp_block

    In the listing of reusable blocks, you can hover over edit for any block and you can see the post ID in the link address in your status bar. Then you can call the block only without a new page using the code we detailed earlier.

    Thanks @michael-barker ! That’s awesome!

    Thread Starter WorkspacePros

    (@michael-barker)

    No problem. It’s nice to help someone else on here for a change!

    MB

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Reusable Block in single.php’ is closed to new replies.