op1001
op1001

Reputation: 348

Wordpress - If elseif else statement - Can't figure out mistake

So I have been trying to have a index.php do three things.

If there is a thumbnail to show the thumbnail and do the specific styling.

if there isn't a thumbnail do something different

and the last statement will have if the page is singular it will show something else.

For some reason it keeps failing. I've seen some demos on here, but i can't figure it out

<?php /** this is the first if statement **/?>



<?php         if ( is_home () || is_category() || is_archive() ): ?>

  <?php if ( has_post_thumbnail() ) { ?>
    <article class="blog-card" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">
            <?php optz_post_thumbnail(); ?>

        <?php
        if ( is_singular() ) :
            the_title( '<h1 class="entry-title">', '</h1>' );
        else :
            the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
        endif;

        if ( 'post' === get_post_type() ) : ?>
        <div class="entry-meta">

        </div><!-- .entry-meta -->
        <?php
        endif; ?>
    </header><!-- .entry-header -->

    <div class="entry-content">
      <?php

    the_excerpt( sprintf(
                wp_kses(
                    /* translators: %s: Name of current post. Only visible to screen readers */
                    __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'optz' ),
                    array(
                        'span' => array(
                            'class' => array(),
                        ),
                    )
                ),
                get_the_title()
            ) );

            wp_link_pages( array(
                'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'optz' ),
                'after'  => '</div>',
            ) );
?>
        </div><!-- .entry-content -->
        <footer class="entry-footer">
        <?php
                optz_posted_on();
                optz_posted_by();
            ?>
        <?php optz_entry_footer(); ?>
    </footer><!-- .entry-footer -->
</article><!-- #post-<?php the_ID(); ?> -->



<?php /** this is the first second else statement **/  elseif (is_home () || is_category() || is_archive()) :>





<article class="blog-card" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">

        <?php
            the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
        if ( 'post' === get_post_type() ) : ?>
        <div class="entry-meta">

        </div><!-- .entry-meta -->
        <?php
        endif; ?>
    </header><!-- .entry-header -->
<div class="entry-content">
     <?php
            the_content( sprintf(
                wp_kses(
                    /* translators: %s: Name of current post. Only visible to screen readers */
                    __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'optz' ),
                    array(
                        'span' => array(
                            'class' => array(),
                        ),
                    )
                ),
                get_the_title()
            ) );

            wp_link_pages( array(
                'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'optz' ),
                'after'  => '</div>',
            ) );
            ?>

            </div><!-- .entry-content -->
<footer class="entry-footer">
        <?php
                optz_posted_on();
                optz_posted_by();
            ?>
        <?php optz_entry_footer(); ?>
    </footer><!-- .entry-footer -->


          </article><!-- #post-<?php the_ID(); ?> -->
<p>Nothing to see</p>
**Updated code:**
<?php         if ( is_home () || is_category() || is_archive() ) { ?>

  <?php if ( has_post_thumbnail() ) { ?>
    <article class="blog-card" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">
            <?php anasa_post_thumbnail(); ?>

        <?php
        if ( is_singular() ) :
            the_title( '<h1 class="entry-title">', '</h1>' );
        else :
            the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
        endif;

        if ( 'post' === get_post_type() ) : ?>
        <div class="entry-meta">

        </div><!-- .entry-meta -->
        <?php
        endif; ?>
    </header><!-- .entry-header -->

    <div class="entry-content">
      <?php

    the_excerpt( sprintf(
                wp_kses(
                    /* translators: %s: Name of current post. Only visible to screen readers */
                    __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'anasa' ),
                    array(
                        'span' => array(
                            'class' => array(),
                        ),
                    )
                ),
                get_the_title()
            ) );

            wp_link_pages( array(
                'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'anasa' ),
                'after'  => '</div>',
            ) );
?>
        </div><!-- .entry-content -->
        <footer class="entry-footer">
        <?php
                anasa_posted_on();
                anasa_posted_by();
            ?>
        <?php anasa_entry_footer(); ?>
    </footer><!-- .entry-footer -->
</article><!-- #post-<?php the_ID(); ?> -->


<?php /** this is the second else statement **/ } elseif (is_home () || is_category() || is_archive()) {?>
<article class="blog-card" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">

        <?php
            the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
        if ( 'post' === get_post_type() ) : ?>
        <div class="entry-meta">

        </div><!-- .entry-meta -->
        <?php
        endif; ?>
    </header><!-- .entry-header -->
 <div class="entry-content">
     <?php
            the_content( sprintf(
                wp_kses(
                    /* translators: %s: Name of current post. Only visible to screen readers */
                    __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'anasa' ),
                    array(
                        'span' => array(
                            'class' => array(),
                        ),
                    )
                ),
                get_the_title()
            ) );

            wp_link_pages( array(
                'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'anasa' ),
                'after'  => '</div>',
            ) );
            ?>

            </div><!-- .entry-content -->
<footer class="entry-footer">
        <?php
                anasa_posted_on();
                anasa_posted_by();
            ?>
        <?php anasa_entry_footer(); ?>
    </footer><!-- .entry-footer -->


          </article><!-- #post-<?php the_ID(); ?> -->


<?php /** this is the last if else statement **/ } else {?>

<?php 

    if ( is_singular()) {
   echo'hi';

    }?>



<?php }} ?> <!-- this is the main php thumbnail close tag --!>

Upvotes: 0

Views: 399

Answers (3)

eja
eja

Reputation: 5329

Are you missing a "?" in your php closing tag? On the line just after the elseif, where your comment says: "this is the first second else statement"

<?php elseif (is_home () || is_category() || is_archive()) :>

You also have both a colon and a curly bracket on else on the line where your other comment says: this is the first last if else statement

<?php /** this is the first last if else statement **/ }else :{?>

Also a suggestion, if you write PHP together with Html (within views), many people use colons instead of curly brackets, so the code is easier to look into. Either way, whatever you prefer more, but I think you should stick to one of those two.

Your statements ingeneral are okay. If you tweak all the statements to use either : (colons) or {, it should all work fine.

Upvotes: 1

Andrew Schultz
Andrew Schultz

Reputation: 4243

You have an error here

<?php /** this is the first last if else statement **/ }else :{?>

It should be this without the colon :

<?php /** this is the first last if else statement **/ }else {?>

Try and use the same standard for your conditional statements rather than using different conventions within the same file.

Upvotes: 0

George Roots
George Roots

Reputation: 11

Not sure if this will affect what you're doing but typically you want you if statement flipped: if ( get_post_type() === 'post' ) : ?>

Upvotes: 0

Related Questions