admin管理员组

文章数量:1415673

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 5 years ago.

Improve this question

I am using this loop and instead of getting my posts(blogs) i am getting pagetitle in my case Home and Homepage content

<?php
while(have_posts()) {
    the_post(); ?>
   <h2><?php the_title(); ?></h2>
   <?php the_content(); ?>
    <hr>
 <?php }
?>

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 5 years ago.

Improve this question

I am using this loop and instead of getting my posts(blogs) i am getting pagetitle in my case Home and Homepage content

<?php
while(have_posts()) {
    the_post(); ?>
   <h2><?php the_title(); ?></h2>
   <?php the_content(); ?>
    <hr>
 <?php }
?>

Share Improve this question edited Aug 17, 2019 at 12:19 user173633 asked Aug 16, 2019 at 18:27 user173633user173633 11 bronze badge 4
  • Where are you using this code? Is it in a template file, functions.php, a plugin,...? Please edit your question to provide more context. – Pat J Commented Aug 16, 2019 at 18:46
  • That is the main query, and it is for that specific page. You will need to build a different query and loop to display something else. – Nathan Powell Commented Aug 16, 2019 at 18:49
  • There is no query argument, you don't set up the query correctly. Google for $args of the query. In addition, I'd add an if statement if(have_posts()).. while... there are hundrets of examples on this site and on the web. – user3135691 Commented Aug 16, 2019 at 20:30
  • I am trying to make my own theme i made an index.php and i want to try this loop but didnt work. – user173633 Commented Aug 16, 2019 at 21:10
Add a comment  | 

1 Answer 1

Reset to default 1

I'm not 100% sure what you're trying to do with this.

Out of the Box, WordPres allows you to set your home page as either a static page, or a blog. If you choose blog, it will automatically output title and content for you.

If you're trying to customize a static home page, then you'll need to add a piece with arguments, like they've been saying there in the comments.

    <?php   
    $args = [
            'post_type'      => 'posts', // you can also use custom post types here
            'posts_per_page' => '10', // how many I want to display
            'post_status'    => 'publish', // I only want published posts to appear

        ];

    // The Query.
    $the_query = new WP_Query( $args );

    // The Loop.
    if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) {
            $the_query->the_post(); 
    } ?>

    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
    <hr>

    <?php endif;
        wp_reset_postdata();
    }
    ?>

本文标签: phpWhy my loop isn39t working