adding pagination to a foreach loop in wordpress - php

Am looking for a way to implement pagination on my categories
i tried to use paginated_links(); functions but it works only for posts, and not the actual categories. I have looked around and it seems this is a little trickier than I was expecting...
$cats = get_categories(
array('parent' => $cat->cat_ID)
);
// loop through the categries
foreach ($cats as $cat) {
echo '<div class="card">';
// setup the cateogory ID
$cat_id= $cat->term_id;
echo '<div class="card-body">';
// make images for the category
echo '<div class="card-title">' . $cat->name . ' <i class="fas fa-arrow-right"></i></div>';
echo '</div>';
// create a custom wordpress query
query_posts("cat=$cat_id&posts_per_page=3");
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php echo '<div class="blog-list-points">' ?>
<?php // create our link now that the post is setup ?>
<a href="<?php the_title();?>">
<?php the_excerpt(); ?>
<hr>
</a>
<?php echo '</div>'?>
<?php endwhile; endif; echo ' </div> '// done our wordpress loop. Will start again for each category ?>
<?php }
// done the foreach statement ?>

Related

Wordpress - How to exclude a category from being shown

<div class="small-12 columns">
<ul class="category-filters">
<?php
$args = array(
'parent' => 0,
'exclude' => '1, 9'
);
$cats = get_categories($args);
foreach($cats as $cat) {
$output =
'<li>
<input class="checkbox" type="checkbox" value="' . $cat->cat_name . '">'
. $cat->cat_name .
'</li>';
echo $output;
}
?>
</ul>
<h4>Meet our Coaches</h4>
<?php
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1));
if ( $wpb_all_query->have_posts() ) : ?>
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<div class="callout horizontal word-wrap"
data-category="
<?php
$cats=get_the_category();
echo $cats[0]->cat_name;
?>">
<?php the_post_thumbnail() ?>
<h5><?php the_title(); ?></h5>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
I have the above php. It creates a list of checkboxes and then creates a list on callouts looping through all of the categories in wordpress.
I would like to know how I could modify this code so that it never creates and checkboxes or callouts for the category 'coaching'.
In addition to this question it would be great to know if there is a better way to get my desired outcome through something that wordpress has to offer that I have missed.

Outputting a posts categories as a class

My existing code outputs all posts in the portfolio category, however I want to also add a class to each item that is populated by the categories the post relates to. I've tried using get_the_category and think i'm nearly there but can't work out what I haven't quite done correct:
<?php query_posts('category_name=portfolio&order=DSC&orderby=ID&posts_per_page=20');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$portfolio_link = get_post_meta($post->ID, 'portfolio_link', true);
$categories = get_the_category($postID);
?>
<li class="<?php echo $categories->cat_name;?>">
<?php if ($portfolio_link) { echo "<a href='$portfolio_link'>"; } ?>
<?php the_post_thumbnail('small'); ?>
<?php if ($portfolio_link) { echo "</a>"; } ?>
</li>
<?php endwhile; endif; wp_reset_query();?>
At the moment no classes are output at all, but no errors are shown either!
get_the_category() returns an array so you need to iterate over it.
$catNames = array();
foreach($categories as $category) {
$catNames[] = $category->cat_name;
}
$classes = implode(' ', $catNames);
<li class="<?php echo $classes ?>">

List Wordpress categories in sidebar with image

