Pull Two Different Post Types into Different HTML Containers on the Same Page

admin2025-06-02  3

If I have two different post types - say, the standard WP post type and a custom post type, how do I go about pulling them both into one page, but into two different HTML containers?

For instance I'd like one post-type to show on my home page with the standard posts (I'd set this to show the top 5 posts)

while(have_posts()){
  the_post(); ?>

// Pulls in standard WP posts

<?php } ?>

And inside a different HTML container have a custom post type called 'features', that pulls in a 'features' custom post type (which would also show the most recent posts of this post-type).

while(have_posts()){
  the_post(); ?>

// Pulls in the 'features' custom post type

<?php } ?>

** NOTE - I know how to set up the CPT in my functions.php file, it's just how I'd pull them into the same page I'd like to know.

Thanks in advance for any suggestion.

Em

If I have two different post types - say, the standard WP post type and a custom post type, how do I go about pulling them both into one page, but into two different HTML containers?

For instance I'd like one post-type to show on my home page with the standard posts (I'd set this to show the top 5 posts)

while(have_posts()){
  the_post(); ?>

// Pulls in standard WP posts

<?php } ?>

And inside a different HTML container have a custom post type called 'features', that pulls in a 'features' custom post type (which would also show the most recent posts of this post-type).

while(have_posts()){
  the_post(); ?>

// Pulls in the 'features' custom post type

<?php } ?>

** NOTE - I know how to set up the CPT in my functions.php file, it's just how I'd pull them into the same page I'd like to know.

Thanks in advance for any suggestion.

Em

Share Improve this question asked Mar 15, 2019 at 21:50 pjk_okpjk_ok 9082 gold badges15 silver badges36 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Updated 10 hours later: enriched with alternative solution and complemented with comments.

<?php
/* ******* Built-in post type "Post" ******* */
$arguments_for_posts = array(
    'numberposts' => 1, // number of posts to get
    'post_type'   => 'post' // post type
);

// get the array of post objects
$posts = get_posts( $arguments_for_posts );

// iterate through array to get individual posts
foreach( $posts as $post ) {
    // Enclosing block having unique ID
    echo '<article id="post-' . $post->ID . '">';
        // Post title (property of $post object)
        echo '<h1>' . $post->post_title . '</h1>';
        // Post content (property of $post object)
        echo $post->post_content;
    echo '</article>';
}

/* ******* Custom post type "Feature" ******* */
$arguments_for_features = array(
    'numberposts' => 1,
    'post_type'   => 'features'
);

$features = get_posts( $arguments_for_features );

foreach( $features as $feature ) {
    echo '<article id="feature-' . $feature->ID . '">';
        echo '<h1>' . $feature->post_title . '</h1>';
        echo $feature->post_content;
    echo '</article>';
}

Or you can use two WP_Queries when loops will look more familiar:

<?php
/* ******* Built-in post type "Post" ******* */
// Arguments
$args = array(
    'posts_per_page' => 1,
    'post_type'      => 'post',

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

// The loop
while ( $query_posts->have_posts() ) {
    $query_posts->the_post();
?>
    <article id="post-<?php the_ID(); ?>">
        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>
    </article>
<?php
} // END while()

// Essential!
// Set the post data back up
wp_reset_postdata();

/* ******* Custom post type "Feature" ******* */
$args = array(
    'posts_per_page' => 1,
    'post_type'      => 'feature',

);

$query_features = new WP_Query( $args );

while ( $query_features->have_posts() ) {
    $query_features->the_post();
?>
    <article id="feature-<?php the_ID(); ?>">
        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>
    </article>
<?php
} // END while()
?>

Thanks @Qaisar Feroz for the useful reminder.

Useful links:

  • get_posts()
  • WP_Query
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748801071a313831.html

最新回复(0)