Support

Account

Home Forums General Issues use a ACF to replace post title in this code

Solved

use a ACF to replace post title in this code

  • I am using a map plugin, and each cpt post is a location that is mapped on a map. The map markers can be clicked which pops open an small info box. The issue is the code below uses the post title in the pop up box, which these titles are longer than I would like to display in these small pop up boxes. I would like to shorten the titles. I figured I could use an advanced custom field to use a shorter version of a page title, and swap out the page title with the advanced custom field value, and still have it be clickable as a permalink (just as the post title was). Here is the existing code:

    add_filter(
    'wp_grid_builder_map/marker_content',
    function( $content, $marker ) {
    
    ob_start();
    
    $url = wp_get_attachment_url( get_post_thumbnail_id() );
    
    if ( $url ) {
    
    echo '<div class="wpgb-map-marker-img">';
    echo '<div style="background-image:url(' . esc_url( $url ) . ');"></div>';
    echo '</div>';
    
    }
    
    echo '<div class="wpgb-map-marker-body">';
    
    the_title(
    '<h3 class="wpgb-map-marker-title"><a href="' . esc_url( get_the_permalink() ) . '">',
    '</a></h3>'
    );
    
    echo '</div>';
    
    return ob_get_clean();
    
    },
    10,
    2
    );

    does anyone know how to accomplish this? (I am not a coder)

  • @billy_mac – you should be able to replace the line:

    the_title(
    '<h3 class="wpgb-map-marker-title"><a href="' . esc_url( get_the_permalink() ) . '">',
    '</a></h3>'
    );

    with:

    echo '<h3 class="wpgb-map-marker-title"><a href="' . esc_url( get_the_permalink() ) . '">'. get_field( 'FIELDNAMEHERE' ) .'</a></h3>';

    just make sure FIELDNAMEHERE is changed to the field name you setup. Depending on your page template setup, this should work out of the box.

    Otherwise, you may need to configure the get_field() a bit differently (see reference link)

    — the_title() documentation shows a bit of how it formats in correlation with the Post/Page Title, so basically, removing that function and echo-ing it in without the assistance. See Reference

  • that worked perfectly. Thank You!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.