• Resolved driz

    (@driz)


    <p><ul>
    <?php
    $posts = get_posts('numberposts=5&offset=0');
    foreach($posts as $post) {
       echo '<li><a class="newslink" href="' . get_permalink() . '">' . the_title('', '', false);
       echo '</a></li>';
    }
    ?>
    		</ul></p>

    Okay im using this code to display recent topics but im just getting a list of the page name, in this case home. see https://www.simplecandy.com/ and look at the bottom left block. Please note that the php is being inserted inside a post and run using phpRun plugin. x

Viewing 7 replies - 1 through 7 (of 7 total)
  • Kafkaesqui

    (@kafkaesqui)

    get_permalink() must be passed the post’s ID as an argument:

    get_permalink($post->ID)

    Or just try (if in a template and not through the various ‘PHP in a post’ plugins):

    <ul>
    <?php
    $posts = get_posts('numberposts=5&offset=0');
    foreach($posts as $post) {
    ?>
    <li><a class="newslink" href="<?php the_permalink(); ?>">
    <?php the_title(); ?></a></li>
    <?php } ?>
    </ul>

    (We’ll assume you know what you’re doing by resetting the global $posts object.)

    Thread Starter driz

    (@driz)

    okay i tried using get_permalink($post->ID) instead of just get_permalink() no changes. what do you mean by resetting the global object?

    Thread Starter driz

    (@driz)

    Any new info?

    Thread Starter driz

    (@driz)

    Help?

    Kafkaesqui

    (@kafkaesqui)

    With the code I’ve specified or using yours I have no problems, so it must have to do with trying to run it through a post.

    All I can suggest is to:

    1. Instead of using get_posts() create a new query object and use this to generate your post links:

    <ul>
    <?php
    $query_posts = new WP_Query('showposts=5');
    while($query_posts->have_posts()) {
    	$query_posts->the_post();
    	echo '<li><a class="newslink" href="' . get_permalink() . '">' . the_title('', '', false);
    	echo '</a></li>';
    } ?>
    </ul>

    2. Drop the idea of using phpRun and just place the code in your template where you want the post links to appear.

    Thread Starter driz

    (@driz)

    Wicked the WP_Query worked superb, i wonder why the get_posts didn’t? In any case thank you. x

    i wonder why the get_posts didn’t?

    Relates to my original comment on global objects. $posts and $post are pretty important ones in WordPress, $post especiallly here as it holds the *current* post’s properties. Trying to initialize $post through get_posts() while inside a post no doubt is the culprit behind what you were seeing.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘display recent topics not working?’ is closed to new replies.