How to display recent Wordpress posts on a page

Asked

Viewed 9,573 times

1

I found a way to display the 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 found here. I had already found a script for Blogger, but it does not work in Wordpress.

In the tutorial also has this other code that is to show the post image.

<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 show a page with articles from a given article: Category or tag. And thus manage to organize them better.

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

  • Please, check out http://answall.com/editing-help . . . . When you edit here on [pt.so] you have a box of preview below, you can see clearly that the code is not appearing.

  • I add the normal html, but it doesn’t appear, keeps cutting several parts.

  • Check out my edition to see how to format here in SOPT: http://answall.com/posts/30997/revisions. . . I suggest you install a spell checker in your browser, it greatly decreases the work of editors.

  • PS: I formatted the code to be readable, all in one line is just not cool for understanding... But checking now I see that there is no difference in the first two blocks of code. Can [Edit] your question and clarify this?+

  • I removed the block that was equal to the first.

  • There was a bug in the code of the plugin, I fixed. It came to test some of the solutions?

Show 1 more comment

1 answer

1

There are two ways to do this.

Page template

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

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

<-- will appear Recent posts in place of My Custom Page

Now just adapt your code in this template, create a new page and select the new template Posts recentes.

Shortcode

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

<?php
/**
 * Plugin Name: (SOPT) Shortcode para Posts recentes
 * Plugin URI:  /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:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.