【问题标题】:Wordpress: Display image attachments from child pages of a specific pageWordpress:显示来自特定页面的子页面的图像附件
【发布时间】:2019-05-23 17:51:52
【问题描述】:

我正在尝试在特定父页面的子页面上显示所有图像附件,即第 10、11 和 12 页上的所有图片。

Projects (Page ID: 5)
- Project 1 (Page ID: 10)
- Project 2 (Page ID: 11)
- Project 3 (Page ID: 12)

这是我目前所拥有的,它可以显示网站上的所有图像:

<?php 

    $args = array(
        'post_parent' => 0,
        'post_type'   => 'attachment', 
        'numberposts' => -1
    );

    $images = get_children( $args );

    if ( empty($images) ) {
        // no attachments here
    } else {
        foreach ( $images as $attachment_id => $attachment ) {
            echo wp_get_attachment_image( $attachment_id, 'full' );
        }
    }

?>

但是,如果我添加帖子父 ID (5),则什么也不会出现:

<?php 

    $args = array(
        'post_parent' => 5,
        'post_type'   => 'attachment', 
        'numberposts' => -1
    );

    $images = get_children( $args );

    if ( empty($images) ) {
        // no attachments here
    } else {
        foreach ( $images as $attachment_id => $attachment ) {
            echo wp_get_attachment_image( $attachment_id, 'full' );
        }
    }

?>

任何建议都会很有帮助!

【问题讨论】:

    标签: php html wordpress foreach wordpress-theming


    【解决方案1】:

    我认为 get_children() 从单个父帖子或全部返回对象(将 0 作为值传递)。

    您可以尝试使用嵌套的 foreach 先获取所有帖子子项,然后逐页查询附件。这是一个未经测试的样本,但它给了你一个想法:

    <?php 
    
    $subpages_args = array(
        'post_parent' => 5,
        'post_type'   => 'page', 
        'numberposts' => -1
    );
    
    $sub_pages = get_children( $subpages_args );
    
    foreach( $sub_pages as $subpage_id => $sub_page) {
    
        $args = array(
            'post_parent' => subpage_id,
            'post_type'   => 'attachment', 
            'numberposts' => -1
        );
    
        $images = get_children( $args );
    
        if ( empty($images) ) {
            // no attachments here
        } else {
            foreach ( $images as $attachment_id => $attachment ) {
                echo wp_get_attachment_image( $attachment_id, 'full' );
            }
        }
    
    }
    ?>
    

    祝你好运:)

    【讨论】:

      【解决方案2】:

      这是一个基于 SQL 查询的解决方案。在主题的functions.php中复制以下函数

      function get_children_page_attachments($parent_id) {
          global $wpdb;
          // just a precautionary typecast and check 
          $parent_id = intval( $parent_id );
          if( empty($parent_id) ) return [];
      
          $query = "SELECT ID FROM {$wpdb->posts} P WHERE post_type='attachment' AND P.post_parent IN (SELECT ID FROM {$wpdb->posts} WHERE post_parent={$parent_id} AND post_type='page' )";
          return $wpdb->get_results($query, 'ARRAY_A');
      }
      

      一旦你在你的functions.php中有上述函数,那么你可以像这样使用它:

      // for example the parent page id is 16
      $image_ids = get_children_page_attachments(16);
      foreach($image_ids as $image_id) {
          $image = wp_get_attachment_image($image_id['ID'], 'full');
      }
      

      【讨论】:

        猜你喜欢
        • 2014-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-08
        • 1970-01-01
        • 1970-01-01
        • 2013-03-09
        • 1970-01-01
        相关资源
        最近更新 更多