• Resolved kesking7

    (@kesking7)


    Hi,

    I would like to display only a few news&blog posts on my home page. Therefore, If a user interest these posts can direct to the new&blog pages.

    I’ve written a code so far but the issue is that calling is not from a specific category. I mean, it displays posts from the whole category.

    Here is my code. Thank’s in advance.

    $args = ( array(
        'category_name'  => 'news',
        'posts_per_page' => 1
    ) );
    // the query
    $the_query = new WP_Query( $args ); ?>
     
    <?php if ( $the_query->have_posts() ) : ?>
     
     
    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div class="post-text">
    <div>
    
    <?php the_title(); ?>
    
    </div>
    
    <?php the_content(); ?>
    
    <button type="button" class="btn post-text-button">Read more</button>
    </div>
        <?php endwhile; ?>
        <!-- end of the loop -->
    
        <?php wp_reset_postdata(); ?>
     
    <?php else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
    • This topic was modified 5 years ago by bcworkz.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Alan Fuller

    (@alanfuller)

    but the issue is that calling is not from a specific category. I mean, it displays posts from the whole category.

    Can you better explain, I don’t understand what you mean when you say whole category?

    Does the category ‘news’ contain children ( sub categories )

    As category_name will select the category and its children, is so use a category id instead – see https://developer.wordpress.org/reference/classes/wp_query/#category-parameters

    Thread Starter kesking7

    (@kesking7)

    Firstly, I feel sorry for being unclear. I’ll create 2 separate pages, blog & news. Also, I was planning to create 2 categories for blog & news.

    I would like to display selected featured posts that belong to these categories on the home page. Therefore, I was planning to create a “featured” category and after that, I was going to assign the posts into these categories. So I can display them on the home page.

    For instance, If I would want to change the featured posts in the future, I would like to be able to update them from the wp panel. I may be assigning one post to 2 categories and so I can display it for one news page and for one featured posts section on the home page.

    I hope that makes sense?

    Shortly, I would like to display specified posts from news & blog.

    Thanks in advance.

    Alan Fuller

    (@alanfuller)

    OK,

    Having a ‘featured’ category and using that will work.

    So I’m not sure of the specific issue. Is it you want to select ‘featured’ and ‘blog’ and ‘featured’ and ‘news’

    $query = new WP_Query( array( 'category_name' => 'featured+news' ) );

    $query = new WP_Query( array( 'category_name' => 'featured+blog' ) );

    Thread Starter kesking7

    (@kesking7)

    Alright,

    How should I implement the rest of the code after this? Should I keep it the same or sth? And also, I should write these codes to the page template that I’m using right?

    Thank you for your precious help.

    Alan Fuller

    (@alanfuller)

    Your original code started

    $args = ( array(
        'category_name'  => 'news',
        'posts_per_page' => 1
    ) );
    // the query
    $the_query = new WP_Query( $args ); ?>

    Which gives you 1 post in the category ‘news’

    If you want all posts in category featured and news use

    $args = ( array(
        'category_name'  => 'featured+news',
        'posts_per_page' => -1
    ) );
    // the query
    $the_query = new WP_Query( $args ); ?>

    ( the -1 gives all )

    you may want just published post ( rather than draft or scheduled )

    $args = ( array(
        'category_name'  => 'featured+news',
        'post_status'   => 'publish',
        'posts_per_page' => -1
    ) );
    // the query
    $the_query = new WP_Query( $args ); ?>

    see https://developer.wordpress.org/reference/classes/wp_query/#status-parameters

    The documentation is comprehensive https://developer.wordpress.org/reference/classes/wp_query/

    Thread Starter kesking7

    (@kesking7)

    OK,

    I am so thankful for your time. Have a good one sir!

    Thread Starter kesking7

    (@kesking7)

    As I progressed on this topic, I came up with this hedge.

    After I specified the number of posts per page like “‘posts_per_page’ => -1”, the posts are not displaying correctly. Other than that, I’ve tried to put the value of “1” the only error is being that all posts in this section are just calling from one content/post.

    I’ve shared the screenshot of the error here to explain better.

    Thank’s in advance.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Calling Post from Specific Category’ is closed to new replies.