Get custom post type by ID in Wordpress

29,717

Solution 1

<?php 
$ID = 3788;
$args = array('p' => $ID, 'post_type' => 'about');
$loop = new WP_Query($args);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php global $post; ?>
    <?php
    $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ),  false, '' ); ?>  
    <div class="section" style="background: url(<?php echo $src[0]; ?>) no-repeat center     center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; z-index:-1;">
    <?php the_content () ?>
<?php endwhile; ?>

Solution 2

   <?php
   $ids= array(3788); // for example, You can also pass multiple IDs in array
   $args = array('post_type' => 'about','post__in' => $ids);
   $loop = get_posts($args);
   while ( $loop->have_posts() ) : $loop->the_post();
   global $post;

   $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 
   5600,1000 ), false, '' ); ?>  
   <div class="section" style="background: url(<?php echo $src[0]; ?>) no- 
   repeat center center fixed; -webkit-background-size: cover; -moz-background- 
   size: cover; -o-background-size: cover; background-size: cover; z-index:-1;">
   <?php the_content () ?>

   <?php endwhile; ?>
Share:
29,717
user3181828
Author by

user3181828

Updated on July 09, 2022

Comments

  • user3181828
    user3181828 almost 2 years

    I'm basically looking at somehow creating a loop so I can show the content of a single custom post type based on it's ID.

    So, I want to get the content from the custom post type with an ID of 3788.

    There is a function in there to get the featured image URL too.

    For example, here's my code at the moment:

    <?php $args = array( 'post_type' => 'about', 'posts_per_page' => 1 ); ?>
    
    <?php $loop = new WP_Query( $args ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    <?php global $post; ?>
    <?php
    $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' ); ?>  
    <div class="section" style="background: url(<?php echo $src[0]; ?>) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; z-index:-1;">
    
    <?php the_content () ?>
    
    <?php endwhile; ?>
    
  • user3181828
    user3181828 almost 10 years
    Sorry, I'm getting an error on line 5, I tried ?> after $loop = new WP_Query($args); but it still didn't work.
  • Calvin deClaisse-Walford
    Calvin deClaisse-Walford almost 10 years
    I edited that mistake, and the code is working for me - are you sure all other PHP tags are closed properly?
  • user3181828
    user3181828 almost 10 years
    I tried the code, and whilst I'm not getting any errors, it is coming up blank. The ID is correct, do I need to put the ID anywhere else?
  • Calvin deClaisse-Walford
    Calvin deClaisse-Walford almost 10 years
    How are you getting the post ID? Make sure that's correct, and also see my edit where I add post type to $args.