Forums

get_posts() and permalink - only showing page title/link, not post title/link (7 posts)

  1. browngrapevine
    Member
    Posted 1 year ago #

    Hi,

    I'm using the following code to display the most recent post on my static homepage. It appears that "the_permalink" and "the_title" aren't working - I only see the page title (Home) and the page link (www.thinkbicycles.org/)

    How do I get it to display the post's title and URL instead?

    <?php
    $previous_posts = get_posts('numberposts=1');
    foreach($previous_posts as $post) :
    setup_postdata($post); ?>
    "><?php the_title(); ?>
    <?php the_excerpt(); ?>
    <?php endforeach; ?>

  2. browngrapevine
    Member
    Posted 1 year ago #

    Sorry - the code didn't display correctly in my last e-mail:

    <?php
    $previous_posts = get_posts('numberposts=1');
    foreach($previous_posts as $post) :
    setup_postdata($post); ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php the_excerpt(); ?>
    <?php endforeach; ?>

  3. MAS
    Member
    Posted 1 year ago #

    EDIT: at first use the wp_reset_query() and global post function

  4. browngrapevine
    Member
    Posted 1 year ago #

    thanks for your fast comment. I just tried that, and it didn't change the title/link that's displayed.

  5. MAS
    Member
    Posted 1 year ago #

    what is yor code now?

  6. browngrapevine
    Member
    Posted 1 year ago #

    Thank you! Adding "global post" function to the code seems to do the trick. I now see the correct post title/link. However, now the "comments" box appears at the bottom of the post excerpt, even though the "Comments" box for the homepage has been disabled. Any suggestions why that is happening?

    Here's my code now:
    <?php wp_reset_query(); ?>
    <?php
    global $post;
    $previous_posts = get_posts('numberposts=1');
    foreach($previous_posts as $post) :
    setup_postdata($post); ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php the_excerpt(); ?>
    <?php endforeach; ?>

  7. MAS
    Member
    Posted 1 year ago #

    by default "Comment Box" should not show on home page. It will be coming on page.php, single.php page only. check that have this type of code global $withcomment;

Topic Closed

This topic has been closed to new replies.

About this Topic