$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>loop - Posts will not display on page-mypage.php|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

loop - Posts will not display on page-mypage.php

matteradmin10PV0评论

Something is really tripping me up. I have been used to querying posts via custom queries however based on research lately I have learned that for the most part this is a big no no. So, in testing the standard wp loop, I realized it does not work on for example, page-mypage.php. Can anyone share insight on this?

If I do something like get_title, it just gives the title for that page. I realize there is other posts about this topic on stack but all the up-voted responses are to use a custom query which again, as I have understood, is a poor method and pre_get_posts should be used instead. However, I tried using pre_get_posts function and that did not work either.

add_action( 'pre_get_posts', 'display_posts' );
function display_posts( $query ) {
if ( ! is_admin() && $query->is_main_query()) {
} // end if;
} 

Does the standard loop only work on index.php? So I am assuming the global query does not run before page.php is called? Im totally confused here as well because how would wp know to display posts on, for example, page-mypage.php unless you are querying them directly in the template?

Again, I am using the standard loop, and it is not displaying any post data.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>

Something is really tripping me up. I have been used to querying posts via custom queries however based on research lately I have learned that for the most part this is a big no no. So, in testing the standard wp loop, I realized it does not work on for example, page-mypage.php. Can anyone share insight on this?

If I do something like get_title, it just gives the title for that page. I realize there is other posts about this topic on stack but all the up-voted responses are to use a custom query which again, as I have understood, is a poor method and pre_get_posts should be used instead. However, I tried using pre_get_posts function and that did not work either.

add_action( 'pre_get_posts', 'display_posts' );
function display_posts( $query ) {
if ( ! is_admin() && $query->is_main_query()) {
} // end if;
} 

Does the standard loop only work on index.php? So I am assuming the global query does not run before page.php is called? Im totally confused here as well because how would wp know to display posts on, for example, page-mypage.php unless you are querying them directly in the template?

Again, I am using the standard loop, and it is not displaying any post data.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
Share Improve this question edited Jan 24, 2019 at 5:16 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Jan 24, 2019 at 4:43 JamesJames 211 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can use custom query to show post in your template


$args = array(
    'post_type' => 'product',
    'posts_per_page' => 10
  );
  $loop = new WP_Query( $args );
  while ( $loop->have_posts() ) : $loop->the_post();
    the_title();
    the_content();
  endwhile;

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far