Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End [WORDPRESS] Display 2 latest Events on home page

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #44490
    asiek
    Participant

    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…

    #133773
    asiek
    Participant

    @siouxfan45 Thanks, I will try it out :)

    #133776
    asiek
    Participant

    Didn’t work :/

    #133787

    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.

    #133802
    asiek
    Participant

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

    here is the loop-page.php content//




    Register CPT//
    [http://pastebin.com/MFBEPZVK](http://pastebin.com/MFBEPZVKhttp://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/”)

    #133815
    nigel123
    Member

    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!

    // 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 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.