Jquery and calendar next and previous month buttons - php

I am using wordpress, a plugin called The Events Calendar (great plugin btw), and I'm trying to create a little widget with a small navigational element in it. The navigation would show the previous month, the current month, and the next month. For example, today the navigation would show "Apr May Jun".
What should happen is when you click "Jun" it should load the events that occur in June. I want to load these via jquery/ajax magic into a specified div.
I kinda have it working, but i have a bug and I'm not sure how to fix it. When i click one of the months that is displayed initial, it works. But then when i click another month, I'm losing my jquery function (probably due to the page not reloading completely.)
So, I'm wondering if there is a way to re-initialize the navigation so i can get more clicks.
On with the code:
My sidebar.php file contains the following code:
Some HTML
<div id="upcoming-events-box">
<div class="upcoming-events-block-inner">
<div class="upcoming-events-content">
<div id="EventLoader">
<?php get_template_part( 'content', 'the-events' ); ?>
</div>
</div>
</div>
</div>
And some Jquery:
<script type="text/javascript">
jQuery(function() {
jQuery('#upcoming-events-months li a').click(function() {
var toLoad = jQuery(this).attr('href')+' #EventLoader';
jQuery('#EventLoader').hide('fast',loadContent);
jQuery('#load').remove();
jQuery('.upcoming-events-content').append('<span id="load">LOADING...</span>');
jQuery('#load').fadeIn('normal');
function loadContent() {
jQuery('#EventLoader').load(toLoad,'',showNewContent);
}
function showNewContent() {
jQuery('#EventLoader').show('normal',hideLoader);
}
function hideLoader() {
jQuery('#load').fadeOut('normal');
}
return false;
});
});
</script>
content-the-events.php which is called into the sidebar contains the following code:
<div id="upcoming-events-months">
// Some php
global $post;
$current_page_URL = $_SERVER["SERVER_NAME"];
if (isset($_GET['setmonth'])) {
$current_month = $_GET['setmonth'];
} else {
$current_month = date('M'); // This gets the current month
}
$next_month = date('M', strtotime('+1 month', strtotime($current_month)));
$prev_month = date('M', strtotime('-1 month', strtotime($current_month)));
echo "<ul>";
echo "<li><a href='http://".$current_page_URL."?setmonth=".$prev_month."'
id='".$prev_month."'>".$prev_month."</a></li>";
echo "<li>".$current_month."</li>";
echo "<li><a href='http://".$current_page_URL."?setmonth=".$next_month."'
id='".$next_month."'>".$next_month."</a></li>";
echo "</ul>";
</div>
<div id="content"></div>
<div class="upcoming-event-scroller">
<?php
$all_events = tribe_get_events(array(
'eventDisplay'=>'all',
'posts_per_page'=>-1,
'start_date'=>'01 '.$current_month.' 2012',
'end_date'=>'31 '.$current_month.' 2012'
));
foreach($all_events as $post) {
setup_postdata($post);
?>
<div class="row <?php echo (++$j % 2 == 0) ? 'even' : 'odd'; ?>">
<p><?php the_title(); ?></p>
<p class="event-date"><?php echo tribe_get_start_date($post->ID, true, 'M j, Y'); ?> - <?php echo tribe_get_end_date($post->ID, true, 'M j, Y'); ?></p>
</div>
<?php } //endforeach ?>
<?php wp_reset_query(); ?>
</div>
Maybe I've just gotten myself really confused trying to figure this out. haha. Any help would be greatly appreciated.

Related

Wordpress link and content

I'm creating a menu for wordpress, but when I click on the permalink the content stays the same. And my second dropdown does not work eather. The problem is that on my section where the content needs to be it not shows. The content just takes the last message and not the post i select on the menu.
https://i.gyazo.com/1698056c27baa40768659d2edab5e3d9.png
This is how it stays even if i click an other post.
<?php
get_header();
?>
<div class="menu">
<div>
<h1>Documentatie</h1>
</div>
<?php $cats = get_categories();
foreach ($cats as $cat) {
$cat_id= $cat->term_id;
echo "<div class='dropdown'>";
echo "<button onclick='myFunction()' class='dropbtn'>".$cat->name."</button>";
echo "<div id='myDropdown' class='dropdown-content'>";
query_posts("cat=$cat_id&post_per_page=100");
if (have_posts()) : while (have_posts()) : the_post(); ?>
<button class="dropbutton"><h2><?php the_title(); ?></h2></button>
<?php endwhile;
?>
</div>
</div>
<?php
else :
echo "<p>Geen content gevonden</p>";
endif;
wp_reset_postdata();
}
?>
</div>
<div class="content">
<h2><?php the_title(); ?></h2>
<div class="wpcontent"><?php the_content(); ?> </div>
</div>
<?php wp_footer(); ?>
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>
Have a look at my other 2 comments and proposed solution below, this is not a WP issue you're facing, it's JS/jQuery
echo "<div class='dropdown'>";
echo "<button onclick='myFunction(" . $cat->slug . ")' class='dropbtn btn-" . $cat->slug . "'>".$cat->name."</button>";
echo "<div id='myDropdown' class='dropdown-content'>";
And:
function myFunction(cat_slug) {
$('.btn-' + cat_slug).show();
}
Try that, should work :)

