Adding Events in homepage instead of Posts

WordPress Event Management, Calendars & Registration Forums General Question Adding Events in homepage instead of Posts

This topic contains 9 replies, has 3 voices, and was last updated by  LDeKay 11 years, 2 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2891

    First of All I love your plugin. I only have one question. I would like to insert the events instead of the posts in the homepage any help of how I can do this. Thanks

    Danica
    #2897

    Thanks Danica,

    You can add the following code to your functions.php (or a utility plug-in):

    add_action('pre_get_posts','danica_alter_main_page',5,1);
    function danica_alter_main_page($query){
          if( $query->is_main_query() && is_front_page() ){
              $query->set('post_type','event');
          }
    }
    
    Stephen Harris
    #2898

    Tried to do that but still nothing. I think I need something to overwrite the post and instead posts the events

    Danica
    #2899

    This should do just that – its over-riding the query for the front page, and instead of asking for posts its changes to to ‘event’.

    Maybe, see why its failing – e.g. is the conditional if( $query->is_main_query() && is_front_page() ) ever true? – trying adding wp_die('') to kill processing once it reaches that part. If it does, then it should show a blank page.

    I think is_front_page() is the correct one to use. But I get confused with is_home()…. http://codex.wordpress.org/Function_Reference/is_home

    The $query->is_main_query() is essential though – otherwise odd things will happen.

    Of course if the theme is ‘doing it wrong’ then this may not work, but that’s highly unlikely.

    Stephen Harris
    #2908

    Hi, Still cannot figure it out I know I am being a Pain but if you could help me by giving me step by step instructions how to do it I would really appreciate. I am new to all this coding stuff. I only know some basic stuff. thank you in advance
    danica

    Danica
    #2912

    Hi Danicia,

    The code above worked for me. For debugging purposes try:

    add_action('pre_get_posts','danica_alter_main_page',5,1);
    function danica_alter_main_page($query){
          if( $query->is_main_query() && is_front_page() ){
              $query->set('post_type','event');
             echo '<pre>';
             var_dump($query);
             echo '<pre>';
          }
    }
    

    It should print something at the top. It will probably mess up the page layout to, but that’s fine. If you do see something, then something is changing the main query after this.

    If you don’t, then for some reason the code is not being reached ( i.e. is_front_page() is false or the function is never called). If it doesn’t, try removing && is_front_page() – then it really should do something. If not, then the code isn’t being included for some reason. If it does – then is_front_page() is the wrong conditional to use…

    Stephen Harris
    #2926

    Yes, Now It changed the whole thing but It messed up everthing.

    Danica
    #3297

    I’ve been looking for a long time for an Events plug-in that treats Events as posts, adds Event posts to the frontpage, and works consistently. Looks like you’ve done it.

    Could this frontpage insert code be modified to show both regular posts and Event posts? And would it be possible to control which ones are posted by shortcodes referencing categories?

    Specifically, I’m using the Weaver II theme with a [weaver_show_posts cats=”cat_slugs”] shortcode on a static page as the homepage to show only selected posts. Could your code above be modified to post selected Post and Event categories on a static page? I know it’s probably asking a lot, but it would certainly be useful.

    Thanks

    LDeKay
    #3305

    Hi LDeKay,

    Having posts and events together on the same page (by which I mean in the same ‘list’) is problematic – namely because WordPress will treat them all as posts as order by publish date – nor will your theme know to treat events separately (but this can be dealt with).

    If the events are in a separate list from the post – then everything should work out fine. You can use a WP_Query or eo_get_events() to retrieve the events and then list them.

    In the same way, having the weaver_show_posts shortcode and eo_events shortcode on the same page shouldn’t present any problems.

    If you wanted one list to include both events and post – then again this problematic – (and not possible with eo_events and probably not possible weaver_show_posts.

    Stephen Harris
    #3308

    Yeah, could use a two column template so posts and events are separate but side-by-side. Since finding that some Events plug-ins gave the option of creating events with all the details of a post I’ve been looking for a way to post events on the blog page. Finally recognized those evants were a special type of post so I understand why there are problems in posting together, but that would still be the grail.

    I’ll work on the side-by-side solution. Thanks

    LDeKay
Viewing 10 posts - 1 through 10 (of 10 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.