Wordpress displaying custom post types and their fields

25,634

Solution 1

You can use get_post_meta to pull in your fields as needed. Inside your loop, you can start with the following:

<?php 
    $release_artist = get_post_meta($post->ID, 'release_artist', true); 
    $release_title = get_post_meta($post->ID, 'release_title', true); 
?>

<ul>
    <li class="release_artist">
        <?php echo $release_artist; ?>
    </li>
    <li class="release_title">
        <?php echo $release_title; ?>
    </li>
</ul>

Solution 2

Are those custom fields? If yes, try what codex.wordpress.org is saying. Better yet, try ACF plugin.

-- edit

If you want to display parts of your pages on other ones (eg. on your home), you need to use query_posts. This is fairly simple function. For your loop, try something like this:

      <?php
        global $wp_query;
        query_posts(array(
            'post_type' => 'releases'
        ));
        while(have_posts()) : the_post(); ?>
            <?php $key = get_post_meta($post->ID, 'Opis nazwy'); ?>
            <li <?php post_class(); ?>><a href="<?php the_permalink(); ?>"><?php if($key) { echo $key[0]; } else { the_title(); }; ?></a></li>
            <?php
        endwhile;
        wp_reset_query();
        ?>

$key is a single value, here set to release_artists. It's purely for testing. If it works - feel free to define your own variables.

Solution 3

You should use:

<?php the_field('field_name') ?>

inside your loop. Hope it helps!

Solution 4

From most of the documentation I've seen online, query_posts shouldn't be the go-to function for creating custom queries and loops. The following code snippet might be a good starting point. You should be able to use this inside or outside of the main loop of your themes template files.

$args = array(
    'post_type' => 'release', //remember this is-case sensitive
    'posts_per_page' => -1,
);

$releaseQuery = new WP_Query( $args );

if ( $releaseQuery->have_posts() ) :
while ( $releaseQuery->have_posts() ) :
$releaseQuery->the_post();

// Fetching the post ID for demonstration and for use later
$c_id = get_the_ID();

// After running the_post(), alot of the Wordpress functions (not all) can now be used without supplying the post ID.    
echo get_the_title();
// You could also have used get_the_title($c_id);

// Then:
echo get_post_meta($c_id, 'release_title', true); 
echo get_post_meta($c_id, 'release_artist', true); 

endwhile;
endif;

// Return to the current page's main query
wp_reset_query();

// This should now display the page's title
the_title();
Share:
25,634

Related videos on Youtube

lowercase
Author by

lowercase

Updated on May 29, 2020

Comments

  • lowercase
    lowercase almost 4 years

    I have set up a Custom Post Type called 'RELEASES' - think music cd release.

    This post type has fields named 'release_artist', 'release_title', 'release_date', 'release_artwork' and 'release_tracklisting' for entering all of the relevant music cd information.

    I am having real trouble actually displaying this information in my Wordpress template. I have really only had luck outputting a list of the titles and none of the other data.

    Any idea what I put in the LOOP to display all of the information? Preferably each in its own LIST item so I can style each separately?

    Any thoughts greatly appreciated.

  • lowercase
    lowercase almost 12 years
    yes they are custom fields - I have that part sorted - I just can't display the results on my site.
  • Tomek Buszewski
    Tomek Buszewski almost 12 years
    What exactly are you doing? You should be able to simply echo them in theme files. Best way is to create a single page for those particular post types (like single-releases.php) and there you can use echo get_post_meta($post->ID, 'release_artist').
  • lowercase
    lowercase almost 12 years
    This just doesn't work. I can't work out why. Is the 'ID' in your above example dynamically generated? Or are you suggesting I hard code the ID for each one?
  • lowercase
    lowercase almost 12 years
    Unfortunately I can't get this to work. I think it has something to do with the 'ID' aspect. Where does that 'ID' come from? Could you put that above code ina simple loop for me? I am starting tho think I have it in the wrong place in the loop (or the loop itself is incorrect).
  • Ryan
    Ryan almost 12 years
    If you provide me with your current loop, I can take a look and see what could be wrong. If not, I should have some time tomorrow to write up a more in depth example.
  • lowercase
    lowercase almost 12 years
    In case my question didn't make sense, I am looking to display the 6 most recent music/cd releases on the home page of my site. So each would contain artist name, album title, picture of album artwork and a link/buy it button. I have not been using my homepage template to put this info into and NOT a custom post type specific template.
  • Tomek Buszewski
    Tomek Buszewski almost 12 years
    I have read your additional comments from Ryan's answer. If you want to display it on your home page, you have to use query_posts. I've edited my answer.
  • Tomek Buszewski
    Tomek Buszewski almost 12 years
    Are you sure these posts are published? Anyway, post your code.
  • lowercase
    lowercase almost 12 years
    We are getting somewhere! Thanks again for your help on this. I can now output one of the variables using your above code. As this is delivered via the KEY variable though - how do I list multiple custom field values? Do i need to specify new KEYS? For example... <?php $key1 = get_post_meta($post->ID, 'release_title'); ?> <?php $key2 = get_post_meta($post->ID, 'release_date'); ?> <?php $key3 = get_post_meta($post->ID, 'release_artwork'); ?>
  • Subrata Sarkar
    Subrata Sarkar over 7 years
    For me get_post_meta works as intended. $into = get_post_meta($post->ID, 'introduction', true); $how_to_reach = get_post_meta($post->ID, 'how_to_reach', true); and then I can display them as echo '<p>'.$into.'</p>'; '<p><b>How to reach: </b>'.$how_to_reach.'</p>';