0

Using underscores, I made a lot of small, simple custome themes. I was more interested in CSS and simple layout modifications, but I'm not used to vanilla PHP. What I need I think is a simple code, but I can't wrap my head around it and I've been trying for hours. I'm trying to have a different layout for my front page, which is a static page. It works only under certain circumstances. Here's what I did and it sort of works, only I get a blank screen when trying to use elseif.

My content-page.php looks like this:

<?php
/**
 * Template part for displaying page content in page.php.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package mythemename
 */

?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">
        <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
    </header><!-- .entry-header -->

    <div class="entry-content">
        <?php the_content(); ?>
        <?php
            wp_link_pages( array(
                'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'phoebelovegood' ),
                'after'  => '</div>',
            ) );
        ?>
    </div><!-- .entry-content -->

    <footer class="entry-footer">
        <?php
            edit_post_link(
                sprintf(
                    /* translators: %s: Name of current post */
                    esc_html__( 'Edit %s', 'phoebelovegood' ),
                    the_title( '<span class="screen-reader-text">"', '"</span>', false )
                ),
                '<span class="edit-link">',
                '</span>'
            );
        ?>
    </footer><!-- .entry-footer -->
</article><!-- #post-## -->

Using conditionals for my page ID, I'm trying to do this:

<?php if(is_page("18")): ?>
<p>This is front page</p>
<?php elseif: ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">
        <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
    </header><!-- .entry-header -->

    <div class="entry-content">
        <?php the_content(); ?>
        <?php
            wp_link_pages( array(
                'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'phoebelovegood' ),
                'after'  => '</div>',
            ) );
        ?>
    </div><!-- .entry-content -->

    <footer class="entry-footer">
        <?php
            edit_post_link(
                sprintf(
                    /* translators: %s: Name of current post */
                    esc_html__( 'Edit %s', 'phoebelovegood' ),
                    the_title( '<span class="screen-reader-text">"', '"</span>', false )
                ),
                '<span class="edit-link">',
                '</span>'
            );
        ?>
    </footer><!-- .entry-footer -->
</article><!-- #post-## -->
<?php endif; ?>

Instead, I get a blank page. What do I do wrong? Code is working if used in page.php for example, when I want to add something in-between, not using elseif.

Any thoughts? Thanks in advance!

1

Your first code sample didn't work because the elseif had no condition. elseif is a concatenation of else and if:

if ( something ) {

} elseif ( something else ) {

} else {

}

Or

<?php if ( something ): ?>

<?php elseif ( something else ): ?>

<?php else: ?>

<?php endif; ?>

Also, if you want a front page that's different from your other pages, look at the front-page.php template.

  • Thank you! No, it won't be only the front-page, I will hardcode multiple pages, based on ID. – designarti Nov 30 '15 at 17:32
  • I might suggest you hardcode based on something you can control, like the page slug, instead of ID. If for any reason you have to migrate your site or restore content from a backup, there is no promise your IDs will remain the same. Something to consider - not to mention, there are likely much better ways to achieve the desired goal. – phatskat Nov 30 '15 at 17:48
  • this will be something like (is_page("pageslug"))? – designarti Dec 1 '15 at 13:11
0

Oh, boy. I did manage to fix that and I will answer my first question here on WPSE. It shouldn't work with elseif, only with else. Works with:

<?php if (is_page('18')) { ?>
<p>This is front page</p>
<?php } else { ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">
        <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
    </header><!-- .entry-header -->

    <div class="entry-content">
        <?php the_content(); ?>
        <?php
            wp_link_pages( array(
                'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'phoebelovegood' ),
                'after'  => '</div>',
            ) );
        ?>
    </div><!-- .entry-content -->

    <footer class="entry-footer">
        <?php
            edit_post_link(
                sprintf(
                    /* translators: %s: Name of current post */
                    esc_html__( 'Edit %s', 'phoebelovegood' ),
                    the_title( '<span class="screen-reader-text">"', '"</span>', false )
                ),
                '<span class="edit-link">',
                '</span>'
            );
        ?>
    </footer><!-- .entry-footer -->
</article><!-- #post-## -->
<?php } ?>

Different approach...

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.