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

  1. wp_snowman1980
    Posted 2 years 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


    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.

    	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"; ?>">
    		<p><span>“</span><?php echo get_the_content(); ?><span>”</span></p>
    		<footer><?php echo get_the_title(); ?></footer>
    	$firstQuote = false;

    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
    Posted 2 years ago #

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


    Both to no avail.


  3. Michael
    Part-Time Forum Moderator
    Posted 2 years 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:

  4. wp_snowman1980
    Posted 2 years 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!


Topic Closed

This topic has been closed to new replies.

About this Topic