I know this question was asked so many times, but none of the solutions are working for me.
I want to display my categories on the website with their thumbnails together.
I tried to use category imagies plugin, but for some reason this plugin stopped working and doesn't give me the opportunity to attach picture to the category (the add image button isn't shown in the category settings).
After this I created custom field, where I made a taxonomy is equal to category. With this I could pair images with categories, but I don't know how to add it to the code.
Here is my code:
<section id="categories">
<div class="center-box">
<div class="categories">
<?php
$categories = get_categories(array ('parent' => 0));
foreach($categories as $category) { ?>
<div class="category-box">
<a class="category-img" href="<?php echo get_category_link($category->term_id); ?>">
<img class="img" src="<?php echo z_taxonomy_image_url($category->term_id); ?>"/>
<div class="category-name">
<h3> <?php echo $category->name; ?> </h3>
</div>
</a>
</div>
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'date',
'order' => 'DESC',
'category' => $category->term_id, );
?>
<?php } ?>
</div>
</div>
</section>
It still has the previous category imagies plugin code which doesn't work anymore.
Someone could help me how to display the categories with image?
Thank you!
I'd suggest using the Advanced Custom Fields plugin for this, if you are not already. Under the working assumption that you are, you could do the following:
<section id="categories">
<div class="center-box">
<div class="categories">
<?php
$categories = get_categories(array ('parent' => 0));
foreach($categories as $category) { ?>
<div class="category-box">
<a class="category-img" href="<?php echo get_category_link($category->term_id); ?>">
<img class="img" src="<?php echo wp_get_attachment_image_src( get_field( 'thumbnail', "term_$category->term_id" ), 'small' ) ?>"/>
<div class="category-name">
<h3> <?php echo $category->name; ?> </h3>
</div>
</a>
</div>
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'date',
'order' => 'DESC',
'category' => $category->term_id, );
?>
<?php } ?>
</div>
</div>
Related
i make two wp query loop one is within the loop something went wrong i cant figure out here is the code
the inner wp query for image cpt related with main query cpt.
$maincontest = new WP_Query(array(
'posts_per_page' => 3,
'post_type' => 'contest',
'post_status' => 'any'
));
while($maincontest->have_posts()){
$maincontest->the_post(); ?>
<div class = "listloopmain">
<a href="<?php the_permalink(); ?>">
<div class = "listcontestitem">
<div class = "listimage">
<?php
$toplogo = new WP_Query(array(
'posts_per_page' => 1,
'post_type' => 'logo',
'orderby' => 'rand'
));
while($toplogo->have_posts()){
$toplogo->the_post();
$image = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<div class ="listim">
<img src="<?php echo $image ?>"/>
</div>
<?php }
wp_reset_postdata();
?>
</div>
<div class = "title_des">
<div class = "listtitle">
<h4>
<?php the_title(); ?>
</h4>
</div>
<div class = "listdescription">
<?php echo wp_trim_words(get_the_content(), 20); ?>
</div>
</div>
<div class = "listcontestprice">
$ <?php the_field('price'); ?>
</div>
<div class = "listtime">
5 Days
</div>
</div>
</a>
</div>
<?php } ?>
</div>
<div class ="my-pagination">
<?php
echo paginate_links(array(
'prev_text' => __( '<' ),
'next_text' => __( '>' )
));
wp_reset_postdata();
?>
Problem is main query permalink is correct but content is same for each loop. Image query is working fine. Can anybody help?
Thanks in advance
hi i got the solution want main custom query only for showing private post. achieved by filtering pre_get_posts and set method. thank u all
function contest_adjust_query($query){
if (!is_admin() AND is_post_type_archive('contest') AND $query->is_main_query()) {
$query->set('post_status', 'private');
$query->set('post_status', array('publish', 'private'));
}
}
add_action('pre_get_posts', 'contest_adjust_query');
At this part of your code -
while($toplogo->have_posts()){
$toplogo->the_post();
$image = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<div class ="listim">
<img src="<?php echo $image ?>"/>
</div>
<?php }
**wp_reset_postdata();**
Use $toplogo->reset_postdata();
Reference for the reset_postdata function you can find here - https://developer.wordpress.org/reference/classes/wp_query/reset_postdata/.
Also, here's a nice article explaining how it's used - https://neliosoftware.com/blog/the-problems-of-using-nested-loops-in-wordpress/.
Im working in WP and basically what I've got set up is some logic to create a separate carousel for each category of my blog.
For each carousel im displaying the top viewed posts for each category. Each Item in the carousel has a post thumbnail, author name and author avatar.
My problem(if you scroll down to the bottom of the home screen pst"meet the community") the posts are showing the right post names and thumbnails but something funky is going on with the author names and avatars. The first post in the shelf looks correct but after that each post is showing the wrong author.I really dont know how to handle this. Ive been mashing they keys for a week already. Any thoughts?
<!-- A shelf for each category by most posts and posts by highest view count-->
<?php
$cat_args = array(
'number' => 5,
'orderby' => 'count',
'post_type' => 'post',
'order' => 'DESC',
'child_of' => 0
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<div class="shelf_holder">';
echo '<div class="shelf_title"> <h1> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" )) . '" ' . '> View All</h5></div>';
echo '<div class="shelf_prev"><div class="backwards_icon"></div></div>';
echo '<div class="shelf_next"><div class="forward_icon"></div></div>';
echo '<div class="display-posts-listing grid">';
$post_args = array(
'numberposts' => 8,
'category' => $category->term_id,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<div class="listing-item">
<?php
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'medium');
list($url, $width, $height, $is_intermediate) = $thumbnail;
?>
<div class="media-wrapper">
<?php $post_views = get_post_meta( get_the_ID(), 'post_views_count', true );?>
<div class="counter"><div class="views_icon"></div><?php echo $post_views; ?></div>
<div class="save_card"> <?php the_favorites_button($post_id);?></div>
<div class="media-gradient" style="height:196px;"></div>
<div class="media" style="height:196px;width:<?php echo $width; ?>px;background-image:url(<?php echo $url; ?>);background-size:cover;overflow: hidden;background-position: center;width: 100%;left:0px;"></div>
</div>
<div class="tiny_head"><?php echo get_avatar( get_the_author_meta( 'ID' ), 32 );?> </div>
<div class="card_content">
<h3><?php the_title(); ?></h3>
<div class="entry-meta">
<?php
global $post;
$author_id=$post->display_name;
?>
<?php
the_author_meta( 'display_name', $author_id );
?>
<?php wp_reset_query(); ?>
</div>
</div>
</div> <!-- listing item -->
<?php
}
echo '</div class="display-posts-listing grid">';
echo '</div class="shelf_holder">';
} ?>
Replace your foreach with this:
foreach($posts as $post) : ?>
<div class="listing-item">
<?php
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'medium');
list($url, $width, $height, $is_intermediate) = $thumbnail;
$author_id=$post->post_author;
?>
<div class="media-wrapper">
<?php $post_views = get_post_meta( $post->ID, 'post_views_count', true );?>
<div class="counter"><div class="views_icon"></div><?= $post_views; ?></div>
<div class="save_card"> <?php the_favorites_button($post_id);?></div>
<div class="media-gradient" style="height:196px;"></div>
<div class="media" style="height:196px;width:<?= $width; ?>px;background-image:url(<?= $url; ?>);background-size:cover;overflow: hidden;background-position: center;width: 100%;left:0px;"></div>
</div>
<div class="tiny_head"><?= get_avatar( get_the_author_meta( 'ID' ), $author_id );?> </div>
<div class="card_content">
<h3><?= get_the_title($post); ?></h3>
<div class="entry-meta">
<?= get_the_author_meta( 'display_name', $author_id ); ?>
</div>
</div>
</div> <!-- listing item -->
<?php
endforeach;
You had some errors in the way you were using get_the_author_meta. It needs the author ID and you tried getting it using display_name. The one you used for the avatar is hardcoded. (That's only going to return the meta that belongs to ID 32).
There are a lot of inconsistencies in your code. Try to understand what is happening. Look up the functions you're using and what they do exactly. The PHP documentation and WordPress documentation both are excellent resources.
Basically i am trying to display the 3 most recent posts from a category on a wordpress site.
The Query gets the posts fine, but does not limit them to 3 posts on the page, it instead shows every posts in that category, Here is the complete code for the page, any ideas?
<?php
if (is_front_page( )) {
?>
<style>
.site-inner {
max-width: 100%;
}
</style>
<div class="sponsor-section">
<div class="one-third first">
<?php
$catquery = new WP_Query(array(
'posts_per_page' => 3,
'category_name' => 'general',
));
while($catquery->have_posts()) : $catquery->the_post();
?>
<h4><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></h4>
<p><i><?php echo get_the_date(); ?></i></p>
<p><?php the_excerpt(); ?></p>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</div>
<div class="one-third">
<!-- MAP start-->
[4web_scripts id="1"]
</div>
<div class="one-third">
<button>Book Now</button>
<br />
<?php putRevSlider("sponsors") ?>
</div>
</div>
<?php }
?>
If you write it like this it should work just fine:
$catquery = new WP_Query(array(
'post_type' => 'post'
'category_name' => 'general',
'posts_per_page' => 3,
'nopaging' => true
));
If this does not work, do you have sticky posts? They are always shown as far as I know.
I am working in a custom WP theme.I need to show each posts under individual categories, which is working fine.
I changed the category to taxonomy.Now, i want to show more info under each category name,but, i cannot understand,where should i put my code in the loop.
Specially the post counts under each categories.
<?php
/*
Template Name: Home Page
*/
get_header();
global $redux_demo;
?>
<div class="sroll"><div class="container">
<marquee><p> <?php echo $redux_demo['main-option-marquee']; ?></p></marquee>
</div></div>
<div class="container">
<div class="row">
<div class="col-sm-9"> </br>
<div class="content mCustomScrollbar" style="height: 690px;">
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
foreach($terms as $cat){
$cata_name = $cat->name;
$term_id = $cat->term_id;
?>
<div class="col-sm-6 col-md-4 col-lg-3 p10">
<div class="box">
<?php
//echo '<h3>'.$catname[0]->cat_name.'</h3>';
?><h3><a href="<?php echo home_url('index.php/category/'.$cata_name) ?>">
<?php echo $cata_name; ?></a></h3> <?php
$catqueryy = new WP_Query( 'cat='.$term_id.'&posts_per_page=4' );
while($catqueryy->have_posts()) : $catqueryy->the_post();
?>
<p class="post_title"><?php echo ''.__(get_the_title(),'rockon').''; ?></p>
<p class="post_cont"><?php echo get_the_excerpt(); ?></p>
<?php
endwhile;
?>
</div>
</div>
<?php } ?>
</div></br>
</div>
<div class="col-sm-3">
<h1></h1>
<?php get_sidebar(); ?>
<h1></h1>
</div>
</div>
</div>
<?php
get_footer();
?>
Custom taxonomy try:
$the_query = new WP_Query( array(
'post_type' => 'CUSTOM_POST_TYPE',
'tax_query' => array(
array(
'taxonomy' => 'CUSTOM_TAXONOMY',
'field' => 'id',
'terms' => TERM_ID
)
)
) );
$count = $the_query->found_posts;
https://wordpress.org/support/topic/counting-posts-within-categories
For Example:
<?php
/*
Template Name: Home Page
*/
get_header();
global $redux_demo;
?>
<div class="sroll"><div class="container">
<marquee><p> <?php echo $redux_demo['main-option-marquee']; ?></p></marquee>
</div></div>
<div class="container">
<div class="row">
<div class="col-sm-9"> </br>
<div class="content mCustomScrollbar" style="height: 690px;">
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
foreach($terms as $cat){
$cata_name = $cat->name;
$term_id = $cat->term_id;
?>
<div class="col-sm-6 col-md-4 col-lg-3 p10">
<div class="box">
<?php
//echo '<h3>'.$catname[0]->cat_name.'</h3>';
?><h3><a href="<?php echo home_url('index.php/category/'.$cata_name) ?>">
<?php echo $cata_name; ?></a></h3>
<?php
$catqueryy = new WP_Query( 'cat='.$term_id.'&posts_per_page=4' );
$count = $catqueryy->found_posts;
?>
<h3><?php echo "Post Count : ".$count; ?></h3>
<?php
while($catqueryy->have_posts()) : $catqueryy->the_post();
?>
<p class="post_title"><?php echo ''.__(get_the_title(),'rockon').''; ?></p>
<p class="post_cont"><?php echo get_the_excerpt(); ?></p>
<?php
endwhile;
?>
</div>
</div>
<?php } ?>
</div></br>
</div>
<div class="col-sm-3">
<h1></h1>
<?php get_sidebar(); ?>
<h1></h1>
</div>
</div>
</div>
<?php
get_footer();
?>
You can check this Link enter link description here
if it helps.. I guess, some one will write this code here as I also need to understand how it will work after you pass a query.
Actually you do not need to count anything by manually written code. If you look at the get_terms() description, you can see that WP counts this for you (if you set 'pad_counts' to true (or 1)). With this turned on there will be a "count" key and a number value in your response array with each category.
You can just "echo" it where you want to.
This way your
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
should look like this:
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
'pad_counts' => true,
) );
Notice that I added 'pad_counts' => true, to the query, so you will have the number you are looking for - without writing too much code.
If you want to do it manually, I suggest that you create a loop that fills an array with 'category => number' elements, and look up the required key-value pairs in the loop that writes out the HTML.
I was also searching for something similar to this question. I think, you need a code to display the post counts of each categories.
I have created a custom taxonomy for products. In custom taxonomy user can enter name and can upload a image. In the products page I can get the image and title of category. When user clicks on this it moves to next page where description of post and title is shown.
I also want to get image also in this next page.
Here is the code: For showing custom categories
foreach($terms as $term) {
$prod_meta = get_option("taxonomy_term_".$term->term_id);
if(($counter % 4) == 0) {
echo '<div class="row product-gallery">';
}
?>
<div class="col-sm-3">
<div class="product-warp">
<div class="product">
<img src="<?php echo $prod_meta['img']; ?>" title="" alt="">
</div>
<div class="product-name">
<h5>
<a href="<?php echo get_term_link($term->slug, 'product_categories') ?>">
<?php echo $term->name; ?>
</a>
</h5>
</div>
</div>
</div>
For product detail page
<?php
get_header();
$slug = get_queried_object()->slug; // get clicked category slug
$name = get_queried_object()->name; // get clicked category name
$tax_post_args = array(
'post_type' => 'products', // your post type
'posts_per_page' => 999,
'orderby' => 'id',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product_categories', // your taxonomy
'field' => 'slug',
'terms' => $slug
)
)
);
$tax_post_qry = new WP_Query($tax_post_args);
if ($tax_post_qry->have_posts())
while($tax_post_qry->have_posts()) :
$tax_post_qry->the_post(); ?>
<div class="row product-details">
<div class="col-sm-7">
<div class="fire-product">
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>