Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

Steven Price
Steven Price
15,398 Points

Problem getting custom fields to work with child theme php

Hey, I have been trying to use some of the code from Shirts4Mike as a template to display an about page for a band and its members.

I was able to get the code to work in a html site, but then I tried converting it into a wordpress child theme that calls on custom fields and have not been able to get it to work.

I have a file called "members.php" in an inc folder with the following code:

<?php

function get_list_view_html($member_id, $member) {

    $output = "";

    $output = $output . "<li>";
    $output = $output . '<a href="localhost/themilleniumDEMO.com/band/' . $member_id . '">';
    $output = $output . "<img ";
    $output = $output . 'id="' . $member["modCSS"] . '"'; 
    $output = $output . ' src="' . $member["img"] . '" alt="' . $member["name"] . '">';
    $output = $output . "</a>";
    $output = $output . "<p>" . $member['name'] . "</p>";
    $output = $output . "</li>";

    return $output;
}

$members = array();
$members["the-millenium"] = array(
    "name" => echo "'" . the_field('band_name') . "'",
    "url_name" => echo "'" . the_field('band_name_url') . "'",
    "modCSS" => "user1",
    "img" => echo "'" . the_feild('band_profile_image') . "'",
    "spotify_user" => echo "'" . the_field('spotify_user_band') . "'",
    "spotify_playlist" => echo "'" . the_field('spotify_playlist_id_band') . "'",
    "instagram" => echo "'" . the_field('instagram_id_band') . "'",
    "twitter" => echo "'" . the_field('twitter_id_band') . "'"
);
$members["matt-hasenmueller"] = array(
    "name" => echo "'" . the_field('name_1') . "'",
    "url_name" => echo "'" . the_field('name_1_url') . "'",
    "modCSS" => "user2",
    "img" => echo "'" . the_field('profile_image_1') . "'",
    "spotify_user" => echo "'" . the_field('spotify_user_1') . "'",
    "spotify_playlist" => echo "'" . the_field('spotify_playlist_id_1') . "'",
    "instagram" => echo "'" . the_field('instagram_id_1') . "'",
    "twitter" => echo "'" . the_field('twitter_id_1') . "'"
);
$members["kyle-culver"] = array(
    "name" => echo "'" . the_field('name_2') . "'",
    "url_name" => echo "'" . the_field('name_2_url') . "'",
    "modCSS" => "user3",
    "img" => echo "'" . the_field('profile_image_2') . "'",
    "spotify_user" => echo "'" . the_field('spotify_user_2') . "'",
    "spotify_playlist" => echo "'" . the_field('spotify_playlist_id_2') . "'",
    "instagram" => echo "'" . the_field('instagram_id_2') . "'",
    "twitter" => echo "'" . the_field('twitter_id_2') . "'"
);
$members["sean-koran"] = array(
    "name" => echo "'" . the_field('name_3') . "'",
    "url_name" => echo "'" . the_field('name_3_url') . "'",
    "modCSS" => "user4",
    "img" => echo "'" . the_field('profile_image_3') . "'",
    "spotify_user" => echo "'" . the_field('spotify_user_3') ."'",
    "spotify_playlist" => echo "'" . the_field('spotify_playlist_id_3') . "'",
    "instagram" => echo "'" . the_field('instagram_id_3') . "'",
    "twitter" => echo "'" . the_field('twitter_id_3') . "'"
);
$members["kyle-featherstone"] = array(
    "name" => echo "'" . the_field('name_4') . "'",
    "url_name" => echo "'" . the_field('name_4_url') . "'",
    "modCSS" => "user5",
    "img" => echo "'" . the_field('profile_image_4') . "'",
    "spotify_user" => echo "'" . the_field('spotify_user_4') . "'",
    "spotify_playlist" => echo "'" . the_field('spotify_playlist_id_4') . "'",
    "instagram" => echo "'" . the_field('instagram_id_4') . "'",
    "twitter" => echo "'" . the_field('twitter_id_4') . "'"
);

