0

I'm building a cinema site,

  • In posts I store the Movies
  • I have a post_type name "box-office" , this post type have 1-10 custom fields, with i can select the movies stored in post's. locul_1 - is one of that.

How can I display on front page the movie attached to that custom field I want to display on front page 5 movies In post type BoxOffice I have 2 categories (box-office-sua, and another one)

Custom Fields : locul_1,locul_2,locul_3,locul_4,locul_5

In addition, if somoene knows how to do that, I want when I add new post on Post Type Box-Office, I want in front page to automate change with last post from Box-Office, let say category box-office-sua

See image:

What is retrieving the code down? See:

<?php

$posts = get_posts(array(
    'post_type' => 'box-office',
    'meta_key' => 'locul_1',
));

if( $posts ):
foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>

<div class="bi">
<?php if(get_field('locul_1') != ""){foreach(get_field('locul_1') as $post_object): ?>
<div class="bi-img">
<?php
if ( ! $img = get_field('img', $post_object->ID ) )
    $img = '/wp-content/uploads/noimage1.jpg';
?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<img src="/scripts/timthumb.php?src=<?php echo urlencode($img); ?>&h=55&w=40&zc=1" alt="<?php the_title(); ?>"  title="<?php the_title(); ?>"/></a></div>
<div class="bi-po">1.<span class="bi-b">
<?php $key="incasari_totale"; echo get_post_meta($post_object->ID, $key, true); ?> <b>$</b>
<img class="bi-u" src="/wp-content/themes/movies/images/<?php $key="box_clasament_img"; echo get_post_meta($post->ID, $key, true); ?>" alt="Tip Clasament Box Office" title="Tip Clasament Box Office"/></span></div>
<div class="bi-linie"></div>
<div class="bi-ti"><a href="<?php echo get_permalink($post_object->ID); ?>">
<?php echo get_the_title($post_object->ID) ?></a></div>
<div class="bi-tiro"><?php $key="titluro"; echo get_post_meta($post_object->ID, $key, true); ?></div>
<?php endforeach;}?>
</div>

<?php
    endforeach;
    wp_reset_postdata();
    endif; 
?>

Solved..

4
  • Have you read the ACF Documentation for the Relationship field? All the code you need is there.
    – Milo
    Commented Jan 25, 2013 at 2:24
  • yes, i read it, but im not so good with php, i learn day by day, in not understand so good the code, so i'm asking for a little help, if i knew i did't post here...
    – Batman
    Commented Jan 25, 2013 at 2:40
  • But at least you can try to copy the code there and apply to your problem, then it's not "write the code for me" but "I tried this, why it doesn't work?". The second type receives more answers than the first.
    – brasofilo
    Commented Jan 25, 2013 at 3:11
  • hy bransofilo, i update the code but it retrive me the post from last post 1 iulie-7 iulie 2012 (please see the picture).. i now see somewone answerr the question, i give that a try.. remember i do not want the code to be exact, i try for a few hours before i add here a question. Thanks for the answer
    – Batman
    Commented Jan 25, 2013 at 3:23

1 Answer 1

0

Here's the code from the ACF Relationship Field example adapted to your field name. I edited out some of the markup just to simplify things.

A couple of notes:

  1. Use a proper text editor and indent your code so it is readable. A soup of random indentation makes it much more difficult to debug code.

  2. The ACF Relationship Field lets you associate multiple posts in a single field, so having 5 or 10 different fields is unnecessary and just adds extra queries.

.

<?php

$posts = get_field('locul_1');

if( $posts ):
    foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>

        <div class="bi">
            <div class="bi-img">
                <a href="<?php the_permalink(); ?>">
                    <img src="/scripts/timthumb.php?src=<?php the_field('img'); ?>&h=55&w=40&zc=1" alt="<?php the_title_attribute(); ?>"  title="<?php the_title_attribute(); ?>"/>
                </a>
            </div>
        </div>

    <?php
    endforeach;
    wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
endif;
2
  • thanks, i solved .. it need a little modification to adapt to my theme. I update the post. Thanks Milo.
    – Batman
    Commented Jan 25, 2013 at 3:30
  • how can i addapt this to get the content with the number.. Ex: 1,2,3,4,5,6 - first movie,second.. Now to retrive the number <div class="bi-po">1.<span class="bi-b"> of the movie. how can i get it to display acording to the insert of the movie in advance custom fields?
    – Batman
    Commented Apr 15, 2013 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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