Home » Wordpress » Custom Post Type isn't Appearing

Custom Post Type isn't Appearing

Posted by: admin November 30, 2017 Leave a comment

Questions:

I am relatively new to WordPress development, and I am attempting to build a site-specific plugin that gives me the ability to have a photo album gallery.

A Little Background

Each image will be treated as an attachment (thus getting it’s own single page). I will use the built-in featured thumbnails for album cover photo (Source: http://www.wpbeginner.com/wp-tutorials/how-to-create-a-photo-album-gallery-in-wordpress-without-a-plugin/comment-page-1/#comment-182006)

I essentially have four new files, algallery.php, style.css in my plugin folder and two template folders under templates of a child theme of Roots, archive-albums.php, and single-albums.php.

My problem so far is that when I have implemented get_template_part('templates/archive', 'albums'), I have my “grid” and CSS rendered, but WordPress isn’t finding my album posts I’ve created, and these posts were created with a custom post type. To give you an idea of the approach:

<li class="album-grid">
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
        <?php the_post_thumbnail('album-grid'); ?>
    </a>
</li>

<?php if ( $post->post_type == 'albums' && $post->post_status == 'publish' ) {
    $attachments = get_posts( array(
        'post_type' => 'attachment',
        'posts_per_page' => -1,
        'post_parent' => $post->ID,
        'exclude'     => get_post_thumbnail_id()
    ) );

    if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
            $class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
            $title = wp_get_attachment_link( $attachment->ID, 'album-grid', true );
            echo '<li class="' . $class . ' album-grid">' . $title . '</li>';
        }

    }
}
?>

I have WP_DEBUG on as well, and I have yet to find the issue. What may be the problem?

Answers:

What if you add a global $post; at the top of your code ?