WordPress.org

Ready to get started?Download WordPress

Forums

Advanced Issue With the_title() in a custom loop (2 posts)

  1. ninjaboy81
    Member
    Posted 6 years ago #

    Here's my code:

    <?php
    //do an sql query for posts
    <?php global $wpdb; /* i found out that this is required in the sidebar! and i think this is somehow relevant to the bigger problem!*/
    <?php $querystr = "SELECT * FROM $wpdb->posts"
    <?php $pageposts = $wpdb->get_results($querystr, OBJECT); ?>
    
    // custom loop
    <? if ($pageposts) : foreach ($pageposts as $post): setup_postdata($post); ?>
    
    // loop stuff!!!
    Number 1: <?php the_title(); ?>
    
    Number 2: <?php echo $post->post_title ?>
    
    <?php endforeach; ?>
    <?php else : ?>
    // if no posts to loop through
    <?php endif; ?>

    this custom loop (used in Daiko's phptextbox sidebar) fails to call the_title() function during each iteration of the loop

    the_title() will display the LAST title found in the whole sql query each time.

    compare against: echo $post->post_title which displays each post title. but it is very stripped down without all the html formating the_title() adds in.. which is undesirable.

    how do i get the_title() to work in this loop?

  2. ninjaboy81
    Member
    Posted 6 years ago #

    any help with this?

    somehow if i place this loop in my footer.php file, it will work just fine displaying all post titles using the_title() and all permalinks using the_permalink().

    however, when this loop is placed in the sidebar it returns the_title() and the_permalink() of only the last found post in the whole query.

Topic Closed

This topic has been closed to new replies.

About this Topic