Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using Advanced Custom Fields plugin and microkid Related Posts. I would like to display my custom fields inside Related post This is what I got so far?

<?php //related artworks

$related = MRP_get_related_posts( $post->ID, true, false, 'quote_list' );

//for testing related array
//print_r ($related);

if(!empty($related)) {
    foreach($related as $key => $value) {
        $related = get_post($value);
        $related_id=$related->ID;
        $related_content=$related->post_excerpt;
        $related_url=$related->get_permalink;
        echo "<div><h4>".get_the_title($related_id)."</h4></div>";
        //echo get_the_post_thumbnail($related_id);
        echo "".$related_content."<br/><br/>";
        echo "<a href=".get_permalink($related_id).">read more</a>";
        //for testing related array
        //print_r ($related);
    }

}?>

But how I would pull out my advanced custom field? Many thanks,

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

According to advenced custom fields you get the value with get_field http://www.advancedcustomfields.com/docs/functions/get_field/

Just change "text_field" to your field-key.

<?php
$related = MRP_get_related_posts( $post->ID, true, false, 'quote_list' );

if( !empty( $related ) ) {
    foreach( $related as $key => $value ) { 

        // Get post
        $related = get_post( $value );

        // Get the field "text_field" on all posts
        $value = get_field( "text_field", $related->ID  );

        $output = '<div><h4>';
            $output .= get_the_title( $related->ID );
        $output .= '</div></h4>';

        // print the value from ACF

        if( $value ) {
            $output .= $value;
        }

        $output .= $related->post_excerpt;

        $output .= '<a href="'. get_permalink( $related->ID  ) .'">'. __('Read more','domain') .'</a>';
    }

    // Echo 
    echo $output;
}
share|improve this answer
    
Thanks @Pontus exactly what I was looking for :) –  jmysona Dec 22 '12 at 12:48
add comment

Use the function get_post_meta() to get data to a custom field. Include this function in your loop and set the right parameters. The first parameter is the id of the post ($related_id) , the second is the key, the meta tags key/name, and the third parameter set to true, if you get a single parameters.

To get all meta data use get_post_custom($related_id).

For identifier the data and key can you use the plugin Debug Objects, but currently only the git version.https://github.com/bueltge/Debug-Objects

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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