Support » Fixing WordPress » get_pages > Two arrays, one foreach

  • Hello,

    I’m wondering if it’s possible to use the get_pages function to either get on outcome or the other in the same loop.

    Let me explain a little bit further. I have these two get_pages arrays, with almost the same values, except for the ‘meta_key’. What I want is if either one of the meta_keys are the post_slug (so Artiestlink or Featuredlink), then give me the article.

    I’m building this music website with WP pages used for albums and sometimes albums have multiple artists, so I want to show an album on both of the album artists.

    Let’s say I have the album ‘Distant Relatives’ from Nas & Damian Marley. Now I want this album to appear on both Nas’ artist page and on Damian Marley’s. I’d put ‘nas’ as meta_value for ‘Artiestlink’ and ‘damian-marley’ for ‘Featuredlink’ in the custom fields of the album page. These then would be equal to the slug of the artist pages (Nas’ slug > ‘nas’, Damian’s > ‘damian-marley’.

    This is what I have so far:

    <?php $mypages = get_pages( array( 'child_of' => 127, 'sort_column' => 'post_date', 'sort_order' => 'desc', 'exclude' => $post->ID, 'meta_key' => 'Artiestlink', 'meta_value' => $slug ) ); ?>
                        <?php $mypages2 = get_pages( array( 'child_of' => 127, 'sort_column' => 'post_date', 'sort_order' => 'desc', 'exclude' => $post->ID, 'meta_key' => 'Featuredlink', 'meta_value' => $slug ) ); ?>
                        <?php if( $mypages || $mypages2 ) : ?><h2 class="alt">Discografie</h2><?php endif; ?>
                        <?php foreach( $mypages as $page || $mypages2 as $page ) { ?>
                        <article class="album-item">
                            <a href="<?php echo get_page_link($page->ID) ?>" alt="<?php echo $page->post_title ?>"><?php echo get_the_post_thumbnail($page->ID, 'xsmall', array('title' => '')); ?></a>
                            <div class="album-item-desc">
                                <a href="<?php echo get_page_link($page->ID) ?>" alt="<?php echo $page->post_title ?>"><h3><?php echo get_post_meta($page->ID, 'Titel', true); ?></h3>
                                <h4><?php echo get_post_meta($page->ID, 'Artiest', true); ?><?php if ( get_post_meta($page->ID, 'Featured', true) ) : ?> & <?php echo get_post_meta($page->ID, 'Featured', true); ?><?php endif; ?></h4></a>
                        <?php }	?>
                        <div class="clear"></div>
    					<?php wp_reset_query(); ?>

    Obviously, this isn’t going to work, because the ‘foreach’ bit isn’t written correctly, but my knowledge of php isn’t that great.

    I hope my question is clear and someone has a solution to this.

    Thanks a lot!

Viewing 1 replies (of 1 total)
  • I believe that you could combine the two arrays with array_merge(). Try replacing this:

    <?php foreach( $mypages as $page || $mypages2 as $page ) { ?>

    with this:

    <?php $allpages = array_merge($mypages, $mypages2);
    foreach( $allpages as $page ) { ?>

    Note too that $page is a WP global and you probably would be safer using $mypage instead.

Viewing 1 replies (of 1 total)
  • The topic ‘get_pages > Two arrays, one foreach’ is closed to new replies.