How to display recent WordPress posts on a page

1

I found a way to display WordPress articles with the following code.

<ul> 
    <?php 
    $recent = new WP_Query("cat=3&showposts=5"); 
    while($recent->have_posts()) : $recent->the_post();?> 
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
    <?php endwhile; ?> 
</ul>

The complete tutorial is here . I already found a script for Blogger, but it does not work in WordPress.

In the tutorial also has that other code that is for shows the image of the post.

<a href=""><img src="/timthumb.php?src=&h=60&w=60&zc=1" title="Clique para ler: " alt="" class="recent-posts-thumb" width="60px" height="60px" />

The goal is to display a page with articles from a particular article: Category or tag. So you can better organize them.

The name of this code would be a hack Read more . What I would like to do is show specific articles with a short summary of the post on a page.

    
asked by anonymous 31.08.2014 / 16:25

1 answer

1

There are two ways to do this.

Page template

Create a file within your theme, based on page.php and putting a name as recentes-page.php . Change the PHP header to:

<?php
/*
Template Name: Posts recentes
*/

<-willappearRecentPostsinsteadofMyCustomPage

Nowjustadaptyourcodeinthistemplate,createanewpageandselectthenewtemplatePostsrecentes.

Shortcode

Create a plugin that will generate the result when you add [meu_shortcode] to any post, page, or widget. Since a shortcode has to return the result instead of printing, it is better to use get_posts instead of WP_Query .

<?php
/**
 * Plugin Name: (SOPT) Shortcode para Posts recentes
 * Plugin URI:  http://pt.stackoverflow.com/a/31002/201
 * Description: Use o shortcode [recentes] para mostrar uma lista de posts recentes
 * Author:      brasofilo   
 */

/**
 * Permite o uso de shortcodes no Widget de Texto
 */
add_filter( 'widget_text', 'do_shortcode' );

/**
 * Declaração do shortcode
 */ 
add_shortcode( 'recentes', 'shortcode_sopt_30997' );

/**
 * Callback do shortcode [recentes]
 */
function shortcode_sopt_30997( $atts, $content = null ) 
{
    $html = '<ul>';
    $recent = get_posts("cat=3&showposts=5"); 

    if( !$recent )
        return 'Não há posts recentes';

    foreach( $recent as $post )
    {
        $html .= sprintf(
            '<li><a href="%s">%s</a></li>',
            get_permalink( $post->ID ),
            $post->post_title
            );
    }

    $html .= '</ul>';

    return $html;
}

Related posts:

31.08.2014 / 17:14