Grow your CSS skills. Land your dream job.

[WORDPRESS] Display 2 latest Events on home page

  • # May 1, 2013 at 4:52 pm

    I followed [this](http://tatiyants.com/how-to-use-wordpress-custom-post-types-to-add-events-to-your-site/ “http://tatiyants.com/how-to-use-wordpress-custom-post-types-to-add-events-to-your-site/”) tutorial. Everything looks fine except I wanted to add 2-3 of the latest events on the homepage, just not sure how to display 2 posts from a CPT etc…

    Please Help…

    # May 1, 2013 at 5:21 pm

    This may be wrong, but here’s my best shot:

    First, you add this to grab the posts and replace ‘portfolio’ with ‘your post type’:

    < ?php $loop = new WP_Query(array('post_type' => ‘portfolio’, ‘posts_per_page’ => get_option(’2′), ‘paged’ => get_query_var(‘paged’) ? get_query_var(‘paged’) : 1 )); ?>
    < ?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

    Then you put your content here (for example: the title, excerpt, image, whatever you’re grabbing, etc.)

    Then you close it with this:

    < ?php endwhile; ?>

    # May 1, 2013 at 6:04 pm

    @siouxfan45 Thanks, I will try it out :)

    # May 1, 2013 at 6:16 pm

    Didn’t work :/

    # May 1, 2013 at 7:05 pm

    That link isn’t loading for me, can you post your code for the loop and the register post type from your functions.php file? With those two pieces we should be able to help.

    # May 1, 2013 at 9:50 pm

    I have page.php , and loop-page.php

    here is the loop-page.php content//

    < ?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    < ?php the_content(); ?>
    < ?php endwhile; else: ?>
    < ?php endif; ?>

    Register CPT//
    [http://pastebin.com/MFBEPZVK](http://pastebin.com/MFBEPZVK “http://pastebin.com/MFBEPZVK”)

    Link to tutorial//
    [http://tatiyants.com/how-to-use-wordpress-custom-post-types-to-add-events-to-your-site/](http://tatiyants.com/how-to-use-wordpress-custom-post-types-to-add-events-to-your-site/ “http://tatiyants.com/how-to-use-wordpress-custom-post-types-to-add-events-to-your-site/”)

    # May 2, 2013 at 2:17 am

    Okay, not sure how beneficial this is going to be but I’m going to try! Firstly, events are my worst favourite CPT to create, if you can use a (reputable) plugin I would. Reason: I dislike dates – past dates, events still showing up after even has passed, etc.

    Okay mini vent over! This is something similar to what I have made. Some notes I would change is on the CPT is entering the DATE as YYYY-MM-DD rather than MM/DD/YYYY – it just makes it work better in the meta_query.

    Also I’m no way a WordPress aficionado or knowledged. This is just somethings I came across and combined into one. I’m not even sure if it is usuable, but it’s a start and somebody might be able to clean it up/improve it for both of us!

    < ?php
    // Required
    global $post;

    // Some variables!
    $event_s_date = get_post_meta( $post->ID, ‘event_date’, true ); // Get your start date from the CPT post
    $current_date = date( “Y-m-d”, mktime(0,0,0,date(“m”),date(“d”),date(“Y”)) ); // Get todays date

    // Conversions
    $event_s_date = date(“Y-m-d”, strtotime($event_s_date)); // Turn the CPT start date PHP readable/comparable data

    // LOOP TIME YAY!
    $args = array(
    ‘post_type’ => ‘event’,
    ‘post_status’ => ‘publish’,
    ‘numberposts’ => 2,
    ‘meta_query’=> array(
    array(
    ‘key’ => ‘event_date’,
    ‘compare’ => ‘>’,
    ‘value’ => $current_date,
    ‘type’ => ‘DATE’,
    )
    ),
    ‘meta_key’ => ‘event_date’,
    ‘orderby’ => ‘meta_value’,
    ‘order’ => ‘ASC’
    );

    $event_posts = get_posts($args);

    // If there are events to show
    if($event_posts) {

    foreach($event_posts as $post) {
    setup_postdata($post);
    // Enter the stuff for your post (Title, dates, times, etc.)
    }

    // If there are no events to show
    } else {

    // Enter stuff if there are no events

    }
    ?>

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".