Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Situation is I have some posts which are showing in my home page there are some read more links(excerpts) after clicking on those links they go to the complete post page, now the issue is i want to open a pdf on one of the read more link, it is not working right now what i have done is I pasted the pdf url on the post page and thought it will open the pdf after clicking it but shows the page with the same url and not opening the pdf I know i am doing wrong what I am supposed to do.

In nutshell how can i open any file such as .doc,image,pdf on read more link (excerpts)

I am using latest wp3.4 tweenty-eleven theme

I am complete beginner so don't laugh over any silly mistake of mine

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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' );
share|improve this answer
    
Function modified to now correctly show output of the excerpt as well as the read more link. –  userabuser Jun 22 '12 at 14:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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