• 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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor fireproofsocks

    (@fireproofsocks)

    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: https://www.ads-software.com/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 https://code.google.com/p/wordpress-summarize-posts/wiki/get_posts for more documentation on the functions and filters available to you.

    Thread Starter chocolateteapot

    (@chocolateteapot)

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

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    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.

    Thread Starter chocolateteapot

    (@chocolateteapot)

    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.

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    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: https://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.

    Thread Starter chocolateteapot

    (@chocolateteapot)

    I’ll have a look at this.

    Thanks, you’ve been really helpful.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Include fields from custom type on the home page’ is closed to new replies.