WordPress.org

Ready to get started?Download WordPress

Forums

Custom Content Type Manager
Include fields from custom type on the home page (7 posts)

  1. ChocolateTeapot
    Member
    Posted 2 years ago #

    I have a custom type called Events with a category called Forthcoming Events.

    I'd like to create a slider for forthcoming events, something like

    <ul id="forthcomingEventsSlider">
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <li>
    	<h1><?php the_title(); ?></h1>
    		<img src="<?php print_custom_field('event_image'); ?>" />
    		<?php print_custom_field('event_content'); ?>
    </li>
    <?php endwhile; // end of the loop. ?>
    </ul>

    I'm not sure how to 'load up' the Events from the home page and limit them to just events within the Forthcoming Events category.

    Any advice would be appreciated, thanks.

  2. fireproofsocks
    Member
    Plugin Author

    Posted 2 years ago #

    I'll say right off the bat that I loathe the have_posts() function -- it's WordPress' little myopic and unpredictable view into the database which has very few options and a long list of caveats, so in a word, my best advice here is to avoid using it like the plague.

    What I recommend is using my other plugin, Summarize Posts -- this will be merged into the Custom Content Type Manager plugin in an upcoming version because it's so useful: http://wordpress.org/extend/plugins/summarize-posts/

    What I'd do is set up a dedicated page for the homepage (e.g. use your theme's home.php file), and on that page, you can effectively cross-link to list posts that appear somewhere else, e.g. to summarize your "Events" posts.

    <?php
    
    $Q = new GetPostsQuery();
    $Q->set_output_type(OBJECT);
    $Q->post_type = 'events';
    
    $Events = $Q->get_posts();
    foreach ($Events as $e) {
       print $e->post_title;
       print $e->event_content;
       // ...
    }
    
    ?>

    Alternatively, you can use a simple shortcode to paste into that page's content, and the effect will be comparable to the above.

    See http://code.google.com/p/wordpress-summarize-posts/wiki/get_posts for more documentation on the functions and filters available to you.

  3. ChocolateTeapot
    Member
    Posted 2 years ago #

    Thanks very much for your help, I'll have a look at this.

  4. fireproofsocks
    Member
    Plugin Author

    Posted 2 years ago #

    Hope it helps... the documentation for Summarize Posts is pretty rough (sorry), but it'll all get revamped as I merge it with the CCTM plugin. I've used Summarize Posts on almost every recent WP site, and I've got an updated version to release of that as well.

  5. ChocolateTeapot
    Member
    Posted 2 years ago #

    This did help, thanks. I just have one issue.

    Using the Custom Content Type Manager I have created an Events type.

    Using the following code in home_page.php

    $Q = new GetPostsQuery();
    $args = array();
    $args['post_type'] = 'events';
    $forthcoming_events = $Q->get_posts($args);

    <?php echo $fe->event_image;?> is being returned as a number.

    <?php print_custom_field('event_image'); ?> in events_single.php correctly returns it as a url.

    <?php print_custom_field('event_image'); ?> on the home page gives The event_image field is not defined as a custom field.

    How do I either use print_custom_field in home_page.php or translate the number (corresponding to a media(?)) into a URL?

    Any help is appreciated, thanks.

  6. fireproofsocks
    Member
    Plugin Author

    Posted 2 years ago #

    Glad this helped. I've gotten so many emails about this one... any media or relation field stores a foreign key to the image or post or page. It's the post_id of the foreign thing being linked to. It achieves data normalization. But you're not after the integer, you're after what it represents.

    Have a look at this page in the wiki: http://code.google.com/p/wordpress-custom-content-type-manager/wiki/CreateImageField -- it attempts to explain this in more detail.

    Specifically you want to tie in the integer with one of WP's functions, e.g. wp_get_attachment_image():

    <?php
    foreach( $forthcoming_events as $r) {
       print wp_get_attachment_image($r->event_image, 'full');
    }
    ?>

    Once I integrate Summarize Posts with the CCTM, I can implement the defined output filters on the fields, but now since they are separate, you have to run the filters manually.

  7. ChocolateTeapot
    Member
    Posted 2 years ago #

    I'll have a look at this.

    Thanks, you've been really helpful.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic