【问题标题】:Load WordPress images of posts and show them on another page加载帖子的 WordPress 图像并在另一个页面上显示它们
【发布时间】:2013-09-15 21:59:09
【问题描述】:

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

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

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

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

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

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

    把这个函数放在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();
    

    【讨论】:

      【解决方案2】:

      请勿使用query_posts 进行其他帖子查询。见:When should you use WP_Query vs query_posts() vs get_posts()?

      您需要的可以通过 2 个get_posts 来完成,一个用于抓取该类别中的帖子。第二个,在第一个内部,使用post_parent(如理查德丹顿的回答)和第一个循环的ID来获取附件。

      您可以在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
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-17
        • 1970-01-01
        • 2019-03-16
        • 2017-11-01
        • 2013-03-09
        • 2016-04-10
        • 1970-01-01
        • 2017-07-20
        相关资源
        最近更新 更多