I'm to list categories with images in the sidebar this is how I do it (and it works) I do this because I've certain categories I do not want to display!
<?php $latests = new WP_Query('posts_per_page=2&ignore_sticky_posts=1&cat=12'); ?>
<?php echo get_cat_name(12); ?>
<?php while ($latests->have_posts()) : $latests->the_post(); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata(); ?>
but I need to copy past that code for every single category... and all this code for only changing a number is a good practice I guess. Is there another way it could be done?
I've tried with a foreach but it seems to be wrong
<?php $latests = new WP_Query('posts_per_page=2&ignore_sticky_posts=1&cat=12'); ?>
<?php foreach($latests as $latest) :?>
<?php while ($latests->have_posts()) : $latests->the_post(); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata(); ?>
<?php endforeach; ?>
Well, You can do it like this:
<ul>
<?php
$cat_args=array(
// 'include' => '3,6,9', // display only these categories
'exclude' => '3,6,9', // display all categories except categories 3,6,9
'orderby' => 'name', // the order
'order' => 'ASC' // asc or desc
);
$categories=get_categories($cat_args);
foreach($categories as $category) {
$args=array(
'showposts' => 2, // how many posts you want to display
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
$posts=get_posts($args);
if ($posts) {
echo '<h3> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in: %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h3> ';
foreach($posts as $post) {
setup_postdata($post);
?>
<li>
<div>
<div><?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?></div>
<div><?php the_title(); ?></div>
</div>
</li>
<?php
} // close foreach
} // close if
} // close foreach
?>
</ul>
The easiest way is like that
<?php wp_list_categories('orderby=name&exclude=3,5,9,16'); ?>
So, this will return you all the categories excluding the one you specified. After this, you can get the actual image you want for your categories and all.

How to resolve problems with WordPress loop?

I have created WordPress loop for custom post type - portfolio and it does work. However I need to add message when no posts are displayed but it returns error in any way I have tried.
Here is my working WordPress loop:
$gallery = new WP_Query($args);
while($gallery->have_posts()): $gallery->the_post();
if(has_post_thumbnail()):
$data_types = '';
$item_cats = get_the_terms($post->ID, 'portfolio_category');
if($item_cats):
foreach($item_cats as $item_cat) {
$data_types .= $item_cat->slug . ' ';
}
endif;
$full_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'portfolio-full'); ?>
<li data-id="<?php echo $post->ID;?>" class="portfolio_item" data-type="<?php echo $data_types;?>">
<a href="<?php the_permalink(); ?>" rel="prettyPhoto" title="">
<span class="pic"><?php the_post_thumbnail('portfolio-medium'); ?><div class="img_overlay"></div></span>
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endif; endwhile; ?>
And I have tried to close the loop like this:
<?php endwhile; ?>
<?php else : ?>
<p>My Text Here</p>
<?php endif; ?>
Instead of the current:
<?php endif; endwhile; ?>
I get error "syntax error, unexpected T_ENDWHILE"
Am I missing something important here?
i would wrap the loop in an if statement, which you can use to check whether there are any posts - like so:
<?php
$gallery = new WP_Query($args); // initialize query
if( $gallery->have_posts() ): // if there are posts
while( $gallery->have_posts() ) : $gallery->the_post(); // the loop
// do stuff with the post here
endwhile; // end of the loop
else: // if there aren't any posts
echo "<p>no posts found!</p>";
endif; // end of the if statement
?>

PHP/Wordpress: list posts per sub-category

In the below code there's a loop that features all products under a given category:
<?php
wp_reset_query();
query_posts($query_string . '&posts_per_page=15&paged=' . $paged);
if (have_posts()) :
while ( have_posts() ) : the_post();
$price = get_post_meta(get_the_ID(), 'inception_price', true);
?>
<div class="omc-product-listing">
<a href="<?php the_permalink();?>">
<?php
if(has_post_thumbnail()) {
the_post_thumbnail('product', array('class' => 'omc-product-frame'));
} else {
?>
<img src="<?php echo get_template_directory_uri() ;?>/images/no-image.png" width="170" height="170" class="omc-product-frame" alt="no photo" />
<?php } ?>
</a>
<span class="omc-listing-header"><?php the_title();?></span>
<span class="omc-listing-price"><?php echo($price);?></span>
<span class="omc-listing-more">ยป</span>
</div><!-- /omc-product-listing -->
<?php endwhile; ?>
<br class="clear" />
<div class="product-pagination">
<?php kriesi_pagination(); ?>
</div>
<br class="clear" />
<?php endif; wp_reset_query(); ?>
In this loop, I'd like to pull out every product for a given category (that's what the code does now), but display then "per subcategory", like this:
For category publications:
Books:
Book 33
Book 32
Book 1
...
Mobile Apps:
App 12
App 76
...
...
I think the code above needs a foreach loop, like below, but I don't know how to implement it in this case.
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categories
foreach ($cats as $cat) {
// setup the categories ID
$cat_id= $cat->term_id;
// Make a header for the categories
echo "<h2>".$cat->name."</h2>";
// create a custom wordpress query
query_posts("cat=$cat_id&post_per_page=100");
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<?php the_title(); ?>
<?php echo '<hr/>'; ?>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
I've taken this request to wpquestions.com. It just got resolved by Abdessamad Idrissi. the answer is too long to copy it here, so I'm posting the discussion link and the code link here, in case someone has the same need.
You could try the following:
$cats = get_categories();
foreach ($cats as $cat) :
// setup the categories ID
$cat_id= $cat->term_id;
// Make a header for the categories
echo "<h2>".$cat->name."</h2>";
$args = array( 'cat' => $cat_id, 'posts_per_page' => 100 );
$posts = get_posts($args);
if($posts) :
foreach($posts as $post) : setup_postdata($post); ?>
<?php the_title(); ?>
<?php endforeach; // foreach($posts)
endif; // if($posts)
endforeach; // foreach($cats)
Not tested this but it should put you in the right direction!

Categories