list of the last 20 articles

Home » Snippets » list of the last 20 articles
0

Created with:

Visibility: 

public

Creator: Jan

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php

namespace WPTurbo;

use WP_Query;

function wpturbo_display_last_articles() {

    // Check if the user has the capability to view articles.
    if ( ! current_user_can( 'read' ) ) {
        return;
    }

    $args = [
        'post_type'      => 'post',
        'posts_per_page' => 20,
        'orderby'        => 'date',
        'order'          => 'DESC',
    ];

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        echo '<ul>';

        while ( $query->have_posts() ) {
            $query->the_post();
            echo '<li>' . esc_html( get_the_title() ) . '</li>';
        }

        echo '</ul>';

        wp_reset_postdata();
    } else {
        echo '<p>' . esc_html__( 'No articles found.', 'wpturbo' ) . '</p>';
    }
}

wpturbo_display_last_articles();
				

By following these security practices, you can help safeguard your code against common vulnerabilities. If you have any specific functionality planned that might introduce new inputs or outputs, please let me know, and I can provide additional guidance!

Register an account to save your snippets or go Pro to get more features.