0

I want to to show a specific content on the top of each post (on single page) depending of the post-type.

If it's normal, show something, if it's gallery, show another.

By now the code is like this:

<?php
    if  (has_post_format( 'post-format-normal' , $post_id )) {
        <div class="post-image-section section" style="background-image: url(<?php echo $single_post_image_url ?>);">
            <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">
            <?php the_post_thumbnail('single-post-image'); ?>
            <?php if ( !empty(get_post(get_post_thumbnail_id())->post_excerpt) ) : ?>
                <div class="flex-caption-container">
                    <p class="flex-caption"><?php echo get_post(get_post_thumbnail_id())->post_excerpt; ?></p>
                </div>
            <?php endif; ?>
            </a>
        </div>
    } elseif ( has_post_format( 'post-format-gallery' , $post_id )) {
        <?php flexslider('index-post-image'); ?>            
    }
?>

But it's not working, looks like there is a typing error on the code... I want that if the post type is gallery, to show the flexslider, and if it's normal, show the post_thumbnail...

How to achieve that?

  • Where does 'post-format-normal' come from? – Chip Bennett Oct 3 '13 at 0:41
  • I believe thats the default post format type? If the author has not selected anyother else i guess that this is the one choosen, like a default – lucasmx Oct 3 '13 at 0:45
  • @lucasmx: Default post_types are post & page, not "normal". Details Here :( – Mayeenul Islam Oct 3 '13 at 1:08
  • From your comment below: "the problem im having it that because the conditional content i wish to use has HTML and PHP codes so when i try to render the website says that there is errors on the code": can you please edit your question to include the error message text? Very likely, these are just PHP syntax errors (outside WPSE scope). Strip out the code down to the conditional statements, and make sure they're working; then add back your content piece-by-piece. – Chip Bennett Oct 3 '13 at 11:42
  • Chip the error starts at the first line of the code... i already managed to fix this, thank you – lucasmx Oct 14 '13 at 17:58
1

There is no "default" post format type, though internally in core, a post that has no post format assigned is usually referred to as standard. So, your first conditional will return false using post-format-normal because there is no such post format - but would still return false using post-format-standard.

You could reverse the conditional, and check for gallery post format first. Note that you could use either 'gallery' == get_post_format() or has_post_format( 'post-format-gallery' ). (Personally, I find get_post_format() to be a bit more intuitive.)

In any case, you would reverse the conditional like so:

if ( has_post_format( 'post-format-gallery' ) ) {
    flexslider('index-post-image');       
} else {
    // Fallback output here
}

Note that you also have syntax errors in your code, such as nested PHP tags.

  • The problem im having it that because the conditional content i wish to use has HTML and PHP codes so when i try to render the website says that there is errors on the code – lucasmx Oct 3 '13 at 1:17
0

First of all your problem I think is $postid variable, where are you setting it?

The code should be putted after the_post();.

After that consider that post format are just taxonomies, so even if you can use, for sure, has_post_format() if you want to show one different thing for every format probably is better use just get_the_terms(edited after @ChipBennett comment) get_post_format with a switch, but this is just an opinion.

You have also to consider that (as said in the above linked answer) there is no post format 'normal': a post format normal means no post format.

If you have some html/php mixed content can be hard and less readable correctly write code in the condition statement, so why dont create some template parts and use them?

Something like:

if ( have_posts() ) : while ( have_posts() ) : the_post();

  get_template_part( 'before-single', get_post_format() );

endwhile; endif;

After that you can create in your theme (or child theme) folder the files:

  • before-single.php
  • before-single-post-format-chat.php
  • before-single-post-format-aside.php
  • before-single-post-format-link.php
  • before-single-post-format-chat.php
  • before-single-post-format-audio.php
  • before-single-post-format-video.php
  • before-single-post-format-image.php
  • before-single-post-format-quote.php
  • before-single-post-format-status.php

Of course, you are not required to create all of this files, when the file for the current post format does not exist, the file before-single.php will be used.

In thes files you can freely use all the html and php code you want...

  • 2
    Why not just use get_post_format() for $format? – Chip Bennett Oct 3 '13 at 0:57
  • Because I didn't remember it exists.. :) (ok, I admit I hate post format and never use them, so I'm not familiar with the related functions) @ChipBennett – gmazzap Oct 3 '13 at 1:00
  • nice, but the problem im having it that because the conditional content i wish to use has HTML and PHP codes so when i try to render the website says that there is errors on the code – lucasmx Oct 3 '13 at 1:25
  • @lucasmx you can out html and php code inside the conditional statement, but is a bit harder and less readable, so I think is better create some template files and use them to write you content. See the edit on my answer. – gmazzap Oct 3 '13 at 1:37
0

Ok guys, thank you for all the help, As i said the problem was PHP syntax which was causing render errors. As i was mixing PHP with HTML content i didnt knew how to properly write.

Here is the solution i found:

<?php  if( has_post_format( 'gallery' )) {  
flexslider('index-post-image'); 
}  else {?>  
<div class="post-image-section section" style="background-image: url(<?php echo $single_post_image_url ?>);">
            <a href="$the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">
            <?php the_post_thumbnail('single-post-image') ?>
            <?php if ( !empty(get_post(get_post_thumbnail_id())->post_excerpt) ) : ?>
                <div class="flex-caption-container">
                    <p class="flex-caption"><?php echo get_post(get_post_thumbnail_id())->post_excerpt; ?></p>
                </div>
            <?php endif; ?>
            </a>
        </div>
<?php  }?> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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