最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wordpress - Post title ignoring context and patterns not working with provided postId and postType context in render - Stack Ove

programmeradmin2浏览0评论

I've setup a basic block that needs to pass down the context of a post to its innerBlocks similar to how the query block does, so that the title, featured image and excerpt blocks work off that new post and not the current page. I'm doing this so I can make use of the same pattern for a "tease" in both query blocks and elsewhere on the site.

I've set this up with a basic useSelect to get all the posts of the type I'm after and am storing the postType and postId attributes. My block.json has providesContext setup and this is correctly passing the context to my test block in the front end and back end. On thsi back end this all seems to work fully with the exception of excerpts not loading, But I'm having some weird issues on the front end I can't work out.

If i add a featured image, excerpt block or published date block, the context is correctly passed and the right image and content loads. But if I add a post-title block, this does not work.

If I make a synced pattern that contains the same set of blocks, none of them work.

I had initially thought that as post-title was using get_the_title() and not making use of it's context I'd need to also setup postdata in my render.php so I added this, but even that doesn't seem to work to setup post data at all.

<?php
$newPost = get_post($attributes['postId']);
setup_postdata( $newPost );
?>
<div <?php echo get_block_wrapper_attributes(); ?>>
    <?php echo get_the_title(); ?>
    <?php echo $content; ?>
</div>
<?php wp_reset_postdata(); ?>

Is my approach here wrong? I can't seem to find much online at all on the subject and the docs are really no help.

I've been trying to debug this and I setup a basic test block that consumes the postId and postType context, this works fine front and back end. But when in a synced pattern that is placed inside this provider block it only gets the postId and postType for the page it is on and not the one from the context.

I've setup a basic block that needs to pass down the context of a post to its innerBlocks similar to how the query block does, so that the title, featured image and excerpt blocks work off that new post and not the current page. I'm doing this so I can make use of the same pattern for a "tease" in both query blocks and elsewhere on the site.

I've set this up with a basic useSelect to get all the posts of the type I'm after and am storing the postType and postId attributes. My block.json has providesContext setup and this is correctly passing the context to my test block in the front end and back end. On thsi back end this all seems to work fully with the exception of excerpts not loading, But I'm having some weird issues on the front end I can't work out.

If i add a featured image, excerpt block or published date block, the context is correctly passed and the right image and content loads. But if I add a post-title block, this does not work.

If I make a synced pattern that contains the same set of blocks, none of them work.

I had initially thought that as post-title was using get_the_title() and not making use of it's context I'd need to also setup postdata in my render.php so I added this, but even that doesn't seem to work to setup post data at all.

<?php
$newPost = get_post($attributes['postId']);
setup_postdata( $newPost );
?>
<div <?php echo get_block_wrapper_attributes(); ?>>
    <?php echo get_the_title(); ?>
    <?php echo $content; ?>
</div>
<?php wp_reset_postdata(); ?>

Is my approach here wrong? I can't seem to find much online at all on the subject and the docs are really no help.

I've been trying to debug this and I setup a basic test block that consumes the postId and postType context, this works fine front and back end. But when in a synced pattern that is placed inside this provider block it only gets the postId and postType for the page it is on and not the one from the context.

Share Improve this question asked Nov 20, 2024 at 18:54 Chris MorrisChris Morris 9832 gold badges18 silver badges41 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

In your render.php, the get_post() function is passed "null", so it retrieves the default post objects title. To resolve your issue, pass $newPost to get_post() to get a specific post title, eg: render.php

<?php
$newPost = get_post($attributes['postId']);
setup_postdata( $newPost );
?>
<div <?php echo get_block_wrapper_attributes(); ?>>
    <?php echo get_the_title($newPost); ?>
    <?php echo $content; ?>
</div>
<?php wp_reset_postdata(); ?>

As a side note, I've previously converted a "featured post" short code into a block, close to the "teaser" concept you describe (a while back now, before block patterns). My approach was similar with the SelectControl in edit.js to select and store the postId as a block attribute. In render.php (or render_callback function) I used a new WP_Query to retrieve the post and feed the post meta into blocks using do_blocks() to render the block content.

Updated Answer for WP 3.7.1

At present, the core/post-title block does not contain the $post object like the post-excerpt and featured-image blocks, which is why they both work recieve the postId context within innerBlocks and post-title does not.

The logic goes that the core/post-title block is to be used within the loop context. I suggest creating a custom variation of the query-loop with your desired pattern so the post title resides inside the query loop. In your variation, define the postType and any other query parameters as needed.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论