Support

Account

Home Forums General Issues How to display a value from a parent page?

Solving

How to display a value from a parent page?

  • Hey everyone!

    I’m using this awesome plugin. I’s been helping me a lot so far but now I have a quick question. It may have been aswerd before but I couldn’t find a solution that was working for me so far.

    I have a parent page with multiple children. The children each have a sidebar that should display a file download link. The value for this field is set in the backend of the parent page.

    This is my code so far (displaying a value set manueally on each child manually):

    <?php 
    
    $attachment_id = get_field('broschuren-download');
    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id );
    
    if( get_field($post->post_parent, 'broschuren-download') ):
        ?><div class="pdf-download">	<a href="<?php the_field('broschuren-download'); ?>" >Broschüre herunterladen</a>
    </div><?php
    endif;
    
    ?

    Do you guys have an idea how to master this? Thanks in advance!

  • maybe that works / help you

    <?php
    $post_ID = get_the_ID();
    $parentpost_id = wp_get_post_parent_id( $post_ID );
    $attachment_id = get_field( "broschuren-download", $parentpost_id ); //use parents-post field: "broschuren-download"
    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id );
     ?><div class="pdf-download"><a href="<?php echo $url; ?>" >Broschüre herunterladen</a></div>
  • Thanks mediawerk,

    it now does show the div but only links to the current url insead of the field on the parent page. Have you got any idea what’s wrong? Must be something small…

    Sven

  • parent post have a filled broschuren-download file-field? (no subfield of a repeater/flex!)
    do you use id or url or object at file-field?

    and what are the values of the different variables?

    <?php
    $post_ID = get_the_ID();
    echo ' id:'. $post_ID;
    $parentpost_id = wp_get_post_parent_id( $post_ID );
    echo ' parentid:'. $parentpost_id;
    $attachment_id = get_field( "broschuren-download", $parentpost_id ); //use parents-post field: "broschuren-download"
    echo ' attachment_id:'. $attachment_id;
    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id );
     ?><div class="pdf-download"><a href="<?php echo $url; ?>" >Broschüre herunterladen</a></div>
  • Thanks again!

    Yes the parent post does have a filled field. Return value for this field is URL.

  • if it is url, than:

    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id )

    is false for sure. because it expect you work with ID 😉

    you didn’t answer what values you get from the echos.
    what was output of my code from last post?
    (do you get correct ID and ParentID? and what did you get from get_field?)

    with URL you may can use $attachment_id direct

  • $attachment_id did indeed work! Thanks a lot!

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘How to display a value from a parent page?’ is closed to new replies.