How to display recent post for a specific category

  • Unknown's avatar

    I need help displaying a specific category using an html widget. Specifically my “travels” category. Is there a way to do it? I have tried copying a code but will always get an error: special characters must be escaped: [<].

    Here’s the code I got from a similar question but I guess/maybe they’re not using an html widget.

    $args = array(
    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘publish’,
    ‘category_name’ => ‘wordpress’,
    ‘posts_per_page’ => 5,
    );
    $arr_posts = new WP_Query( $args );

    if ( $arr_posts->have_posts() ) :

    while ( $arr_posts->have_posts() ) :
    $arr_posts->the_post();
    ?>
    <article id=”post-<?php the_ID(); ?>” <?php post_class(); ?>>
    <?php
    if ( has_post_thumbnail() ) :
    the_post_thumbnail();
    endif;
    ?>
    <header class=”entry-header”>
    <h1 class=”entry-title”><?php the_title(); ?></h1>
    </header>

    </article>
    <?php
    endwhile;
    endif;

    Thank you in advance!

    The blog I need help with is: (visible only to logged in users)

  • Unknown's avatar

    Hi, are you trying to display the posts from TRAVEL category on a specific page – like your TRAVEL page?

  • Unknown's avatar

    Hi umeshx,

    Thank you for your response. I was able to figure it out last night and forgotten to answer my question here. The text widget helped solve my problem and I used one of the shortcodes to display recent posts for a particular category.

    Many thanks,
    Carolyn

    For any one who wants to do the same thing. Here’s the shortcode for it.

    [display-posts category="replace this with the name of your category" posts_per_page="number of posts you want to show"]

    And this link helped me a lot. https://en.support.wordpress.com/display-posts-shortcode/

  • The topic ‘How to display recent post for a specific category’ is closed to new replies.