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.
Related
I have this PHP code that works excellent in displaying arrows to previous/next pages of WordPress child pages:
<?php // Button Code for Prev and Next Page
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(
array('post_type' => 'page'));
$portfolio = get_page_by_title('Services');
$args = array (
'child_of' => $portfolio->ID,
'sort_order' => 'asc',
'post_type' => 'page'
);
$portfolio_children = get_pages( $args );
$pages = array();
foreach ($portfolio_children as $page) {
$pages[] += $page->ID;
}
$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>
<div class="navigation">
<?php if (!empty($prevID)) { ?>
<div class="previousarrow">
<a href="<?php echo get_permalink($prevID); ?>" title="<?php echo get_the_title($prevID); ?>">
<img src="<?php bloginfo('url');?>/wp-content/uploads/2016/06/previousbutton.png">
</a>
</div>
<?php }
if (!empty($nextID)) { ?>
<div class="nextarrow">
<a href="<?php echo get_permalink($nextID); ?>" title="<?php echo get_the_title($nextID); ?>">
<img src="<?php bloginfo('url');?>/wp-content/uploads/2016/06/nextbutton.png">
</a>
</div>
<?php } ?>
</div>
Problem is that on the first and last pages, I can't get it to show the previous/next button to go to the 'last or first' page of the child pages.
Example
A (parent)
1 (child page 1)
2 (child page 2)
3 (child page 3)
I want the previous option on page 1 to go to page 3 and vice versa but I can't figure that out. Any help is appreciated.
The buttons are not appearing because of the php if parameters, which eliminates the previous arrow on page 1 and next arrow on page 3 because it recognizes them as empty. One way around this would be to remove the if clauses and use if statements to define your prevID and nextID variables, or you can simply add else statements after each if statement defines the previous/next buttons to be shown. However, both of these solutions are temporary, as you would require the number of pages in order to implement them, which is subject to change. Instead, use this code to ensure this feature will stay intact even if you add additional pages:
<?php if (empty($prevID)) { $firstpage = $pages[$current]; }
if (empty($nextID)) {$lastpage = $pages[$current]; }
This will define the value of the first and last pages, and can simply be inserted after you define prevID and nextID. Now simply add php else statements like so:
</div> <?php }
else { ?>
<div class="previousarrow>
<a href="<?php echo get_permalink($lastpage); ?>"
title="<?php echo get_the_title($lastpage); ?>">
<img src="<?php bloginfo('url');?>
/wp-content/uploads/2016/06/previousbutton.png"></a></div>
<?php }
This block of code would be inserted after your first div class, as the previous arrow should navigate to the last page when no previous page exists. Do the exact same after the second div class, replacing the lastpage variable with the firstpage one we created. Hope this helps!
I have a working AJAX function within WordPress, There I'm looping through HTML to get my posts which then are accessible in my AJAX Success CB where they are appended to their div.
My question:
How would I format my PHP query func values (a while loop, an array and several separate pagination values) for proper use within JavaScript?
Here's what my function looks like as is;
<?php
function _custom_paginate(){
$paged = $_POST['count'];
$args = array(
'posts_per_page' => 12,
'category' => 3,
'paged' => $paged,
'post_status' => 'publish'
);
$query = new WP_Query($args);
if ( $query->have_posts() ) :
while ($query->have_posts()) : $query->the_post(); ?>
<div class="large-3 three columns box">
<div class="thumb">
<?php the_content();?>
<div class="thumb-foot">
<div class="thumbinfo">
<p>Posted on <?php the_time('m.d.Y') ?></p>
</div>
<div class="thumb-social">
<div class="thumb-twitter"></div>
<div class="thumb-facebook"></div>
</div>
</div>
</div>
</div>
<?php
endwhile;
endif;
//Several pagination values
$total_pages = $query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
?>
<div class="pagi-contain">
<div id="pagi" class="large-12">
<!-- Append pagination here #pagi-->
<ul class="pagi">
<li id="ajaxprev" style="float:left;">prev</li>
<li id="pagenum"><?php echo $current_page; echo ' ... '.$total_pages; ?></li>
<li id="total_pages"></li>
<li id="ajaxnext" style="float:right;">next</li>
</ul>
</div>
</div>
<?php
}
exit;
}
You don't have to rewrite this but a lesson in formatting each value into an accessible array(?) for JS to work with each value separately. I found this person seems to have done what I am trying but the subject here is not PHP side...
Here is an example that is hopefully more clear on what I'm attempting to make work. http://jsfiddle.net/BenRacicot/LRmc3/
I'm not 100% clear what data you want to format, but just do this:
json_encode($variable_you_want_as_json);
To format the entire page's output, use this:
ob_start(); // Start output buffering (echos after this go into the buffer, not to the client)
// all the code and output you want formatted
// example: echo "Some HTML I want to store in a JSON-encoded variable";
$html_data = ob_get_contents(); // Gets the contents of the buffer
ob_end_clean(); // empties the buffer
echo json_encode($html_data); // encodes and echoes whatever you did between ob_start() and ob_get_contents()
It sounds like you want multiple values, so you can just replace the variable in the last line with an array, like this:
$my_array = array('html_data'=>$html_data,
'max_num_pages' => $query->max_num_pages,
'foo'=>$foo,
'bar'=>$bar,
'some_other_var' => $some_other_var);
echo json_encode($my_array);
Project: WordPress project
Query: WP_Query()
With the single query I'm dealing with two loops - I call it loop within a loop. Structure is like below:
<?php
while( $my_query -> have_posts() ) :
$my_query -> the_post();
if( condition) { ?>
<div class="grid-box">
<div class="item">Item of this kind</div>
</div> <!-- .grid-box -->
<?php
}
if( condition) {
$someCounter = grab some counter here;
for( $inner = 0; $inner < $someCounter; $inner ++ ) {
?>
<div class="grid-box">
<div class="item">Item of that** kind#<?php echo $inner; ?></div>
</div> <!-- .grid-box -->
<?php
} //end for
}
endwhile;
?>
With CSS the query is doing excellent job for me, showing the items in nice grid-blocks. But with more items than a row, the items in second row colliding with the first. So I planned to put them within row class like:
<div class="row">
<!-- every 6 items within a grid -->
<div class="grid grid-first>Item of this kind</div>
<div class="grid>Item of this kind</div>
<div class="grid>Item of that** kind</div>
<div class="grid>Item of that** kind</div>
<div class="grid>Item of this kind</div>
<div class="grid grid-last>Item of that** kind</div>
</div>
<div class="row">
<div class="grid grid-first>Item of that** kind</div>
<div class="grid>Item of that** kind</div>
<div class="grid>Item of this kind</div>
</div>
Now I need to count the total items. How can I do this? Do I need to pass two counter and if so then how can I combine them both to count the exact counts and then use the count as conditions to load the div with .row? Please note as what I'm dealing with, the $inner counter is important for my dynamic code. But we can use the count for our total count.
For count you can use like this
<?php wp_count_posts( $type, $perm ); ?>
To get the published status type, you would call the wp_count_posts() function and then access the 'publish' property.
<?php
$count_posts = wp_count_posts();
$published_posts = $count_posts->publish;
?>
Counting pages status types are done in the same way as posts and make use of the first parameter. Finding the number of posts for the post status is done the same way as for posts.
<?php
$count_pages = wp_count_posts('page');
?>
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.
I have a parent page that acts as menu for my portfolio.
It pulls in thumbnail images from the child pages which i have been able to accomplish with magic fields and some code. It dumps the images into a grid layout. The thumbnails are pulled into one container div like so:
div id="folio-content">
<div class="thumb-container">
<div class="thumb"><img src="/images/pic.jpg"/>
</div>JCPenny</div>
... </div>`
when the div gets filled up with 2 thumbnails I want to create a new container div and fill it with 2 images again and so on after 2 images.
So, if you had 4 images it would look like this.
<div id="folio-content"><!--/Main Container/-->
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>JCPenny</div>
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>Champ Car</div></div>
<div id="folio-content"><!--/Main Container/-->
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>JCPenny</div>
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>Champ Car</div></div>
this is the code I am using in my page.php file.
<?php get_header(); ?>
<div id="folio-content">
<?php
$projectpage = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
$count = 0;
foreach($projectpage as $page)
{
$content = $page->post_content;
if(!$content)
continue;
if ($count == 10) --- this is geting 10 images now, but I want to get them all.
break;
$count++;
$content = apply_filters('the_content', $content);
?>
<div class="thumb-container">
<div class="thumb"><a href="<?php echo get_permalink($page->ID); ?>"<?php echo get_image ("thumbnail",1,1,1,$page->ID);?></a>
</div><?php echo $page->post_title ?>
</div>
<?php
}
?>
</div><!--/close set!-->
</div>
also, how can I get ALL child pages? I have it set to 10 now with this if ($count == 10)
any help? thanks a ton again!!!!
I'm not familiar with "get_pages" but since Wordpress treats posts and pages in an identical manner you could use this.
$projectpage = get_posts('numberposts=-1&post_type=page&child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
The -1 removes the limit and gets ALL the specified pages.
I have cobbled together some code, that sort of sounds right but does not work at all! Which I am not surprised. But it is a starting point - please take a look at this code, maybe it is step in the right direction?
<?php
$projectpage = get_posts('numberposts=-1&post_type=page&child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
if (have_posts()) :
$i=0; // counter
while(get_posts()) : the_post();
if($i%2==0) { // if counter is multiple of 3, put an opening div ?>
<!-- <?php echo ($i+1).'-'; echo ($i+2); ?> -->
<div>
<?php } ?>
<div class="single_item">
<h2><?php the_title(); ?></h2>
</div>
<?php $i++;
if($i%2==0) { // if counter is multiple of 3, put an closing div ?>
</div>
<?php } ?>
<?php endwhile; ?>
<?php
if($i%2!=0) { // put closing div here if loop is not exactly a multiple of 3 ?>
</div>
<?php } ?>
<?php endif; ?>