WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] the_title(), get_the_title() both returning page rather than post values (4 posts)

  1. wp_snowman1980
    Member
    Posted 1 year ago #

    Hi guys

    I am creating a custom theme.

    I have set WPAdmin > Settings > Reading > Front page to be "Home" (static page)

    For "Home", I have WPAdmin > Pages > "Home" > Template set to "Front Page" which is the template name for the template I've created for the homepage (front_page.php).

    I have added several test posts under 2 categories : 1) smallprint 2) quote

    Now..

    On front_page.php, I want to get all posts with category of "quote" and loop through them, outputting the title and content of that post.

    The code below seems to output the content of the post fine, but the title it outputs is the page title ("Home"), not the post title.

    I've also tried echo the_title() and using the WordPress Loop. All to no avail.

    <?php
    
    	global $quote;
    	$firstQuote = true;
    
    	$quote_cat_id = get_cat_ID('quote');
    	$quotes = get_posts('category='.$quote_cat_id);
    
    	foreach($quotes as $quote) : setup_postdata($quote);
    ?>
    	<div class="item <?php if ($firstQuote) echo "active"; ?>">
    
    	<blockquote>
    		<p><span>“</span><?php echo get_the_content(); ?><span>”</span></p>
    		<footer><?php echo get_the_title(); ?></footer>
    
    	</blockquote>
    	</div>
    <?php
    	$firstQuote = false;
    	endforeach;
    ?>

    This is really confusing me, could anyone point out where I've gone wrong? I feel I've tried everything I can and this is a last resort.

    Thanks in advance.

  2. wp_snowman1980
    Member
    Posted 1 year ago #

    By the way, I've also tried using the $post object:

    $post->post_title
    $post->post_name

    Both to no avail.

    Thanks

  3. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    the Codex http://codex.wordpress.org/Template_Tags/get_posts does not point this out explicitely, but you have to use:

    foreach($quotes as $post) : setup_postdata($post);

    (as in the examples in the linked Codex chapter) to get it to work.

    also, after the 'foreach' loop, add:
    wp_reset_postdata();

  4. wp_snowman1980
    Member
    Posted 1 year ago #

    Hi alchymyth

    Thanks for taking the time to reply.

    I can't believe that I missed that fix, it worked!

    Some good karma is coming your way!

    Cheers

Topic Closed

This topic has been closed to new replies.

About this Topic