For example i will have 4 categories. For each category i will show 5 recent post. I will have category such as breakfast, dessert, lunch and savory food. There will be a "see all" link for each category, so the user can see all the breakfast category post. On the main page i will list 5 recent post and when the users click the "see all" link it will link them to the whole breakfast category. I want all breakfast in that category if the they click "see all". The same for other categories.
Currently my code looks like this, but i'm stuck with the "see all" link. I don't know how to link it to main category.
<?php
get_header();
?>
<!-- recipe -->
<section class="recipe-wrap">
<?php
/*
* Loop through Categories and Display Posts within
*/
$post_type = 'recipe';
$category_link = get_category_link($cat->cat_ID);
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) : ?>
<div class="recipe-category owl-carousel-slide">
<div class="row">
<h2><?php echo $term->name; ?>see all</h2>
<div class="recipe-category-carousel owl-carousel owl-theme">
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => 10, //show all posts
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<div class="item recipe-box">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo(types_render_field('artwork', array('raw' => true) )); ?>">
<p><?php the_title(); ?></p>
</a>
</div>
<?php endwhile; endif; ?>
</div>
</section>
<?php endforeach;
endforeach; ?>
</div>
</div>
</div>
</section>
<!-- /recipe -->
<?php
get_footer();
?>
Try below code
foreach ( $terms as $term ) {
// Get term link by using get_term_link()
$term_link = get_term_link( $term );
echo '' . $term->name . '';
}
<div id="mini_stream">
<ul>
<? $args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'category_name'=>'Product',
);
$loop = new wp_Query($args);
while($loop->have_posts()) : $loop->the_post();
echo '<a href="'.get_permalink().'">';
echo get_the_post_thumbnail($post->ID, 'category-thumb');
the_title( '<h6>', '</h6>' );
echo '</a>';
endwhile;
wp_reset_query(); ?>
</ul>
</div>
=> Using this method may you get all post
Related
I have a problem, i have a custom type post (authors) and taxonomy (manager and team) called position
I need to get all authors of the team on a page and need the manager to be at the first one of them
the authors ordered by name but the manager name begin with "T"
what can I do with this situation.
my code is
<?php
/**
* The template for displaying auther custom type on custom taconomy position filterd By the page name and position name
* Template Name: taxonomies
*
*/
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
get_header(); ?>
<?php
// echo the $slug name of the page
$page_names = get_the_title(); ?>
<section id="articles" class="articles">
<div class="<?php echo $classcase ?>">
<?php
$loop = new WP_Query( array( 'post_type'=>'authors','posts_per_page' => '-1','orderby' => 'title','order'=>'ASC' ) );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</section>
<div class='endauthors'></div>
For this task you need to use tax_query https://developer.wordpress.org/reference/classes/wp_tax_query/
First we take all managers sorted by title, then the entire team
Try this code
<?php
/**
* The template for displaying auther custom type on custom taconomy position filterd By the page name and position name
* Template Name: taxonomies
*
*/
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
get_header(); ?>
<?php
// echo the $slug name of the page
$page_names = get_the_title(); ?>
<section id="articles" class="articles">
<div class="<?php echo $classcase ?>">
<?php
$args = array(
'post_type' => 'authors',
'posts_per_page' => '-1',
'orderby' => 'title',
'order'=>'ASC'
'tax_query' => array(
array(
'taxonomy' => 'position',
'field' => 'slug',
'terms' => array( 'manager' )
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
<?php
$args = array(
'post_type' => 'authors',
'posts_per_page' => '-1',
'orderby' => 'title',
'order'=>'ASC'
'tax_query' => array(
array(
'taxonomy' => 'position',
'field' => 'slug',
'terms' => array( 'team' )
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</section>
<div class='endauthors'></div>
My code returns category name of default post type "post":
<div class="row masonary-wrap">
<?php
$args = array(
'post_type' => 'project',
'post_status' => 'publish',
'posts_per_page' => '6'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$categories = get_categories( $args );
foreach ( $categories as $cat ) : ?>
<div class="col-lg-4 col-md-6 col-12 port-item mb-30 <?php echo esc_attr( $cat->cat_name ); ?>">
<div class="project">
<div class="proj-img">
<div class="proj-overlay">
<h5><?php the_title(); ?></h5>
</div>
</div>
</div>
</div>
<?php
endforeach;
endwhile;
wp_reset_postdata();
endif; ?>
</div>
I need to display category name or better slug of custom post type "project", not of default posts!
you can do something like that. please check the below code
$terms = get_the_terms( $post->ID , 'project' );
foreach ( $terms as $term ) {
echo $term->name;
}
thanks,
I've been trying to get isotope.js working on a Wordpress site. I've been following this tutorial https://www.aliciaramirez.com/2014/03/integrating-isotope-with-wordpress/ and have been able to get it all functioning. For my design, I'm trying to add <div class="grid-sizer"></div> every four posts that are called. I've been referring to this question: Wrap every 4 posts in a custom wordpress loop with a div but cannot seem to figure out the proper placement for the count and i statements. Can anyone help me figure this out? Here's my loop right now:
<?php
$terms = get_terms( array(
'taxonomy' => 'solutions',
'parent' => 0
)
); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => asc,
'tax_query' => array(
array(
'taxonomy' => 'solutions',
'field' => 'term_id',
'terms' => get_queried_object()->term_id,
),
) );
$the_query = new WP_Query( $args ); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "solutions" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
$i = 0;
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
if($i%4 == 0) {
echo "<div class='grid-sizer'> </div>";
}
?>
<div class="<?php echo $termsString; ?>item">
<p class="product-image"><a href="<?php the_permalink(); ?>" ><img src="<?php the_field("product_image") ?>" alt="<?php the_title(); ?>" class="solution-image" /></a></p>
<h4 class="product-name">
<?php the_title(); ?>
</h4>
</div>
<?php $i++; ?>
<!-- end item -->
<?php endwhile; ?>
</div>
<!-- end isotope-list -->
<?php endif; ?>
Here's the resulting code - thanks for misorude's help!
<?php
$terms = get_terms( array(
'taxonomy' => 'solutions',
'parent' => 0
)
); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php
$i = 0;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => asc,
'tax_query' => array(
array(
'taxonomy' => 'solutions',
'field' => 'term_id',
'terms' => get_queried_object()->term_id,
),
) );
$the_query = new WP_Query( $args ); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "solutions" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
if($i%4 == 0) {
echo "<div class='grid-sizer'> </div>";
}
?>
<div class="<?php echo $termsString; ?>item">
<p class="product-image"><a href="<?php the_permalink(); ?>" ><img src="<?php the_field("product_image") ?>" alt="<?php the_title(); ?>" class="solution-image" /></a></p>
<h4 class="product-name">
<?php the_title(); ?>
</h4>
</div>
<?php $i++; ?>
<!-- end item -->
<?php endwhile; ?>
</div>
<!-- end isotope-list -->
<?php endif; ?>
I am having a custom taxonomy say "project-type" which is registered for the custom post "projects" and under that I have terms "catOne" and "catTwo". Now I want to display all the custom posts that are linked to catOne using the term_id of "catOne", which in my case is 9.
So I am successfully able to loop through all the posts but it is displaying only the ID but not all contents.
My approach:
$cat_id = 9;
$args = array(
'post_type' => 'projects',
'tax_query' => array(
array(
'taxonomy' => 'project-type',
'field' => 'term_id',
'terms' => array( $cat_id )
),
),
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
setup_postdata( $post ); ?>
<div id="post-<?php echo $post->ID; ?> <?php post_class(); ?>">
<h1 class="posttitle"><?php the_title(); ?></h1>
<div id="post-content">
<?php the_excerpt(); ?>
</div>
</div>
<?php } wp_reset_postdata();
Output I am getting
<div id="post-137 class=" ""="">
<h1 class="posttitle"></h1>
<div id="post-content">
</div>
</div>
<div id="post-135 class=" ""="">
<h1 class="posttitle"></h1>
<div id="post-content">
</div>
</div>
Can someone please help me with where I am going wrong?
Instead of using post_class(), the_permalink(), the_title(), and the_excerpt(), you should use the $post object to get the data, just like you did with $post->ID. The functions you've used should be called only if you're using a loop based on have_posts(). You aren't, so replace them with:
post_class() -> get_post_class( '', $post->ID )
the_permalink() -> get_the_permalink( $post->ID )
the_title() -> $post->title
the_excerpt() -> $post->post_excerpt
I feel like this should be very simple but I've been searching for 15 mins and can't find it anywhere:
I have a loop that lists products in a certain category, and I need to modify it so that the picture and title are a link to the individual product page. Something like this is what I'm looking for.
<a href="<?php echo woocommerce_get_the_permalink() ?>">
This is what I have:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'product_cat' => 'power-chairs', 'orderby' => 'ID' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<div class="product">
<div class="padding">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder"/>'; ?>
<p class="caption"><?php the_title(); ?></p>
</div><!-- padding -->
</div><!-- product -->
<?php endwhile; ?>
Edit: Found it - it was href="get_permalink($product_id)"
Have you tried adding the a href link around the image and the title:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'product_cat' => 'power-chairs', 'orderby' => 'ID' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<div class="product">
<div class="padding">
<a href="<?php echo woocommerce_get_the_permalink() ?>">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder"/>'; ?></a>
<p class="caption"><?php the_title(); ?></p>
</div><!-- padding -->
</div><!-- product -->