How to customize read more link

There are a number of ways this can be achieved, some more complex than others, some offering more functionality than others, however it all depends on your usage-case.

For now, the most simple method is to use a combination of,

Step 1)

In your post edit screen you need to create a custom field. If the custom field box is not visible to you in the post edit screen then click on screen options at the top right of your screen and then check the Custom Fields checkbox after which the Custom Fields meta-box will appear on screen.

Refer to the screenshot below if necessary.

enter image description here

Within the Custom Field meta-box click Enter New and for the name (meta key) of your value (meta value) enter something like read_pdf.

Then enter the link to your PDF attachment in the adjacent value field.

Refer to the screenshot below if necessary.

enter image description here

Step 2)

Next in your theme file, the one in which controls the display of your posts, which may be your index.php file or your archive.php (or something else) you need to look for your post loop and replace the current read more link with a conditional statement.

Example;

 <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <?php the_title(); ?>

        <?php the_excerpt(); ?>

        <?php if ( get_post_meta($post->ID, 'read_pdf', true) ) : ?>

          //replace read more link to value of read_pdf key if exists

          <a href="<?php echo get_post_meta($post->ID, 'pdf_key', true) ); ?>">
          read more
          </a>

        <?php else : ?>

          //show normal read more link if no pdf attached.

          <a href="<?php the_permalink(); ?>">
          read more
          </a>

        <?php endif; ?>
    
  <?php endwhile; endif; ?>

The code above is an example of how your template files may look, but I can’t know for sure. However the purpose here is to illustrate how to use Custom Fields in their basic form to set a link to a document, then have that value replace the standard read more link when it exists via a conditional if/else statement.

NOTE: Once you enter the key of “read_pdf” once, you do not have to keep on re-entering it. Instead you will be able to select it from a drop down box in the Custom Field meta-box from then onward.

UPDATE

After further dicussion with the OP above, the solution used for the Twenty Eleven theme is that of below, however the solution above is also applicable as a general approach for themes that do not control the output of their excerpts via the functions.php file.

In your functions.php file on line 364 (Twenty Eleven Theme v1.4) replace the function with;

function twentyeleven_custom_excerpt_more( $output ) {

    global $post;

    if ( get_post_meta($post->ID, 'read_pdf', true) ) {

        $output .= '<a href="'.get_post_meta($post->ID, 'read_pdf', true) .'">read more</a>';
        return $output;

        }
        else
        {
            if ( has_excerpt() && ! is_attachment() ) {
                $output .= twentyeleven_continue_reading_link();
            }
            return $output;

        }

}
add_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );

Leave a Comment

error code: 523