0

I'm having some trouble with WordPress to show images that belong to a certain post on another page. What I'm looking for is a homepage that lists all posts within a certain category and show the title, the excerpt and a 'view examples' link. The view examples link would show all images that belong to the post in a LightBox, but on the homepage.

So far I have this, but now I'm sort of stuck.

<?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 ?>
4

2 回答 2

0

不要用于query_posts额外的帖子查询。请参阅:何时应该使用 WP_Query、query_posts() 和 get_posts()?

你需要的可以用 2 来完成get_posts,一个来抓取类别中的帖子。第二个,在第一个内部,使用post_parent第一个循环的 ID(如 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
        }
    }
}
于 2013-09-17T19:33:18.753 回答
0

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

这应该使您朝着正确的方向获取附加到页面的所有图像。

将此函数放在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();
于 2013-09-17T08:19:08.507 回答