0

In Wordpress single.php post pages are both for media attachments (e.g. http://domain.se/postname/imagename/) and blog post pages.

What conditional tag can I use to determine if a post is not a media attachment but a normal blog post?

The reason why I want to determine this is because at the moment I have customized the single.php file to with additional fields that I need for the single post blogposts and as it is now the customized fields show up empty on the media attachment pages too which looks bad.

2 Answers 2

0

Most of the time you want to customize the output of different post (post, page, attachment or custom post type), you will want to use specific template files as it is easier to maintain. For example:

  • single.php for standard post and defualt template for other post types with no specific tempalte file
  • attachment.php for media attachements
  • image.php for image attachments
  • page.php for pages
  • custom-type.php for custom-type post

More info in Wordpress template hierarchy.

Of course, you can also use conditional tags as suggested in the other answer or suggested by you in your question.

if( is_single() ) {
     //This is a post of any type except attachment or page
}

if( is_attachment() ) {
     //This is an attachment
}

if( is_page() ) {
      //This is a page
}

if( is_singular( 'post' ) ) {
      //This is a post (core standard post type)
}

if( is_singular( 'custom-type' ) ) {
      //This is a custom-type post
}
0

You can make use of a conditional tag, in this case is_singular() would be fitting:

is_singular('post')

Returns true when viewing a regular post, because the $post_types parameter is set to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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