$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Pull Two Different Post Types into Different HTML Containers on the Same Page|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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
Post a comment

comment list (0)

  1. No comments so far