I'm using the "bones" theme on WordPress. On my blog page I'm trying to have it with 9 blog posts displayed in 3 columns, but on the first page have 10 blog posts with the first (most recent) one being enlarged to span all 3 columns.
What is the best way to display 10 posts on the first page and 9 thereafter without messing up the pagination?
Here's my code: (I removed all the HTML and what-not because I assume it's not needed)
<?php if (have_posts()) : ?>
<?php $post = $posts[0]; $c=0;?>
<?php while (have_posts()) : the_post(); ?>
<?php $c++;
if( !$paged && $c == 1){
//code for the first post
} else { // THE REST: begin the code for the remainder of the posts ?>
<?php }
endif; ?>
<?php endwhile; ?>
I haven't really experimented with it as I don't have the page function set up - but try this
<?php
$post = $posts[0]; $c=0;
$c++; if( !$paged && $c == 1){
$query1 = new WP_Query( array ('posts_per_page' => 1 ) );
if ($query1-> have_posts()) : while ($query1-> have_posts()) : $query1-> the_post(); ?>
<?php the_title(); ?>
<?php endwhile; endif; wp_reset_query();}
else{
$query2 = new WP_Query( array ('posts_per_page' => 9, 'offset' => 1 ) );
if ($query2-> have_posts()) : while ($query2-> have_posts()) : $query2-> the_post(); ?>
<?php the_title(); ?>
<?php endwhile; endif; wp_reset_query(); }?>
Related
I'm working on a website for a band where you can add gigs and add the songs played on that specific gig.
So I've created two custom post types:
- gig
- song
I got a custom field "Songs" of type "Relationship". This field is shown on the Custom Post Type. This way I can add songs to a specific gig. This works perfectly.
But I want to show some statistics on the homepage of that website: I want to count how many times a specific song is played and show the top 10. So I guess that I have to loop over the gig custom post type and count the relation with 'songs'.
I thought this would do the trick:
<?php
$args = array(
'post_type' => 'gig'
);
?>
<?php $loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
print_r(get_field('songs'))
//$song_count = count(get_field('songs'));
//echo $song_count . " ";
the_title();
?><br />
<?php endwhile; ?>
<?php else: ?>
<!-- No gigs available -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>
You can find the result of the print_r over here: http://snippi.com/s/njzg3uu
For example: the song "A memory" is on 2 gigs. That why you can find it twice in the array. The song "Wasted" can be found only once, because it's on 1 gig.
You can use this code to create an array of all the songs:
<?php
$args = array(
'post_type' => 'gig'
);
$countArray = []; //create an array where you can put all the song id's and the number of times played
?>
<?php $loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$posts = get_field('songs');
if( $posts ):
foreach( $posts as $post):
setup_postdata($post);
//if the song id already exists -> count + 1
if (array_key_exists($post->ID, $countArray)){
$countArray[$post->ID]++;
}
else { // otherwise the song is played 1 time
$countArray[$post->ID] = 1;
}
endforeach;
wp_reset_postdata();
endif;
?>
<?php endwhile; ?>
The code above will create an array of post ID's of songs and the number of times it is used in the post_type "gig".
Now you can use the array $countArray and do whatever you want with it.
In your example you want to sort it, so you have to do arsort($countArray); This way the array is sorted by it's value (the number of times played) from high to low.
Then you have to loop through the array:
foreach ($countArray as $key => $value) {
?>
<?php echo get_post_permalink($key); //=the permalink of the song ?>
<?php echo get_the_title($key); //= the title of the song ?>
<?php echo $value; //number of times play in a gig ?>
<?php
}
So the full code is:
<?php
$args = array(
'post_type' => 'gig'
);
$countArray = [];
?>
<?php $loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$posts = get_field('songs');
if( $posts ):
foreach( $posts as $post):
setup_postdata($post);
if (array_key_exists($post->ID, $countArray)){
$countArray[$post->ID]++;
}
else {
$countArray[$post->ID] = 1;
}
endforeach;
wp_reset_postdata();
endif;
?>
<?php endwhile; ?>
<?php
arsort($countArray);
foreach ($countArray as $key => $value) {
?>
<?php echo get_post_permalink($key); //=the permalink of the song ?>
<?php echo get_the_title($key); //= the title of the song ?>
<?php echo $value; //number of times play in a gig ?>
<?php
}
?>
<?php else: ?>
<!-- No gigs available -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>
You Could do like this in simple and short way:
$args = array(
'post_type' => 'gig'
);
$gigs = get_posts($args);
$songsarr = array();
foreach($gigs as $gig) {
$posts = get_field('songs', $gig->ID);
array_push($songsarr,$posts[0]);
}
//echo "<pre>;
//print_r($songsarr);
$countsongs = array_count_values($songsarr);
echo 'No. of Duplicate Items: '.count($countsongs).'<br><br>';
// print_r($countsongs);
foreach($countsongs as $songID => $songname){
echo get_the_title( $songID );
echo $songname;
}
I have Tried by making Two Custom Post Type(gig, songs) and I got count numbers of song this way that you can show in home page and also you can give condition in last foreach loop if song more than one etc..
Hope this will help you!
Hope help:
<?php
$args = array(
'post_type' => 'song'
);
?>
<?php $loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$song_count = count(get_field('songs', get_the_ID())); <-- add
echo $song_count . " ";
the_title();
?><br />
<?php endwhile; ?>
<?php else: ?>
<!-- No gigs available -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>
My understanding of your question is you are seeking to generate a top 10 list of songs that have the most associated gigs. The best way to approach this would be to generate a set that would map a unique identifier and a count value of how many times that songs has been seen.
Here is an example:
<?php
// Get all the posts
$gigs = get_posts([
'post_type' => 'gigs',
'numberposts' => -1
]);
// We will use this array to key a running tally of
$set = [];
// If the key doesn't exist yet on the array, then we will initialize it, otherwise, increment the count
function add_set_element(&$set, $key) {
if (!isset($set[$key])) {
$set[$key] = 1;
} else {
$set[$key]++;
}
}
function iterate_songs($songs, &$set){
/** #var WP_Post $song */
foreach($songs as $song) {
$key = $song->post_title;// This can be what ever unique identifier you want to get from $song object, such as ID or title
add_set_element($set, $key);
}
}
foreach($gigs as $gig) {
setup_postdata($gig);
$songs = get_the_field('songs');
iterate_songs($songs, $set);
}
Afterwards you can sort and manipulate the $set variable however you feel to get the data you want from it.
Let me know if I misinterpreted your questions, and I can provide another answer.
I could really use some help with this one... I managed to add a lead section to my wp homepage (home.php):
<?php
if (is_home() && $paged < 2 ) {
get_template_part('template-parts/content', 'news');
} else {
if (have_posts()) :
while (have_posts()) : the_post();
get_template_part('template-parts/content');
endwhile;
else :
get_template_part('template-parts/content', 'none');
endif;
} ?>
Now there is 1 lead article at the top of the page (< 2) and the rest after that. What I would like to do now is adding a section in between those. Kinda like (> 2 but < 5). The content-news template looks like this:
<?php /* Display Posts */
if (have_posts()) :
$counter = 1;
$max_posts = $wp_query->post_count;
while (have_posts()) : the_post();
if ($counter == 1) :
get_template_part('template-parts/content', 'lead'); ?>
<?php
else :
get_template_part('template-parts/content');
endif;
$counter++;
endwhile;
else :
get_template_part('template-parts/content', 'none');
endif;
?>
There are basically two sections now, I want it to be 3 if you know what I mean? I don't know how to explain it better...
I am building featured posts section on my site using Wordpress tags (3 latest posts tagged as "featured" are being shown on the homepage) using this code:
<?php
$args=array(
'tag' => 'featured',
'showposts'=>3,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="featured-article">
<!--thumbnail, title and link code here -->
</div>
<?php
endwhile;
} //if ($my_query)
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Every entry is wrapped with "featured-article", but since i want the first post to be full-width and other 2 half width, i was wondering how can i add appropriate classes to them?
So, the first post gets "full-width" class and other two "half-width"...
I apologize if i didnt explained it properly (english not first language and all).
Any help would be greatly appreciated
You can get the index of your post with $my_query->current_post while you're in the loop.
$class = $my_query->current_post == 0 ? 'full-width' : 'half-width';
You might also want to make sure that the class only gets applied to the first item of the first page
$class = $my_query->current_post == 0 && !is_paged() ? 'full-width' : 'half-width';
Here's your loop
<?php
$args=array(
'tag' => 'featured',
'showposts'=>3,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$class = $my_query->current_post == 0 && !is_paged() ? 'full-width' : 'half-width'; ?>
<div class="featured-article <?php echo $class; ?>">
</div>
<?php
endwhile;
} //if ($my_query)
wp_reset_query(); // Restore global post data stomped by the_post().
?>
You can solve this with counter.
<?php
$args=array(
'tag' => 'featured',
'showposts'=>3,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
//add classes
$counter = 0;
$classes = '';
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$classes = $counter == 0 ? ' full-width' : ' half';
?>
<div class="featured-article<?php echo $classes; ?>">
<!--thumbnail, title and link code here -->
</div>
<?php
$counter++;
endwhile;
} //if ($my_query)
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Or You can make a custom filed for addition class.
I am trying to place google ads between every 7 posts in my homepage and category pages of wordpress site.
i have found this code on WordPress forum and tried myself by putting it it didn't worked. i am not sure how to use this code in my theme.
the code i found is this.
if ( have_posts() ) : $count = 0; while ( have_posts() ) : the_post();
//before
if (($count>1) && ($count%5 == 0) ){ ?>
<div>
[adcode]
</div> <?
} $count++;
here is the relative code from index.php of my theme.
if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php
if ( floatval(get_bloginfo('version')) < "3.6" ) {
//old post formats before they got built into the core
get_template_part( 'includes/post-templates-pre-3-6/entry', get_post_format() );
} else {
//WP 3.6+ post formats
get_template_part( 'includes/post-templates/entry', get_post_format() );
} ?>
<?php endwhile; endif; ?>
i want to know how do i place the code according to my the upper code of index.php. thanks
What you can do is to create a $counter then to display based on it the adcode. So the final code for you will be this one:
$counter = 1;
if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php
if($counter % 7 == 0) {
?>
[adcode]
<?php
}
if ( floatval(get_bloginfo('version')) < "3.6" ) {
//old post formats before they got built into the core
get_template_part( 'includes/post-templates-pre-3-6/entry', get_post_format() );
} else {
//WP 3.6+ post formats
get_template_part( 'includes/post-templates/entry', get_post_format() );
} ?>
<?php $counter++; endwhile; endif; ?>
We just increment the $counter, and display the [adcode] after each seven posts.
I'm assuming [adcode] is a shortcode due to the square brackets. This code is untested.
if(have_posts()) : $count = 0; while(have_posts()) : the_post(); ?>
<?php
if ( $count % 7 == 0 ) {
do_shortcode('[adcode]');
}
if ( floatval(get_bloginfo('version')) < "3.6" ) {
//old post formats before they got built into the core
get_template_part( 'includes/post-templates-pre-3-6/entry', get_post_format() );
} else {
//WP 3.6+ post formats
get_template_part( 'includes/post-templates/entry', get_post_format() );
} ?>
<?php $count++; endwhile; endif; ?>
In Wordpress I need to get the posts form myCategory as blog post (with the meta data example date, posted by etc)- this is working fine but currently bringing back all the post from every category
switch ($page_layout) {
case "layout-sidebar-single-left":
echo '<div class="row fixed">';
echo '<div class="col-220 no-print">';
ewf_setSection('zone-sidebar');
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar-page') );
echo '</div>';
echo '<div class="col-700 last">';
$postTitle = '<h3>'.get_the_title().'</h3>' ;
$readMore = '<p class="text-right">'.__('Read More', EWF_SETUP_THEME_DOMAIN).'</p>';
query_posts('category_name=media&showposts=2');
if (have_posts()) while (have_posts()) : the_post();
echo $postTitle;
echo the_content();
echo $readMore;
endwhile;
echo '</div>';
echo '</div>';
break;
How about this:
query_posts(array('category_name'=>'Category Name','posts_per_page'=>10));
// the Loop
while (have_posts()) : the_post();
the_content( 'Read the full post ยป' );
endwhile;
<?php $args = array(
'category_name'=>'MyCategory',
'posts_per_page'=>10);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
// display stuff here
endwhile;
This is the query that will get you 10 posts from a category named MyCategory - just copy/paste your current code that renders the HTML for the date/author/whatever you already have in the commented part.
It's the part inside your current loop