Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Konrad Pilch
Konrad Pilch
2,435 Points

How to display only one post?

HI,

I want be able to show one post that i just wrote. So whenever i write a post in WP , the post will go full on my main blog page , and if i write another, the blog before will go to archives, as i do believe they do go there anyways.

So if i write a post, it will display only it self on the main blog page. If i write another, it will disapear . I have recent post and archive but i want only one post to display on my page per time. Later one i will have a page where i will display all blogs .

<?php
/**
 * Template part for displaying posts.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package Myself
 */

?>

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




        <?php if ( 'post' === get_post_type() ) : ?>

        <div id="main-column">
            <div class="post-title">

                <div class="author-img">
                    <img src="<?php bloginfo('stylesheet_directory') ?>/assets/img/pen.jpg">
                </div>

                <div class="clear"></div>

                <?php the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>

                <div class="post-meta"> 
                    <ul>
                        <li> <?php echo get_the_date('d.M'); ?>  / </li>
                        <li class="divider"></li>
                        <li>
                            <a href=""><?php the_tags(); ?></a>
                        </li>
                        <li class="divider"></li>
                        <li><?php comments_number() ?></li>
                    </ul>
                </div>
                <div><?php edit_post_link(); ?></div>
            </div><!-- /post-title -->

            <?php if ( has_post_thumbnail() ) { ?>
            <div class="entry-image">
                <?php the_post_thumbnail(); ?>
            </div><!-- /entry image -->
            <?php } ?>

        <?php endif; ?>

         <div class="the-container">
            <div class="entry-container">

                <div class="entry-content">
                <?php
                    the_content( sprintf(
                        /* translators: %s: Name of current post. */
                        wp_kses( __( 'Continue reading %s <span class="meta-nav">&rarr;</span>', 'aurelianportfolio' ), array( 'span' => array( 'class' => array() ) ) ),
                        the_title( '<span class="screen-reader-text">"', '"</span>', false )
                    ) );
                ?>

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

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

    </header><!-- .entry-header -->




</article><!-- #post-## -->

1 Answer

Have you tried looking at get_posts and setting the posts_per_page parameter to 1? For more information the codex provides excellent examples and documentation: https://codex.wordpress.org/Template_Tags/get_posts