skip to Main Content

it loops correctly 10 times but with wrong content. it alloys using the content with id 11

show_posts with 10 posts (thats correct) but always first content

my function show_posts in Theme.php :

    public function show_posts() { # show_posts220914() { 
            $args = array(
            'numberposts'   => 200
        );
        $my_posts = get_posts( $args );
        if( ! empty( $my_posts ) ){
            foreach ( $my_posts as $post ){
                get_template_part( 'content','content');
            }
        }
    }

so result in http://localhost/wordpress/ is

<li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li>

show_posts with correct constant but to less posts:

in this next try i have correct constant but to less posts:

    public function  show_posts(){
        global $wp_query;
        $args = array_merge( $wp_query->query_vars, ['posts_per_page' => 200 ] ); # has no effect here 
        query_posts( $args ); # has no effect here 
        if ( have_posts() ) :
            while ( have_posts() ) : the_post();
                $post= the_post();
                get_template_part( 'content','content');
            endwhile;
        endif;
    }

so result in http://localhost/wordpress/ is

<li>11</li><li>12</li><li>13</li><li>14</li><li></li>

my content.php:

<li>
    <?php the_title(); ?>
</li>

my home.php:

<ol>
    <?php 
    do_action( 'home_content' ); 
    ?>
</ol>

i call it with http://localhost/wordpress/.
i don’t use any pages. means http://localhost/wordpress/wp-admin/edit.php?post_type=page tells me No pages found.

i found inspiration here:

i using WordPress 6.0.2

any idea?

2

Answers


  1. Chosen as BEST ANSWER

    i get this correct output

    <li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li>
    

    if i use this:

    Theme.php :

        public function show_posts_so() { 
            $posts = get_posts(array('numberposts' => -1));
            if ( count($posts)>0 ) :
                foreach($posts as $this_special_post){
                    get_template_part( 'content','content', $this_special_post);   
                }
            endif;
        }
    

    content.php :

    <li><?php echo $args->ID ?></li>
    

    functions.php :

    add_action( 'home_content', [$theme ,'show_posts_so']);
    

    home.php :

        <?php   do_action( 'home_content' );    ?>
    

    it helps me reading this: What is the real purpose of magic method __set_state in PHP? and the use of the var_export command sometimes


  2. Your second WordPress loop looks like it could almost work … I’m not sure what $wp_query->query_vars could contain that you are merging with your post_per_page constraint.

    Try this, more standard loop (and/or read about the loop on WordPress’ Codex):

    function show_post(){
    $args = [
        'posts_per_page' => 10, 
        'post_type' => array( 'post' ),
        'post_status' => array( 'publish' ),
    ];
    
    $query = new WP_Query( $args );
    
    if ( $query->have_posts() ) :
        while ( $query->have_posts() ) :
            $query->the_post();         
     
            get_template_part( 'content','content');   
    
        endwhile;
    endif;
    
    // Restore original Post Data
    wp_reset_postdata();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search