Home>

I want to list Wordpress custom post types (notice/single-info.php) on a fixed page (notice list/archive-info.php).

I created a custom post type "Notice" from the Custom Post Type UI.
As a post type slug "info", I set "Archive" to "true" in the Custom Post Type UI settings.

I want to list the articles of the custom post type "Notice" on the fixed page (Notice list/archive-info.php), so

      
             Information list 
          <? php if (have_posts ()): // Conditional branch: If there are posts?>
              <? php while (have_posts ()): the_post ();// Start iterative processing?>
          <section>
              <ul>
                  <li>
                      <p><? php echo get_the_date ();?></p>
                         ">
                          <p><? php the_title ();?></p>
                  </li>
              </ul>
              <? php endwhile;// End of iteration?>
          <? php else: // Conditional branch: If there is no post?>
                 Post not found 
          <? php endif;// Conditional branch end?>
          </section>
      </article>
code

The loop is described in archive-info.php as above.

However, as shown in the image below, the posted article "Notice" single-info.php is not output in archive-info.php.


What is output is the fixed page title "Notice list/archive-info.php" of archive-info.php, and the link destination is also "Notice list/archive-info.php".

Where should I check or modify to list the custom post type (notice/single-info.php) on the fixed page (notice list/archive-info.php)?

  • Answer # 1

    
         Information list 
      <? php $the_query = new WP_Query (array ('post_type' =>'info'));?>
      <? php if ($the_query->have_posts ()): // Conditional branch: If there are posts?>
          <? php while ($the_query->have_posts ()): $the_query->the_post ();// Start iterative processing?>
          <? php // (omitted)?>
          <? php endwhile;// End of iteration?>
          <? php wp_reset_postdata ();?>
      <? php else: // Conditional branch: If there is no post?>
             Post not found 
      <? php endif;// Conditional branch end?>
      </section>
    </article>


    Is it like this?
    Please add the option of WP_Query as appropriate.

    Reference Function Reference/WP Query