0

I am using the ACF plugin to create static (non-ecommerce) product pages.

I am now making a page that lists all the sub pages with product name and the image added to the product_logo custom field. I found some code that almost did everything I wanted:

                <?php
                //get children of page 241 and display with custom fields

                $args=array(
                  'post_parent' => 825,
                  'post_type' => 'page',
                );
                $my_query = null;
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() ) {

                  while ($my_query->have_posts()) : $my_query->the_post(); ?>
                        <div>
                            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>

                                <!-- here I get the 'product_short_description' custom field's data, assign it to the $meta_one variable and echo it out !-->
                                <?php $meta_one = get_post_meta($post->ID, 'product_short_description', true); ?>
                                <span><?php echo $meta_one; ?></span>

                                <!-- here I get the 'product_logo' custom field's data, assign it to the $meta_two variable and echo it out !-->
                                <?php $meta_two = get_post_meta($post->ID, 'product_logo', true); ?>
                                <span><img src="<?php echo $meta_two; ?>" /></span>
                            </a>
                        </div>
                   <?php endwhile; } ?>
                <?php wp_reset_query();  // Restore global post data stomped by the_post().?>

The second field $meta_two gets the product_logo field containing the ID of the image. I wanted to convert this to a URL using:

  <?php wp_get_attachment_image_src($meta_two, null, true); ?>

But this just gives me a blank URL.

Full code below:

                $args=array(
                  'post_parent' => 825,
                  'post_type' => 'page',
                );
                $my_query = null;
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() ) {

                  while ($my_query->have_posts()) : $my_query->the_post(); ?>
                        <div>
                            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>

                                <!-- here I get the 'product_short_description' custom field's data, assign it to the $meta_one variable and echo it out !-->
                                <?php $meta_one = get_post_meta($post->ID, 'product_short_description', true); ?>
                                <span><?php echo $meta_one; ?></span>

                                <!-- here I get the 'product_logo' custom field's data, assign it to the $meta_two variable and echo it out !-->
                                <?php $meta_two = get_post_meta($post->ID, 'product_logo', true); ?>
                                <span><img src="<?php wp_get_attachment_image_src($meta_two, null, true); ?> " /></span>
                            </a>
                        </div>
                   <?php endwhile; } ?>
                <?php wp_reset_query();  // Restore global post data stomped by the_post().?>

Can anyone suggest what I am doing wrong?

0

I ended up with this

                    <?php
                    //get children of page 241 and display with custom fields

                    $args=array(
                      'post_parent' => 825,
                      'post_type' => 'page',
                    );
                    $my_query = null;
                    $my_query = new WP_Query($args);
                    if( $my_query->have_posts() ) {

                      while ($my_query->have_posts()) : $my_query->the_post(); ?>
                            <?php $img = wp_get_attachment_image_src( get_post_meta($post->ID, 'product_logo', true)); ?>
                            <?php $alt_text_for_logo = get_post_meta($post->ID, 'alt_text_for_logo', true); ?>
                            <div class="column small-6 medium-4 product">
                                <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
                                    <span> <img src="<?php echo $img[0]; ?>" alt="<?php echo $alt_text_for_logo; ?>" /></span>
                                    <h2><?php the_title(); ?></h2>
                                </a>
                            </div>
                       <?php endwhile; } ?>
                    <?php wp_reset_query();  // Restore global post data stomped by the_post().?>
            </div>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.