I have Website with WordPress Bootstrap:
http://www.obra-93.hr/zavrseni-projekti/
I have portfolio with items per row.
The code for loop 3 items per row (totaly 20 items) is below:
<div class="container">
<div class="row vrow">
<?php // slideshow
$args_projekti = array(
'showposts' => -1,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => true,
'post_type' => 'projekti',
'post_status' => 'publish',
'cache_results' => false,
'update_post_term_cache' => false,
'update_post_meta_cache' => false
);
$projekti = new wp_query($args_projekti);
$p=0;
if ($projekti->have_posts()):
?>
<?php
while ($projekti->have_posts()):
$projekti->the_post();
$post_id = get_the_ID();
?>
<div class="col-xs-12 col-sm-4 col-md-4">
<?php the_post_thumbnail('projekt-thumb', array('class' => 'img-responsive fade center-block'));?>
<h3 class="text-center"><?php the_title(); ?></h3>
</div>
<?php if($p%3===0){echo '</div><div class="row vrow">';} ?>
<?php $p++; endwhile; endif; wp_reset_query(); ?>
</div>
</div>
My first and last row is not working properly. Why?
Thanks!
if you increment $p after the if condition the values would be like
0,1,2
3,4,5
and so on an because 0 % 3 == 0
so you could start p with 1 then it looks like
1,2,3
4,5,6
or increment before the check
Related
I'm using Bootstrap carousel to display 3 comments of a specific post in WordPress and the latest comment must have the active class on the div. However, the code that I have adds the active class to all 3 comments. Hence, all 3 shows in a single carousel block, where it should be divided or paginated by 3. Here is my current code:
<?php
$args = array(
'status' => 'approve',
'number' => '1',
'post_id' => 9,
'orderby' => 'comment_date',
'order' => 'DESC'
);
$comments = get_comments( $args );
foreach ( $comments as $comment ) :
echo '<div class="carousel-item active">
<div class="carousel-caption">
<span class="fad fa-quote-right" aria-hidden="true"></span>';
comment_text();
echo '<div id="image-caption">— '.$comment->comment_author.'</div>
</div><!-- carousel-caption -->
</div><!-- carousel-item -->';
endforeach;
?>
I would like to have a condition where if comment is latest, add $active = ' active'; else, show nothing to the <div class="carousel-item'.$active.'">.
Is this possible?
Here you go... Use for instead of foreach and check if that is the first item ($i==0)
<?php
$args = array(
'status' => 'approve',
'number' => '1',
'post_id' => 9,
'orderby' => 'comment_date',
'order' => 'DESC'
);
$comments = get_comments( $args );
for($i=0; $i<count($comments); $i++ ) :
$comment = $comments[$i];
echo '<div class="carousel-item ' . ($i==0 ? "active" : "") . '">
<div class="carousel-caption">
<span class="fad fa-quote-right" aria-hidden="true"></span>';
comment_text();
echo '<div id="image-caption">— '.$comment->comment_author.'</div>
</div><!-- carousel-caption -->
</div><!-- carousel-item -->';
endfor;
?>
There are posts on the page that I display using a shortcode. I need to display a button if there are more than 3 posts. The button should be in a How can I add it there?
function posts() {
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$query = new WP_Query($args);
if ($query - > have_posts()):
while ($query - > have_posts()): $query - > the_post(); ?>
...
<? php
endwhile;
endif;
}
add_shortcode('display_posts', 'posts');
<div class="row latest-blog-posts_content d-flex flex-wrap justify-content-between align-items-center">
<?php echo do_shortcode('[display_posts]'); ?>
</div>
<div class="more-posts">
</div>
To count the number of posts you need to add the logic to the shortcode itself and the button needs to be within the outputted shortcode markup like so.
<?php
function get_posts()
{
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$query = new WP_Query($args);
if ($query->have_posts()):
while ($query->have_posts()): $query->the_post();
endwhile;
endif;
if ($query->post_count > 3) {
echo '<button id="loadmore">Load More</button>';
}
echo '<div class="more-posts">';
// More posts go here...
echo '</div>';
}
add_shortcode('display_posts', 'get_posts');
?>
<div class="row latest-blog-posts_content d-flex flex-wrap justify-content-between align-items-center">
<?php echo do_shortcode('[display_posts]'); ?>
</div>
You can then use AJAX to get load more posts when the 'Load more' button is clicked, assuming this is what you're trying to achieve?
I have coded my theme from base and I have a problem displaying pagination in my page.
I have a foreach over my posts that is working properly and it is showing 9 posts in every page, but I could not display paginations below that to switch between page.
It would be great if someone can help me with it.
<div class="row">
<?php
$args = array(
'posts_per_page' => 9,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$myposts = get_posts($args);
foreach ($myposts as $post) : setup_postdata($post); ?>
<div class="col-md-4 col-sm-12 mt-3">
<div class="bg-gray h-100 blog-cards">
<a href="<?php echo get_the_permalink(); ?>" title="more">
<div>
<div class="archive-img">
<?= get_the_post_thumbnail(); ?>
</div>
<h3 class="upper mb-0 px-3 pt-3 pb-0 text-dark"> <?php echo get_the_title(); ?></h3>
<div class="p-3 pt-0">
<?php the_excerpt(); ?>
<?php
global $post;
foreach (get_the_category($post->ID) as $category) {
echo '' . $category->name . '';
} ?> |
<span>
مدت زمان مطالعه
<?= get_field('duration'); ?>
دقیقه
</span>
</div>
</div>
</a>
</div>
</div>
<?php endforeach; ?>
<?= previous_posts_link()?>
</div>
It does not display with any of these built in functions
<?= paginate_links() ?>
<?= the_posts_pagination() ?>
<?= get_the_posts_pagination() ?>
The functions you mention:
<?= paginate_links() ?>
<?= the_posts_pagination() ?>
<?= get_the_posts_pagination() ?>
All require that the global $wp_query variable contains the necessary fields to generate the pagination (namely found_posts, paged, and posts_per_page).
Since you're using get_posts, the fields in $wp_query will never be set.
Instead, you can overwrite the $wp_query variable with a WP_Query instance which means the fields will be set. It is good practice to then restore $wp_query after your loop in case you have any data later in the page or in the footer that relies on it.
Example:
global $wp_query;
$args = array(
'posts_per_page' => 9,
// 'offset' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
// this one is important, it lets the query object know what page you're on.
'paged' => get_query_var( 'paged' ),
// are you sure you want to suppress filters ? make sure you know what this does.
'suppress_filters' => true,
);
$wp_query = new WP_Query( $args );
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
?>
<div class="col-md-4 col-sm-12 mt-3">
<div class="bg-gray h-100 blog-cards">
<a href="<?php echo get_the_permalink(); ?>" title="more">
<div>
<div class="archive-img">
<?php echo get_the_post_thumbnail(); ?>
</div>
<h3 class="upper mb-0 px-3 pt-3 pb-0 text-dark"> <?php echo get_the_title(); ?></h3>
<div class="p-3 pt-0">
<?php the_excerpt(); ?>
<?php
global $post;
foreach ( get_the_category( get_the_ID() ) as $category ) {
echo '' . $category->name . '';
}
?>
|
<span>
مدت زمان مطالعه
<?php echo get_field( 'duration' ); ?>
دقیقه
</span>
</div>
</div>
</a>
</div>
</div>
<?php
endwhile;
the_posts_pagination();
// restore the original $wp_query.
wp_reset_postdata();
I have this post type that I want to loop through. I have to create two different sections. The top works where it loops through all. But I want to exclude the parent that's holding the children (#6474) and everything in that parent to loop in a different row.
What I have so far. This works in regards of posting all posts. But at the moment includes all parent and children minus #6474. Just want this to show ones that are only parents.
Trying to figure out how to approach creating another row that will only show the children in the post type.
$customersPage_args = array (
'post_type' => array( $global_cat ),
'post_status' => array( 'publish' ),
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'publish_date',
'post__not_in' => array(6474) //excluding the ID holding the children
);
$global_cat_query = new WP_Query( $customers_sort ); ?>
<h3 class="h2 display <?php echo $block[className]; ?>"><?php echo $block_heading; ?></h3>
<div class="card-row">
<div class="card u-pb-0">
<div class="row">
<?php // The Loop
if ( $global_cat_query->have_posts() ) :
while ( $global_cat_query->have_posts() ) : $global_cat_query->the_post(); ?>
<div class="col-md-3 col-sm-4 col-6">
<a href="<?php echo get_permalink(); ?>">
<div class="card card u-mt-0 u-mb-4 align-items-center">
<img src="<?php echo get_the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" />
</div>
</a>
</div>
<?php endwhile;
endif;
// Restore original Post Data
wp_reset_postdata(); ?>
</div>
</div>
</div>
<?php endif; ?>
If you only want top level items, you can make use of the post_parent parameter. If you set it to 0, it will only find "parent" (aka "Top-Level" posts):
$customersPage_args = array (
'post_type' => array( $global_cat ),
'post_status' => array( 'publish' ),
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'publish_date',
'post__not_in' => array(6474), //excluding the ID holding the children
'post_parent' => 0, // Only get "Top Level" posts
);
You can use this function to detect if a post has a parent:
https://developer.wordpress.org/reference/functions/wp_get_post_parent_id/
Pagination isn't working in a Wordpress blog. I've tracked the specific file responsible, to home.php.
The original blog page displays posts fine (10 at a time), however as mentioned pagination isn't working.
<?php
/**
Category Page
*/
get_header(); ?>
<div class="container-fluid blogs post-section" id="blogs">
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-9 col-lg-8">
<?php echo category_description( $category_id ); ?>
<div class="category-posts">
<?php
$args = array(
"post_type" => "post",
"post_status" => "publish",
"posts_per_page" => "10",
"orderby" => "date",
"order" => "DESC"
);
$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<div class="col-sm-12">
<div class="inner-post">
<?php if ( has_post_thumbnail() ) { ?><div class="thumbnail col-sm-4" style="background:none!important;"><?php the_post_thumbnail(); ?></div><?php } ?>
<div class="abso custom_abso col-sm-8"><div class="inner-box">
<div class="date">
<?php
$archive_year = get_the_time('Y');
$archive_month = get_the_time('M');
$archive_day = get_the_time('d');
?>
<span class="month"><?php echo $archive_month; ?></span> <span class="day"><?php echo $archive_day; ?>,</span><span class="year"><?php echo $archive_year; ?></span>
</div>
<h2 style="margin-top:0;"><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
<p><a href="<?php the_permalink(); ?>" class="readmores">Read More <i class="fa fa-caret-right" aria-hidden="true"></i>
</a></p>
</div>
<?php endwhile; endif;
?></div>
<div class="pagination text-center">
<?php the_posts_pagination( array( 'mid_size' => 2 ) ); ?>
</div>
<?php wp_reset_query(); ?>
</div>
<?php get_sidebar(); ?>
</div></div></div>
<?php get_footer(); ?>
Based on this Wordpress documentation, I've updated the top of the file, to look like the following:
<div class="category-posts">
<?php $args = array(
'posts_per_page' => 10,
'offset' => 0,
'cat' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true,
'fields' => '',
);
$posts_array = get_posts( $args ); ?>
<div class="col-sm-12">
This has worked, in that the pagination buttons work, however, only one post is displayed per page:
Any advice on how to display 10 posts per page?
Please add the following parameter 'paged'. It sets pagination query variable. The pagination is works as per this parameter.
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
to the $args.