Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

missgeekbunny
missgeekbunny
37,033 Points

Custom Content isn't showing up

I feel like I have to have some kind of error in my code because it won't get the title and the image for my custom pages. Here is what I have, can anyone see the flaw in my code?

        <pre><?php 
        /* Template Name: special-cake */
        get_header(); ?>
        <?php get_sidebar(); ?>
        \<div class="twelve main clearfix">
        <?php 
            $args = array(
            'post_type' => 'special-cake');
            $the_query = new WP_Query($args); ?>
            <?php   if(have_posts()): while( $the_query       -> have_posts()): $the_query -> the_post();?>
        <div class="three column product">
        \<h3><?php the_title(); ?>\</h3>
        \<a href="<?php the_permalink(); ?>">        \<img src="<?php the_field('image'); ?>" />\</a>
        </div>
        <?php endwhile; ?>
        <?php   else:   ?>
            \<p> No cakes here.\</p>
        <?php endif; ?>
    </div>
    <?php get_footer(); ?></pre>

Please excuse any formatting mistakes and \. I'm not very good with markdown I'm afraid.

ETA: The page I'm having issues with is at www.bunnieblogsdevelopment.com/sweetestscentsations/special-cake

2 Answers

missgeekbunny
missgeekbunny
37,033 Points

Thanks for trying Jeremy Germenis. I actually discovered the error this morning. It was a - and _ mix up. It should have been 'post_type' => 'special_cake'

Jeremy Germenis
Jeremy Germenis
29,854 Points

Try this for the image:

<?php
//$image is an array with the data for custom field 'image'
$image = get_field('image');
//$imageUrl is corresponding value of the $image array key 'url'
$imageUrl = $image['url'];
//if we've found what were looking for do something!
if( $imageUrl ):
    ?><img src="<?php echo $imageUrl; ?>" alt="" /><?php
endif;
?>

For the title you can do something similar and just do a var_dump of your new array to see what goodies are inside:

$title = get_field('title');
var_dump($title);