I then have a "single-bands.php" page with the following code:

<?php
/** * Template Name: Single Bands Page Template */ 

get_template_part("members");
get_header();

?>
    <div class="wrapper section-inner">

        <div class="content left">

            <div class="section members page">

                <div class="wrapper">

                    <ul class="members">
                        <?php foreach($members as $member_id => $member) { 
                                echo get_list_view_html($member_id,$member);
                            }
                        ?>
                    </ul>

                    <div class="top wrapper">

                        <div class="member-picture">

                            <span>

                                <img src="<?php echo $member["img"] ?>" alt="<?php echo $member["name"]; ?>"> 

                            </span>

                        </div>

                        <div class="band instagram">

                            <p>Instagram</p>

                            <!-- www.intagme.com -->
                            <iframe src="http://www.intagme.com/in/?<?php echo $member["instagram"] ?>==" allowTransparency="true" frameborder="0" scrolling="no" style="border:none; overflow:hidden; width:495px; height: 330px" ></iframe>

                        </div>

                    </div><!--top wrapper -->

                    <div class="bottom wrapper">

                        <div class="band spotify">

                            <p>Spotify Playlist</p>

                            <iframe src="https://embed.spotify.com/?uri=spotify:user:stevenjacobprice:playlist:0WidomzRQbkOBYzDqIMhqW" width="315" height="380" frameborder="0" allowtransparency="true"></iframe>

                        </div>

                        <div class="band twitter">

                            <p>Twitter</p>

                            <a class="twitter-timeline"  href="https://twitter.com/stevenprice_"  data-widget-id="505855025675911168">Tweets by @stevenprice_</a>
                            <script>
                                !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
                            </script>

                        </div>

                    </div> <!--bottom wrapper-->

                </div>

            </div>

        </div> <!-- content left -->

        <?php get_sidebar(); ?>

    </div> <!-- wrapper inner-->

<?php get_footer(); ?>

From what I can tell, it seems like wordpress breaks down when I try to get the information from the "members.php" file. Will I have to create separate pages for each band member instead? I like the way it is set up for the shirts4mike page where there is a single "shirts.php" file that changes what is displayed depending on which shirt the customer selects. I'm new with wordpress and php, so I'm not sure which area I'm messing up.

Any help or advice is appreciated!

P.S. I should note that I have also tried putting the php function into the child theme's "functions.php" file which didn't work. I also tried putting all the code from the "members.php" file at the top of the "single-band.php" file, which gave me a blank white page. So I don't know if wordpress has a problem with php variables, or if I'm supposed to define them a special way, or what.

2 Answers

David Klaver
David Klaver
10,649 Points

Hi! I'm no WordPress expert, but I've noticed that your inc file spells it as "feild" when it should be "field". Hope this helps!

Steven Price
Steven Price
15,398 Points

I was really hoping that face palm of a spelling error was going to be the magic fix, but it seems as if my single-bands.php file still isn't able to call on the information stored in the arrays in the inc file at all.

Thanks for saving me another headache in the future though!

Haneen Krimly
PLUS
Haneen Krimly
Courses Plus Student 29,322 Points

Maybe it's because you didn't add a closing php ?> tag at the end of the members.php ?

Steven Price
Steven Price
15,398 Points

that didn't fix it either, even if I have this:

$members = echo '"' . the_field('band_profile_image') . '"';

at the top of the single-bands.php file the page still breaks. It seems like as soon as I try to echo the custom fields to the php variable it stops working.

Haneen Krimly
Haneen Krimly
Courses Plus Student 29,322 Points

Ok... How about you just do something like this instead

$members = '"' . the_field('band_profile_image') . '"';

echo $members;

Steven Price
Steven Price
15,398 Points

when I do that it returns:

<img alt="The Millenium" "=" src="" >