在自定义页面上列出博客标题

List blog titles on custom page

我的 wordpress 博客上有一个静态主页。我不想在所有其他页面的选定区域中列出最新的 post 标题。所以我有这个代码。

<?php

        $args = array(
          'showposts' => '1'
        );
        $the_query = new WP_Query( $args );

        if ( $the_query->have_posts() ) {

        ?>

            <header>
                <h2 class="latestPost"><a href="#"><?php the_title(); ?></a></h2>
            </header>

        <?php

        } else {
            // no posts found
        }
        wp_reset_postdata();

        ?>

这不会像我想要的那样显示最新的 post 标题,它会显示访问者所在页面的标题。

我做错了什么?

这样试试

$args = array(
      'showposts' => '1'
    );

    $the_query = new WP_Query( $args );
    // The Loop
    if ( $the_query->have_posts() ) {
            $the_query->the_post(); ?>
            <header>
            <h2 class="latestPost"><a href="#"><?php the_title(); ?></a></h2>
            </header>
        <?php
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
<?php query_posts('showposts=1'); ?>
<?php if (have_posts()) : the_post(); ?>

 <header>
     <h2 class="latestPost"><a href="#"><?php the_title(); ?></a></h2>
 </header>
<?php

    } else {
        // no posts found
    }

 ?>