3

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

一点背景

每个图像都将被视为附件(从而获得它自己的单页)。我将使用专辑封面照片的内置特色缩略图(来源: http: //www.wpbeginner.com/wp-tutorials/how-to-create-a-photo-album-gallery-in-wordpress-without-插件/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,我还没有找到问题。可能是什么问题?

4

1 回答 1

1

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

于 2013-08-26T18:44:50.710 回答