WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Problem making widget to display newest posts form category (6 posts)

  1. pedjas
    Member
    Posted 2 years ago #

    Well, I am trying to create widget that displays last posts from selected category. Everzthing works fine exept it always displays title and link to last post on blog, not slected post. Here is some code:

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    Problem is this: get_posts() does return needed posts properly, and in a foreach loop I prepare each post using setup_postdata(). However, the_permalink() and the_title() always return values from the last post on forum, not from post prepared using setup_postdata().

    the_excerpt() does return proper exceprt form current post I want to display. So I get pretty messy result.

    I've spent substantial time trying to figure out what I am doing wrong, but I cannot tell.

    Any help?

  2. pedjas
    Member
    Posted 2 years ago #

    I did not realize that posting code is forbidden. I do not see any purpose of this user unfriendly requirement. Ok, I understand hundred or so lines are unreadable, but allowing just ten lines, that is useless. After all, forum should warn and do not allow posting message.

    I tried to use pastebin as required, I hope it will do the job:

    Whole method code is at http://pastebin.com/iPPPxNG5

    Here is problematic part:

    $m_posts = get_posts($m_args);
    		foreach ($m_posts as $m_post) {
     		  setup_postdata($m_post);
    ?>
    		<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
    		<?php //echo the_excerpt(); ?>
    <?php
    		}
  3. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    forum should warn and do not allow posting message.

    http://wordpress.org/support/topic/welcome-to-the-hacks-forum
    http://codex.wordpress.org/Forum_Welcome

    You did read these before posting, didn't you?

  4. pedjas
    Member
    Posted 2 years ago #

    No I did not. I read rules years ago when I first came to wordpress.org and I do not expect I have to check rules to see if something changed, especially to look for something such unreasonable. 10 lines? Then, posting any code should be banned as ten lines is usually not enough.

    Rule is not what upset me, but that forum did not warn me that I posted too much code and did not refuse to save post, so I did not notice that was the problem until several hours later.

  5. pedjas
    Member
    Posted 2 years ago #

    I solved my problem by abandoning the_permalink() and the_title(). They do not work on the contrary what Codex says (and even provides example). So, there must be a bug in API or Codex.

    I managed to get what I needed by using get_permalink() and get_the_title() instead.

  6. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    They do not work

    I've never had a problem with them on dozens of WordPress sites.

Topic Closed

This topic has been closed to new replies.

About this Topic