• Resolved justwander

    (@justwander)


    Hello,
    On my artist website I would like to use an attachment page in order to show more information about works from my gallery pages. I did and named it attachment.php. But ran into a bit of a problem.

    This is what I found and how things stand thus far:
    1 The file page.php is used by the site instead of attachment.php.
    2 It does give the same function as an attachment file but I want to make some changes and page.php can be used other places.
    3 I built an attachment.php file (well, I copied page.php and renamed it attachment.php).
    4 The changes I made work well, the Comments dialog box is now in the sidebar.
    5 The problem: the image I want to show not longer appears.

    Here is the code that I think is where the problem is:

      <?php if(have_posts()):?>
          <?php while(have_posts()): the_post();
            get_template_part('inc/theme/views/content-page'); ?>
            ****have posts****<br>
          <?php endwhile;
      else:?>
          ****no posts****<br>
          <?php get_template_part('inc/theme/views/content-none'); 
      endif; ?>

    You can see on the webpage I am referencing that I put markers in the code. In the snippet above the “have posts” note shows and “no posts” does not.

    Looking at the functions involved [(have_posts() and get_template_part()] didn’t help because I don’t really understand what I am reading.

    Maybe it would be easier to test the webpage as it loads to see if it is an attachment page. I would have to skip over the Comments and the sidebar call in the body of the page and put them to the sidebar. The problem is I don’t know what to test for.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m not familiar with your theme. Some themes can behave in peculiar ways. Users are usually better off resolving theme issues through the theme’s dedicated support forum. I realize many themes are not as well supported as we would like, so I’ll try to help you despite my limited understanding.

    I suspect the template part being loaded (‘inc/theme/views/content-page’) is not appropriate for attachment queries, it is intended for regular page content. Try commenting out the template part call and adding this line below:
    <?php echo wp_get_attachment_image( get_the_ID(), 'large'); ?>

    You can pass another registered size besides ‘large’ if need be. Large is usually a good size, but varies by theme. ‘full’ is the original upload size, which is often too large. If your attachments include other media besides images, this code will be inappropriate. The template can be renamed to image.php so it is only applied to image MIME type attachments, others will probably use single.php.

    The resulting page style will probably not be correct, and other elements will be missing, like the title and meta data, but that can be addressed later. The point is to simply confirm the right image is displayed using your template. If that checks out, the next step is to replace the template part call with the entire content of inc/theme/views/content-page.php. I’m guessing the problem line from that file is <?php the_content(); ?>. Replace that line with my get attachment image line and your template should be pretty close to what you want. Some CSS adjustments may be necessary though.

    Just in case you are unaware of child themes and are adding your template to your theme’s folder, you shouldn’t do that. When the theme is updated, any added files will be deleted and custom code overwritten. Create a child theme to contain your custom work to protect it from updates.

    Thread Starter justwander

    (@justwander)

    @bcworkz, thanks for the reply.

    I was able to trace the problem to the content-page.php file but it wasn’t what you thought. <?php the_content(); ?> brings in the description that is entered when you first put the image into the media library. I use that on the attachment page.

    This line, <?php writee_featured_image(get_the_ID(), ‘WRT-post-image’); ?>, appears just before the content call and is supposed to call the image. It does not work when I copy the page.php file and rename it attachment.php. I suppose one of the functions used on that page refer back and then can’t find it.

    So I copied the content-page.php file and renamed it content-attachment.php. I put in the image call you suggested.

    In the attachment.php file I changed the call to the content page to content-attachment.php.

    The original page.php file was left alone.

    I realized it all would have been a bit more elegant to put if statements in page.php and content-page.php but I couldn’t figure out what to test for. This works just fine and I moved on to the next problem.

    Thanks for pointing me in the right direction.

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. Well done!

    FYI, there are a slew of “template tag” functions taking the form “is_{$property}” that can be used in conditional statements. Functions like is_home(), is_page(), is_category(), etc. that reflect the current request and return true or false. For this situation, we have is_attachment() and wp_attachment_is_image().

    Having separate templates carries other benefits though, so conditionals are not always the preferable approach. Primarily, you can easily alter the HTML of a specific template where using conditionals could become cumbersome, tedious, or redundant. Maybe you don’t need the flexibility now, but at least the option is available for the future.

    Thread Starter justwander

    (@justwander)

    Thanks for the heads up.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Image will not appear on attachment page’ is closed to new replies.