wp attachment page not displaying media

2019-07-31 03:50发布

I am building my first Wordpress theme. I am having trouble with media attachment. When I upload and image, and insert it into the post, it shows the image. If I link it to "Media File" then it also takes me to the file when clicked upon. But if I try to link to "attachment-page" it does not show the image. I am NOT using a custom 'attachment.php' instead I'm only using the default 'single.php'. Here is the code of single.php:

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

    <div class="post" id="post-<?php the_ID(); ?>">
        <div>
        <h2><?php the_title(); ?></h2>

        <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>

        <div class="entry">

            <?php the_content(); ?>

            <?php wp_link_pages(array('before' => 'Pages: ', 'next_or_number' => 'number')); ?>

        </div>

        <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>

    </div>
    </div>
    <?php // comments_template(); ?>

    <?php endwhile; endif; ?>


</div> <!--main-content Ends--> 

标签: wordpress
3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-31 04:30

I would recommend to add the jetpack plugin and use some of its build-in functionalities for handling images! Not only they are going to solve your problem, but you will have more options like carousel or mosaic view.

查看更多
贼婆χ
3楼-- · 2019-07-31 04:31

Your question is not clear enough!!

Well when you upload insert an image in to a post it automatically links to that image some thing like this:

<a href="http://localhost/wordpress_works/Cassiopeia-Bouviers/wp-content/uploads/2013/11/1.png">
    <img class="alignnone size-medium wp-image-86" alt="1" src="http://localhost/wordpress_works/Cassiopeia-Bouviers/wp-content/uploads/2013/11/1-300x175.png" width="300" height="175" />
</a>

But you can give your custom link by editing the default link!

查看更多
在下西门庆
4楼-- · 2019-07-31 04:43

@Zeeshan

Your single.php document does not appear to have the proper PHP code to output the attachments. Try adding the following code within the loop, wherever you'd like the attachment content to show up:

<?php if(wp_attachment_is_image($post->id)) : $att_image = wp_get_attachment_image_src($post->id, "full"); ?>
  <p class="attachment"><a class="show_image" href="<?php echo wp_get_attachment_url($post->id); ?>" title="<?php echo get_the_title(); ?>"><img class="img-responsive attachment-medium" src="<?php echo $att_image[0]; ?>" height="<?php echo $att_image[2];?>" width="<?php echo $att_image[1];?>" alt="<?php echo get_the_title(); ?>"></a></p>
<?php else : ?>
    <a href="<?php echo wp_get_attachment_url($post->ID) ?>" title="<?php echo wp_specialchars( get_the_title($post->ID), 1 ) ?>"><?php echo basename($post->guid) ?></a>
<?php endif; ?>

This code works with the latest 3.x CORE but also has a fallback for older versions.

If provided, the_content() function outputs the media description.

Attachment pages range from using simply attachment.php file to almost a dozen other template files.

http://codex.wordpress.org/Template_Hierarchy#Attachment_display

Attachment display

Template file used to render a single attachment (attachment post-type) page.

  1. MIME_type.php - it can be any MIME type (image.php, video.php, application.php). For text/plain, in order:
    1. text.php
    2. plain.php
    3. text_plain.php
  2. attachment.php
  3. single-attachment.php
  4. single.php
  5. index.php

You can have mime type specific templates for attachments. For example, over at my blog I have a snapshot of a JavaScript Meetup using the mime type specific image.php attachment template that handles image attachments.

So if your dealing with images, then I suggest you create an image.php file and drop that into your themes folder and get to work.

查看更多
登录 后发表回答