Support » Plugins » Problem with simple plugin for showing last year’s posts

  • Resolved Rafael Fischmann



    I’m having trouble with a new plugin I’m developing (my first one!) that is supposed to how in every single.php page a list of posts from exactly one year before. Sorry for the parts in which I’m using the Portuguese language, but the plugin structure itself is very understandable, I reckon:

    	Plugin Name:	Um Ano Atr&aacute;s...
    	Plugin URI:
    	Description:	Lista os posts de um ano atr&aacute;s na p&aacute;gina de leitura do artigo.
    	Author:			Rafael Fischmann
    	Version:		1.0
    	Author URI:
    function um_ano_atras() {
    	global $wpdb, $posts, $post;
    	$sql = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND day(post_date) = day('$post->post_date') AND month(post_date) = month('$post->post_date') AND year(post_date) = year('$post->post_date') - 1 ORDER BY post_date DESC";
    	$posts = $wpdb->get_results($sql);
    	$output = '<h2>Um Ano Atr&aacute;s...</h2>';
    	$output .= '<ul>';
    	if($posts) {
    		foreach ($posts as $post) {
    			$post_title = stripslashes($post->post_title);
    			$permalink = get_permalink($post->ID);
    			$output .= '<li><a href="' . $permalink . '" rel="bookmark" title="' . htmlspecialchars($post_title, ENT_COMPAT) . '">' . htmlspecialchars($post_title) . '</a></li>';
    	} else {
    		$output .= "<li>N&atilde;o houve nenhum post h&aacute; um ano.</li>";
    	$output .= '</ul>';
    	echo $output;

    The plugin seems to be working fine (showing the list of posts from one year before in each post page), however it’s messing up the post comments. Can anybody tell me what’s wrong?!

    Thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Anyone…? This should be simple, I just can’t find my mistake.

    It’s a shame I couldn’t get help from anyone over here. A friend of mine gave me a hand:

    The variable name in the foreach function was conflicting with something. Just changed from $post to $mypost and it started working fine.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem with simple plugin for showing last year’s posts’ is closed to new replies.