WordPress.org

Ready to get started?Download WordPress

Forums

global $post and get_posts( ) (4 posts)

  1. sacher
    Member
    Posted 2 years ago #

    Hi,
    I'm just curious about something that maybe has a obvious answer but I cannot find it.

    Why do you use global $post; with get_posts( );, like in the example bellow. Is it mandatory?

    <?php
    global $post;
    $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>

    Thank you,
    Sacher

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    where is that example coming from?

    can you post a link to that source?

    afaik, global $post; is not needed in that example.

  3. sacher
    Member
    Posted 2 years ago #

    Hi,
    Thanks for your reply.
    There are plentyy of example in the WordPress codex
    http://codex.wordpress.org/Template_Tags/get_posts

  4. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    using global $post; might make sense it this example 'Reset after Postlists with offset' if the code is used in a different template than the main template, for instance in the sidebar, or in a function.

Topic Closed

This topic has been closed to new replies.

About this Topic