WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Problem with simple plugin for showing last year's posts (3 posts)

  1. Rafael Fischmann
    Member
    Posted 6 years ago #

    Guys,

    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:

    <?php
    /*
    	Plugin Name:	Um Ano Atr&aacute;s...
    	Plugin URI:		http://macmagazine.com.br/blog/
    	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:		http://macmagazine.com.br/blog/
    */
    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!

  2. Rafael Fischmann
    Member
    Posted 6 years ago #

    Anyone...? This should be simple, I just can't find my mistake.

  3. Rafael Fischmann
    Member
    Posted 6 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic