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

Wordpress Question: I'm trying to dynamically call my logo using the custom posts and custom fields plugins in wordpress

I'm using 2 plug-ins: Custom Fields, and CPT UI. I created a new post type called logo with a field for an image upload and hit it with my logo file as a .png and completed the post.

I understand it would be WAY easier to just insert the logo using html and CSS but I'm playing around trying to learn.

So here I'm trying to call the logo file up using a wordpress loop

<?php 
    $args = array(
        'post_type' => 'logo', 'posts_per_page' => 1
    );

    $the_query = new WP_Query( $args );
    ?>

<?php if ( have_posts() ) : while ($the_query->have_posts()) : $the_query->the_post(); ?>

<a href="<?php bloginfo( 'siteurl' ); ?>"><img src="<?php the_field( 'logo' ); ?>"> 

My code is breaking the page. My WP_Query object doesn't break the page and neither does the anchor and image tag.

So everything goes fine I suppose until I insert the wordpress loop which breaks the page. Any help would be so appreciated.

4 Answers

Kevin Korte
Kevin Korte
28,149 Points

Is this the entirety of your loop? Your not ending you while loop or your if statement, and that could be breaking the page.

Thanks Kevin, that fixed part of it. I'm still struggling to recall the logo field in any case. it creates an element now that I've added the endwhile and endif but no image is appended.

<?php 
                    $args = array(
                        'post_type' => 'logo'
                        );

                        $the_query = new WP_Query( $args );
                    ?>

                    <?php if ( have_posts() ) : while ($the_query->have_posts()) : $the_query->the_post(); ?>

                    <a href="<?php bloginfo( 'siteurl' ); ?>"><img src="<?php the_field( 'mylogo' ); ?>">
                    <?php endwhile; endif; ?>

I figured out my issue. I had the custom post type installed but hadn't correctly linked it into my custom post so... I was calling the_field but it wasn't able to call any fields.