自定义帖子类型未显示

时间:2013-08-24 20:00:09

标签: php wordpress plugins

我对WordPress开发相对较新,我正在尝试构建一个特定于站点的插件,使我能够拥有相册库。

小背景

每个图像都将被视为附件(从而获得它自己的单页)。我将使用内置的精选缩略图作为专辑封面照片(来源:http://www.wpbeginner.com/wp-tutorials/how-to-create-a-photo-album-gallery-in-wordpress-without-a-plugin/comment-page-1/#comment-182006

我基本上有四个新文件,我的插件文件夹中的algallery.php,style.css和Roots,archive-albums.php和single-albums.php等子主题模板下的两个模板文件夹。

到目前为止,我的问题是当我实现get_template_part('templates/archive', 'albums')时,我有“网格”和CSS渲染,但WordPress没有找到我创建的专辑帖子,这些帖子是用自定义帖子类型。为了让您了解这种方法:

<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>';
        }

    }
}
?>

我也有WP_DEBUG,我还没有找到问题。可能是什么问题?

1 个答案:

答案 0 :(得分:1)

如果您在代码顶部添加global $post;该怎么办?