1

I would like to tick a box while drafting a post which will determine whether:

a) only the excerpt for the post is shown on the homepage, linking you to the full post by clicking 'read more'

or

b) the full post is shown on the homepage with no 'read more' button available...

Could you please explain how to do this? Thank you.

Tom

0

it looks like that you need to add custom meta box to your post page.

The simplest way it could be to use Advanced Custom Fields, check out Google for some tutorials, but of course you need to install additional plugin. (video tutorial: https://www.youtube.com/watch?v=xtqMatzVoSA )

If you want to achieve that without a plugin you need to add a meta box manually. There is a quite good tutorial which explains how to do this https://www.youtube.com/watch?v=ccbImB59JXQ you just have to modify it a little.

0

You need to add a custom field (radio buttons) to your posts editor. To add custom fields and metaboxes I normally use metabox.io plugin, you can download it and get the documentation of that plugin on their official page: https://metabox.io/

To place a radio button with metabox.io you will have to put something like this in your functions.php file once you've the plugin installed and activated:

<?php //Don't use me if on functions.php

add_filter( 'rwmb_meta_boxes', 'your_prefix_radio_demo' );
function your_prefix_radio_demo( $meta_boxes )
{
    $meta_boxes[] = array(
        'title'  => __( 'Content or Excerpt', 'your-prefix' ),
        'fields' => array(
            array(
                'name'    => __( 'Show Content', 'your-prefix' ),
                'id'      => 'content_or_excerpt',
                'type'    => 'radio',
                'options' => array(
                    'content' => __( 'Show Complete Content', 'your-prefix' ),
                ),
            ),
        )
    );
    return $meta_boxes;
}

Then, you can query all posts and retrieve the value of the radio button in order to show the complete content or the excerpt (this code goes wherever you have your homepage code e.g. index.php, custom template or template part):

$args = array(

'post_type'   => 'post',
'posts_per_page' => 12,

);

$query = new WP_Query( $args );

if($query->have_post()) {   
    while($query->have_posts()) {
        $query->the_post();
        $show_content = rwmb_meta( 'content_or_excerpt' ); //Get the value of the post custom field
        $thumbnail_url = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ) );
?>

            <div class="home_featured_post">
                 <?php the_title('<h2>', '</h2>'); ?>
                 <img src="<?php echo $thumbnail_url; ?>">                          
<?php
                if( $show_content == "content" ) {
                     the_content();
                } else {
                     the_excerpt();
                }
?>
           </div>
<?php       
    }
}

wp_reset_query();

And to make the read more link to link to the post, I took this code from the Codex: https://developer.wordpress.org/reference/functions/the_excerpt/ (This code goes on the functions.php file)

/**
 * Filter the "read more" excerpt string link to the post.
 *
 * @param string $more "Read more" excerpt string.
 * @return string (Maybe) modified "read more" excerpt string.
 */
function wpdocs_excerpt_more( $more ) {
    return sprintf( '<a class="read-more" href="%1$s">%2$s</a>',
        get_permalink( get_the_ID() ),
        __( 'Read More', 'textdomain' )
    );
}
add_filter( 'excerpt_more', 'wpdocs_excerpt_more' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.