How to include query string in url and staying on the same page?

I generated a tabs of months with anchor tag and href is equal to query string of ?m=1&y=2014. The year part will be incremented accordingly with next and previous button. By the way this is in a wordpress environment.
--Code--
Next and previous button for year.
<div class="table-header">
<div class="table-header-wrapper">
<div class="prev"></div>
<div class="year"><?php echo $current_year; ?></div>
<div class="next"></div>
</div>
</div>
--Code--
Tab months
<?php
for ( $monthNum = 1; $monthNum <= 12; $monthNum++ ) :
$month = date( 'M', mktime( 0,0,0, $monthNum, 1, $current_year ) ); // month in 3 letter format e.g. Jan
$current_monthNum = date( 'n' ); // current month in number
?>
<div class="<?php echo strtolower( $month ); ?> month" data-month="<?php echo $monthNum; ?>">
<?php echo $month; ?>
</div>
<?php
endfor;
?>
With PHP its not possible as PHP is a server side scripting language.
You can achieve it with jQuery.
Hashchange plugin serves your need.
Here, you can change hash of url on some event.
And fire some event on haschange.
You can change your hashes on Previous and Next buttons.

Wordpress: Getting Month by Month posts, issue with layout

I've got an issue where I'm getting the wordpress posts month by month, but I'm missing something obvious where by it screws with my layout a bit. I'm using the 996 grid system for my layout.
The NEED:
I'm getting all posts in March 2013 and wanting to lay them out each inside a so 3 posts are stacked like screenshot A.
My code is below:
<div class="container clearfix" style="background:yellow;">
<div class="grid_12" style="background:blue;">
<?php
$blogtime = date('Y');
$prev_limit_year = $blogtime - 1;
$prev_month = '';
$prev_year = '';
$args = array(
'posts_per_page' => 20,
'ignore_sticky_posts' => 1
);
$postsbymonth = new WP_Query($args);
while($postsbymonth->have_posts()) {
$postsbymonth->the_post();
if(get_the_time('F') != $prev_month || get_the_time('Y') != $prev_year && get_the_time('Y') == $prev_limit_year) {
echo "<h2>".get_the_time('F, Y')."</h2>\n\n";
}
?>
</div>
<div class="grid_4" style="background:red;">
<article id="post-<?php the_ID(); ?>" role="article" itemscope itemtype="http://schema.org/BlogPosting">
<?php the_post_thumbnail('standard-thumb'); ?>
<h3><?php the_title(); ?></h3>
</article>
</div>
<?php
$prev_month = get_the_time('F');
$prev_year = get_the_time('Y');
}
?>
</div> <!-- END OF CONTAINER -->
THE PROBLEM:
With the above code, every single instance of grid_4 sits outside the container EXCEPT the first one. See Screenshot B
How can I make sure all grid_4 divs are included inside the container?
I've tried moving the div which doesent work, I think I'm missing something fundamental from the query.
So it seems if I remove the closing div to grid_4 - it now works...
In the dom it now all sits inside the container... however looking at the template markup code I'm one div short...
I don't know why this is happening but it is... one day I'll figure out why but right now wordpress is automagically adding an extra div.

Conditional Tag With Custom Fields Not working correctly

What I am after is to get the Date and title to appear on each Calendar if that Custom Field is filled in; but at the moment, it is putting them in order and displaying a calendar for every child page, but I only want a Calendar and date appear if they have an upcoming date.
Any help will be madly appreciated! - See link at bottom for working example.
This is what I have at the moment:
<!-- PHP OF OPEN DAYS -->
<!-- Calender Items -->
<div class="calender-item">
<?php $values = get_post_custom_values("next_open_day");
if ( is_array($values) )
the_field( 'university_name');
else
echo '';
?>
<?php $values = get_post_custom_values("next_open_day");
if ( is_array($values) )
the_field('next_open_day');
else
echo '';
?>
<!-- END OF CALENDER ITEM -->
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
Any idea's then shoot away please...
The way my site is spitting it out at the moment is like this:
http://universitycompare.net/open-days
Move the if above the div, that way it will not print at all when false
<?php $values = get_post_custom_values("next_open_day");
if ( is_array($values) ) {
echo '<div class="calender-item">';
the_field( 'university_name');
the_field('next_open_day');
echo '</div>';
}
else
echo '';
?>

How include the wordpress schedule feature in my own plugin?

My plugin includes a custom content textarea and I want add the same schedule feature that wordpress posts have.
Is there some easy procedure to do it?
HTML:
<div class="misc-pub-section curtime misc-pub-section-last">
<?php
$datef = __('M j, Y # G:i');
$stamp = __('Publish <b>immediately</b>');
$date = date_i18n($datef, strtotime(current_time('mysql')));
?>
<span id="timestamp"><?php printf($stamp, $date); ?></span>
<a href="#edit_timestamp" class="edit-timestamp hide-if-no-js" tabindex='4'><?php _e('Edit') ?></a>
<div id="timestampdiv" class="hide-if-js"><?php touch_time(0, 0, 5); ?></div>
</div>
PHP:
add_action('admin_print_scripts', 'header_scripts');
function header_scripts()
{
wp_enqueue_script('nomikos_my_plugin_js_comment', site_url('wp-admin/js/comment.js'));
}

Categories