WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom post type displayed by wp_query (8 posts)

  1. micasuh
    Member
    Posted 3 years ago #

    I have created a custom post type that we'll call firemen that I established in functions.php. These post types do not support the classic editor b/c they are intended to be used for individual profiles. Thus, I have enabled 'custom-fields'.

    Here's an example of the code I established:

    function post_type_firemen() {
    	register_post_type( 'firemen',
    		array(
    			'label' => __('Firemen'),
    			'public' => true,
    			'show_ui' => true,
    			'supports' => array(
    				'title',
    				'thumbnail',
    				'page-attributes',
    				'post-thumbnails',
    				'custom-fields')
    		)
    	); //end register_post_type
    	register_taxonomy( 'firemen', 'firemen', array( 'hierarchical' => true, 'label' => __('Firemen') ) );
    }
    add_action('init', 'post_type_firemen');

    I'd like to do something like the following:

    <?php
    $firemen = new WP_Query('post_type=firemen');
    if ($firemen->have_posts():
    ?>
    <ul id="firemen">
        <?php while ($firemen->have_posts()): $firemen->the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?></a>
            </li>
        <?php endwhile; ?>
    </ul>
    <?php endif; ?>

    Here's the result I desire:

    <ul id="firemen">
            <li>
                <a href="http://domain.com/bruce-wayne/">Bruce Wayne<img src="http://domain.com/wp-content/themes/theme-name/images/bruce-wayne.png" alt="Bruce Wayne's photo" width="190" height="191" /></a>
            </li>
    </ul>

    I have 10 firemen to display in list items, showing their full thumbnails (heights are the same but widths vary from photo to photo) and a link to the content which is just made up of custom fields information.

    I currently can't get the above code to work properly for some reason and it returns a blank ul.

    Will the link to "/bruce-wayne/" return the custom fields info or will I need to use a different script to call the custom fields info for each custom post type? I realize that custom-fields need to be called properly in order to display but I'm not sure if I include the_meta() in the href after the permalink or instead.

    Thanks for ANY help!!!

  2. micasuh
    Member
    Posted 3 years ago #

    Okay, after playing more, I got it down to this:

    <ul id="firemen">
        <?php query_posts('post_type=firemen'); while ($firemen->have_posts()): $firemen->the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?></a>
            </li>
        <?php endwhile; ?>
    </ul>

    Basically, I changed wp_query to query_posts and that worked. My original code was too complicated for my own good!

    Now, because of the unusual supports for my custom post type, when I click on the permalink, it takes me a 404 page. I wonder how I can make the custom fields show up in the link without actual content from the editor?

  3. fonglh
    Member
    Posted 3 years ago #

    See the function reference for get_post_meta()

  4. micasuh
    Member
    Posted 3 years ago #

    Yes, thanks fonglh but my application of this is a little more advanced than the page lets me know. I found this link myself earlier but it didn't help me with what I need.

  5. fonglh
    Member
    Posted 3 years ago #

    What exactly do you want to display in your link text?

  6. micasuh
    Member
    Posted 3 years ago #

    When the link is clicked, I only want custom fields of each custom post type to populate the browser, nothing else.

    I have filled out several names and values within the custom fields for the custom posts, but I realize that they won't display without meta function. These custom posts don't have traditional content and thus going to the permalink returns a 404.

  7. micasuh
    Member
    Posted 3 years ago #

    I do realize I could include the_meta() and link to it using a hash tag, but I don't want this content to show up until someone clicks on the hyperlink. I was hoping there's a PHP/WordPress trick I could use instead of relying on Javascript.

  8. micasuh
    Member
    Posted 3 years ago #

    I figured out my solution! Here's the final code:

    <ul id="firemen">
        <?php query_posts('post_type=firemen'); while ($firemen->have_posts()): $firemen->the_post(); ?>
            <li>
                <a href="#<?php the_ID(); ?>" class="fancybox"><?php the_title(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?></a>
    	     <div id="<?php the_ID(); ?>" class="profile"><h3><?php the_title(); ?></h3><?php if ( has_post_thumbnail() ) { the_post_thumbnail('large', array('class' => 'baseball')); } ?><?php echo(the_meta()); ?></div>
            </li>
        <?php endwhile; ?>
    </ul>

    I ended up activating the Fancybox plugin and added a div with the meta for custom fields info. In the CSS, I set #firemen to display:none; and Fancybox knew to grab that content into the overlay exactly as I intended.

    This was a neat experiment and now I gotta do any final IE/FF/Safari hacking in case something doesn't work right. Problem solved!

Topic Closed

This topic has been closed to new replies.

About this Topic