0

i have a post of category (BABY HEALTH). In landing page, the recent post should appear according to above post category. The post may have different categories , i show recent post according to this categories.

2 Answers 2

0

Have you tried researching about wp_query?

Also here's a basic looping according to category :

$args = array(
    'post_type' => array('post'),
    'order' => 'DESC',
    'category_name' => 'test',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
            // Your custom code
    }
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
1
  • where you add {'category_name' => 'test'}. category name should fetch from above post category. it will change according to post category
    – Ahmad Raza
    Commented May 22, 2018 at 9:55
0

You need to replace the cat-slug with Baby Health slug.

For Simple Blog Category

<?php
    global $post;
    $args = array( 'numberposts' => 10, 'category_name' => 'cat-slug' );
    $posts = get_posts( $args );
    foreach( $posts as $post ){ 
        setup_postdata($post); 
        get_the_title();
        get_the_excerpt();
        get_the_content();
    }
?>

For Custom Post Type - Taxonomy

$wp_argu = array(
    'post_type' => 'prayer',
    'posts_per_page' => '-1',
    'suppress_filters' => false,
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'slug', // id, name
            'terms'    => 'bob',
        ),
    ),
);

$query = new WP_Query( $wp_argu );
while($query->have_posts()){ 
    $query->the_post();
        get_the_title();
        get_the_excerpt();
        get_the_content();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.