Php 加载文章的WordPress图像并在另一页上显示

Php 加载文章的WordPress图像并在另一页上显示,php,wordpress,Php,Wordpress,我在WordPress上遇到了一些问题,无法在另一个页面上显示属于某个帖子的图像。 我要找的是一个主页,上面列出某个类别内的所有帖子,并显示标题、摘录和“查看示例”链接。“查看示例”链接将在一个灯箱中显示属于帖子的所有图像,但显示在主页上 到目前为止,我有这个,但现在我有点卡住了 <?php query_posts('cat=15&order=DSC'); ?> <?php if ( have_posts() ) : while ( have_posts() )

我在WordPress上遇到了一些问题,无法在另一个页面上显示属于某个帖子的图像。 我要找的是一个主页,上面列出某个类别内的所有帖子,并显示标题、摘录和“查看示例”链接。“查看示例”链接将在一个灯箱中显示属于帖子的所有图像,但显示在主页上

到目前为止,我有这个,但现在我有点卡住了

<?php query_posts('cat=15&order=DSC'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <div class="col-md-6">
            <div class="pakket-block">
                <h3><?php the_title(); ?></h3>
                <?php the_excerpt(); ?>
                <span class="read_more"><a href="<?php the_permalink(); ?>" rel="shadowbox">View examples</a></span>    
            </div> <!-- /.pakket-block -->
        </div> <!-- /.col-md-6 -->
    <?php endwhile; endif; ?>
<?php wp_reset_postdata(); // reset the query ?>

这是一个非常粗略的示例(无法在我当前的环境中测试)

这将为您获取附加到页面的所有图像提供正确的方向

将此函数放入functions.php:

    function get_match( $regex, $content ) {
        preg_match($regex, $content, $matches);
        return $matches[1];
    } 
在模板文件中:

    query_posts('cat=15&order=DSC');
    if ( have_posts() ) : while ( have_posts() ) : the_post();


        // Extract the shortcode arguments from the $page or $post
        $shortcode_args = shortcode_parse_atts(get_match('/\[gallery\s(.*)\]/isU', $post->post_content));

        $ids = $shortcode_args["ids"];

        // get the attachments specified in the "ids" shortcode argument
        $attachments = get_posts(
            array(
                'post_parent' => $post->ID,
                'post_status' => 'inherit', 
                'post_type' => 'attachment', 
                'post_mime_type' => 'image', 
                'order' => 'menu_order ID', 
                'orderby' => 'post__in',
            )
        );


        print_r($attachments);


    endwhile; endif; 
    wp_reset_postdata();

不要使用
query\u posts
进行其他post查询。见:

你所需要的可以用2,1来抓取类别中的帖子。第二个,在第一个循环中,使用带有第一个循环ID的
post_parent
(如Richard Denton的回答)获取附件

您可以在
functions.php
中创建自己的函数来完成这项工作。因此,在模板文件中,您只需要(通用代码大纲):

<?php print_category_15(); ?>
function print_category_15() {
    $posts = get_posts( $your_arguments );
    if( $posts ) {
        foreach ( $posts as $post ) {
            // Print titles and excerpts
            print_childrens_of_15( $post->ID );
        }
    }
}

function print_childrens_of_15( $parent ) {
    $children = get_posts( $your_arguments_for_attachments );
    if( $children ) {
        foreach ( $children as $child ) {
            // Print your hidden divs to be used in the ShadowBox
        }
    }
}