admin管理员组

文章数量:1415139

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..

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..

Share Improve this question edited Aug 26, 2019 at 9:28 Glorfindel 6093 gold badges10 silver badges18 bronze badges asked Jan 25, 2013 at 1:55 BatmanBatman 1451 silver badge16 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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;

本文标签: custom fieldHow can i display on front page a movie that is atached in a post type