wp query template tags not working

admin2025-06-04  3

I create a page : contact-us then I specify a template for this page and this is the template code:

<?php
/*
Template Name: Template wp_query
*/
?>

 <?php  
   $arg = array (
      'post_type' => 'post',
       'post_per_page' => -1 ,

     );

     $test = new WP_Query($arg);
     var_dump($test);

     if ($test->have_posts()) {
       while ($test-> have_posts()) : $test-> the_post();

              echo   $test->get_the_title();
               echo  $test->get_the_content;

         endwhile;
         }
         wp_reset_query();
          ?> 

the result page is blanck even if the var_dump($test) return the list of post with the information. for your information i tried query_post() and it works fine. please help me.

I create a page : contact-us then I specify a template for this page and this is the template code:

<?php
/*
Template Name: Template wp_query
*/
?>

 <?php  
   $arg = array (
      'post_type' => 'post',
       'post_per_page' => -1 ,

     );

     $test = new WP_Query($arg);
     var_dump($test);

     if ($test->have_posts()) {
       while ($test-> have_posts()) : $test-> the_post();

              echo   $test->get_the_title();
               echo  $test->get_the_content;

         endwhile;
         }
         wp_reset_query();
          ?> 

the result page is blanck even if the var_dump($test) return the list of post with the information. for your information i tried query_post() and it works fine. please help me.

Share Improve this question edited Jan 13, 2019 at 13:35 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jan 13, 2019 at 12:59 alpha.romeoalpha.romeo 491 gold badge1 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Template tags are not methods of WP_Query object. They are functions.

On the other hand have_posts and the_post are methods of WP_Query.

So in your code you should use:

while ($test->have_posts()) : $test->the_post();

as you do, but then:

           echo  get_the_title();
           echo  get_the_content();

Also... if you want to echo these values, it would be much better to use the_title and the_content instead - there are some additional filters and actions fired up.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749016599a315646.html

最新回复(0)