• 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!

Viewing 1 replies (of 1 total)
  • Hello @tedheywood,

    I’m afraid this is custom coding and as such out of our scope.

    However, I can tell you that your filter function is incorrect:
    add_filter( 'oceanwp_excerpt', 'do_shortcode');

    do_shortcode will do absolutely nothing here since it’s applied incorrectly.

    Instead of the do_shortcode, replace it with the name of the custom function (for your own excerpt) you would like to use to override the default excerpt.

    Hope this helps

Viewing 1 replies (of 1 total)
  • The topic ‘Shortcode Of Custom Field Value Inside Excerpt’ is closed to new replies.