Return to Support

Use WP Custom Fields to change Read More button text?

Home Forums Graphene WordPress theme Support Use WP Custom Fields to change Read More button text?

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #10637
    gaiiden
    Member

    I love your theme, works great for my site (www.kerbalspace.agency). Was just writing up a post and wanted it to be broken up into a section of pertinent information to display on the main page followed by a more detailed section to come after a Read More tag. But I also want to tell people what’s in the section. I feel awkward putting a sentence at the end of the first section saying “Read on for more details about the flight data analysis” because in the actual article (when not viewed from the front page) the Read More link isn’t there. So that made me think if there was a way to use the WP post Custom Fields to define a caption for the Read More button, so instead of just saying “Read the rest of this entry »” by default I could tell it to say “Read more on the Flight Data Analysis »” instead. Or anything else I want to specify.

    I’m a programmer but not yet well-versed in PHP and coding stuff for WP themes. Just let me know if you think something like this would be possible to setup with your theme, and I’ll figure out how to do it. Thanks!

    #46548
    Syahir Hakim
    Keymaster

    if you just would like to change the wording on all the posts, you can use the code as detailed here:

    https://forum.graphene-theme.com/graphene-support/change-text-category-archive-on-category-pages-to-be-the-category-title#post-10145

    If you would like a more granular control, e.g. different wording for different posts, as you suggested you would need to create a custom field for that. Then, you’ll need to modify the suggested code linked to above to get the custom field value and use that instead.

    #46549
    gaiiden
    Member

    hrm… I didn’t understand how that code you linked to related to the “Read the rest of this entry” button, so I decided to brute-force it and just did a search to find out where the button text was being applied and modified the loop.php file to be

    <?php
    $moretext = get_post_meta(get_the_ID(), 'more text', true);
    if (strlen($moretext)) {
    the_content( '<span class="block-button">' . __( $moretext . ' &raquo;', 'graphene' ) . '</span>' );
    } else {
    the_content( '<span class="block-button">' . __( 'Read the rest of this entry &raquo;', 'graphene' ) . '</span>' );
    } ?>

    And that works great, but yea I’m modifying your base code so that’s not really the most elegant of solutions I know. But hopefully you have a better idea of what I’m asking.

    Or perhaps you do understand and I just need a better idea of how to modify that code snippet you linked me to, because I really can’t figure it out.

    #46550
    Syahir Hakim
    Keymaster

    You can do something like this:

    function graphene_filter_gettext( $translated, $original, $domain ) {

    if ( is_singular() ){
    $moretext = get_post_meta( get_the_ID(), 'more text', true );
    if ( $moretext ) {
    $strings = array(
    'Read the rest of this entry »' => $moretext,
    );
    }
    }

    if ( ! empty( $strings[$original] ) ) {
    $translations = &get_translations_for_domain( $domain );
    $translated = $translations->translate( $strings[$original] );
    }
    return $translated;
    }
    add_filter( 'gettext', 'graphene_filter_gettext', 10, 3 );

    #46551
    gaiiden
    Member

    thanks, that helps. Much appreciated

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

You must be logged in to reply to this topic.