admin管理员组文章数量:1291163
I want to create a shortcode that pulls value from a custom field of a post and returns that value inside excerpt.
I already have a function for the custom value:
function geo_name_function( $atts ) {
$atts = shortcode_atts( array(
'post_id' => get_the_ID(),
), $atts, 'geo_name' );
return get_post_meta( $atts['post_id'], 'field_name', true );
}
add_shortcode('geo_name', 'geo_name_function');
And filter for the title:
add_filter( 'the_title', function( $geo_name_function ) {
return do_shortcode( $geo_name_function );
});
add_filter( 'the_title', 'se385007_title_filter', 20, 2 );
function se385007_title_filter( $title, $post_id )
{
$new_title = str_replace( "[geo_name]", "[geo_name post_id=$post_id]", $title );
return do_shortcode( $new_title );
)
I thought that by adding the Excerpt filter:
add_filter( 'oceanwp_excerpt', 'do_shortcode');
It will fix it but it did not.
I tried adding few other filters but just ended up breaking WordPress ).
At the moment if Custom Value is Ice Cream. If my excerpt is Favorite [geo_name] it only returns Favorite.
Please assist!
I want to create a shortcode that pulls value from a custom field of a post and returns that value inside excerpt.
I already have a function for the custom value:
function geo_name_function( $atts ) {
$atts = shortcode_atts( array(
'post_id' => get_the_ID(),
), $atts, 'geo_name' );
return get_post_meta( $atts['post_id'], 'field_name', true );
}
add_shortcode('geo_name', 'geo_name_function');
And filter for the title:
add_filter( 'the_title', function( $geo_name_function ) {
return do_shortcode( $geo_name_function );
});
add_filter( 'the_title', 'se385007_title_filter', 20, 2 );
function se385007_title_filter( $title, $post_id )
{
$new_title = str_replace( "[geo_name]", "[geo_name post_id=$post_id]", $title );
return do_shortcode( $new_title );
)
I thought that by adding the Excerpt filter:
add_filter( 'oceanwp_excerpt', 'do_shortcode');
It will fix it but it did not.
I tried adding few other filters but just ended up breaking WordPress ).
At the moment if Custom Value is Ice Cream. If my excerpt is Favorite [geo_name] it only returns Favorite.
Please assist!
Share Improve this question asked Jun 15, 2021 at 0:39 DiscoverDiscover 331 gold badge1 silver badge6 bronze badges 1 |1 Answer
Reset to default 0Even though this is specific for OceanWP, the solution is plain Wordpress.
Add these two lines to the end of your functions.php
to render the shortcodes in both the_excerpt()
and get_the_excerpt()
.
add_filter( 'the_excerpt', 'do_shortcode' );
add_filter('get_the_excerpt', 'do_shortcode');
本文标签: Shortcode To Display Post Custom Field Value Inside Excerpt
版权声明:本文标题:Shortcode To Display Post Custom Field Value Inside Excerpt 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741503810a2382202.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
oceanwp_excerpt
is not a core WordPress hook. This question may require specific knowledge about whatever that third-party product is in order to answer, in which case it might be considered off-topic for our stack. Your best bet may be to pose the question in the product's official support channels. – bosco Commented Jun 15, 2021 at 3:39