0

I'm currently building a college football blog and I have 2 custom post types: conference and team.

Each conference post type will be a horizontal list consisting of each teams' logo and will be displayed on every single team post so long as the team is in the respective conference. It will look virtually identical to this:

Horizontal List for Conference Post Type Example

Using the ACF plugin, I've given each team post type a Conference field from which the user can select the conference the team belongs in.

Conference for Team Post Type

Each conference post has the respective HTML already for its content. My question is how do I proceed to make what I just described a reality using PHP? I'm a PHP newb so if the answer is obvious, please forgive me.

The website from which I got this idea is D1ARugby.com. The example of what I'm trying to achieve is as follows:

D1ARugby.com Team Page Example

Any help would be greatly appreciated! Thank you!

UPDATE

Here's the current template I'm using for my single-team post. It works for conferences whose name is just a single word and not multiple words.

<?php
    // get team posts
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $team_args = array(
        'posts_per_page'   => 1,
        'post_type'        => 'team',
        'post_status'      => 'publish',
        'suppress_filters' => true,
        'paged'            => $paged
    );
    $conf_args = array(
        'posts_per_page'   => 1,
        'post_type'        => 'conference',
        'post_status'      => 'publish',
        'suppress_filters' => true,
        'paged'            => $paged
    );

    $teams = get_posts($team_args);
    $conferences = get_posts($conf_args);

    foreach($teams as $team) : setup_postdata($team);
?>

    <section id="conference" class="row">

        <?php foreach($conferences as $conf) : setup_postdata($conf); ?>
        <div class="conf-header">
            <?php
                $conference_name = get_post_meta( get_the_id(), 'conference', true );
                $conference = get_page_by_title( $conference_name, OBJECT, 'conference' );
            ?>
            <h1 class="conference-title"><?php echo $conference_name; ?></h1>
            <?php 
                if ($conference) {
                    echo apply_filters( 'the_content', $conference->post_content );
                }
            ?>
        </div>
        <?php endforeach; ?>

    </section>

I can figure out why it will display for conferences named "California" and "West" but it won't display for conferences named "Big Ten" or "Red River." Any ideas as to why this is happening would be greatly appreciated.

Thanks everyone!

  • 2
    You'll need to jump into the post types single template file and write yourself a WP_Query that will pull post(s) and then you can Loop through them to display the content or whatever you need. – Howdy_McGee Sep 2 '14 at 14:09
  • Any answer for this will need to mention the template hierarchy, what post meta is, and how ACF uses it – Tom J Nowell Sep 2 '14 at 14:16
1

The code you need will vary slightly depending on whether the Conference field is storing a text string (like "Big Ten") vs. the ID of the Conference post. I'll include both:

Text String Example

// place this code inside the loop of your single-team template
$conference_name = get_post_meta( get_the_id(), 'conference', true );
$conference = get_page_by_title( $conference_name, OBJECT, 'conference' );

if ($conference) {
    echo apply_filters( 'the_content', $conference->post_content );
}

ID Example

// place this code inside the loop of your single-team template
$conference_id = get_post_meta( get_the_id(), 'conference', true );
$conference = get_post( $conference_id );

if ($conference) {
    echo apply_filters( 'the_content', $conference->post_content );
}
  • Thanks for the response! Not sure if this will help but I've gone ahead and created custom taxonomies named Conferences grouping the teams in their designated conferences along with the conference post types as well. Is using taxonomies an easier way of achieving what I'm going for? Using your method, I was able to get all the conference logos to load but it was every conference loading at once. Where am I going wrong? – DaveyJake Sep 2 '14 at 17:50
  • Huh, that's strange. It should only show the content from the Conference that you've associated with the current team. Can you edit your question to include the code from your Single Team template? Using a taxonomy to group the teams is certainly one way to go, but I think it probably adds a layer of complexity that you don't need. It sounds like you're really close to getting it sorted out, but for some reason it's selecting multiple conferences instead of one. – Scott Nelle Sep 2 '14 at 19:21
  • I've updated my question to include the template I'm currently using. Got it to work for conferences but only if it's a single word name and not a multiple word name. – DaveyJake Sep 3 '14 at 4:14
  • I've updated my question and my example. Any ideas would be much appreciated. – DaveyJake Sep 8 '14 at 14:03
0

Figured out the answer! It turns out that in ACF, if you're using radio buttons and have key set for each value, the keys CAN in fact contain spaces! By matching the keys to the values (case-insensitive) I was able to get the conferences to display on the single-team posts.

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.