I'm doing something similar. I have a recipe blog and I will have different categories such as breakfast, dessert, lunch, etc... But I don't know how to link them the category pages. For example, If the users click "see all" for the breakfast category, it will show all breakfast post from the breakfast category. So that means it will link to the main breakfast category page. For each category i will show 10 recent post so the users is not seeing all post. So they have the option to see all the post if they click the "see all" link. It will link them to the main category page with all breakfast post. Of course if they click the dessert "see all" link, it will link them to the dessert category page which they can see all dessert posts.
This is my current code:
(but what php code should i put for the "see all" link? IF you take a look at my "see all" a tag, it's # right now. It's not linking to somewhere, but how do i make it dynamic to link to the categories pages. So for example if i'm in the dessert section, if i click "see all" it will link to the dessert category. )
Please also take a look at my design
<?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();
?>
Please see the example that I provided, so you know what i'm talking about.
I have attach some images and link.
This is the example of the links.
This is the blog link
https://iamsteve.me/blog
Design Category link
https://iamsteve.me/blog/category/design
An example what I want
If you try to link to each category page, you can use get_term_link() like so:
$category_page_link = get_term_link($term);
<h2><?php echo $term->name; ?>see all</h2>
The above goes inside the second foreach loop.
EDIT:
This is the full code, I put the function get_term_link() where it should be. Also you had some problems with opening/closing html tags:
<?php get_header(); ?>
<?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 ) );
?>
<section class="recipe-wrap">
<?php foreach ( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$term_page_link = get_term_link($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; ?>
<?php endif; ?>
</div><!-- .owl-theme -->
</div><!-- .row -->
</div><!-- owl-carousel-slide -->
<?php endforeach; // terms loop ?>
<?php endforeach; // taxonomies loop ?>
</section><!-- /recipe -->
<?php get_footer(); ?>
Hope that helps.
Related
I have a working '$related = get_posts' masonry on a single.php page. I also added a hover overlay, so that when the user hovers on the thumbnail, a transparent overlay appears as well as the descriptions (title name, category and nickname).
The problem I am facing is that when I hover on one related post thumbnail, the overlay appears for every post (the overlay is stretched, it is not appearing individually). I've also attempted to call out the descriptions, but it's only calling the current post I am viewing (e.x. the current single.php header says Snow, when I hover the first related post, it also calls out the description for Snow), however, if you click on the first related post thumbnail, it takes you to a different article (it is not calling the description for the different article).
<div class="related">
<h3>Related</h3>
<div class="js-masonry">
<div class="overlay">
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'orderby' => 'rand', 'numberposts' => 3, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) { setup_postdata($post); ?>
<?php the_post_thumbnail(array(300,300)); ?>
<?php } wp_reset_postdata(); ?>
<div class="posts">
<?php foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';}?>
<h1><?php the_title();?></h1>
————
<h4><?php the_author();?></h4>
</div>
</div>
</div>
</div>
As the title says, how do I pull the correct description and overlay for one post via hover for ' $related = get_posts' on the single.php page in WordPress?
I resolved the issue by reorganizing the code correctly.
<div class="js-masonry">
<?php $args = array(
'category__in' => wp_get_post_categories( get_queried_object_id() ),
'posts_per_page' => 3,
'orderby' => 'rand',
'post__not_in' => array( get_queried_object_id() )
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="item-masonry overlay">
<a href="<?php the_permalink();?>">
<div class="posts">
<?php foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';}?>
<h1><?php the_title();?></h1>
————
<h4><?php the_author();?></h4>
</div>
<?php the_post_thumbnail(array(300,300)); ?>
</div>
</a>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
I have a custom post type of hardware with a custom taxonomy called hardware_categories attached to it.
I have a piece of code that outputs each taxonomy as a H3 and under each one it outputs the hardware items inside that taxonomy.
I'm having trouble adding a permalink link around the taxonomy name.
The code I'm trying is here:
<div class="container hardware-archive-container">
<?php // Output all Taxonomies names with their respective items
$terms = get_terms('hardware_categories');
foreach(
$terms as $term ):
$term_link = get_term_link( $term );
?>
<h2><?php echo $term->name; ?></h2>
<div class="row justify-content-center hardware-archive-row">
<?php
$posts = get_posts(array(
'post_type' => 'hardware',
'taxonomy' => $term->taxonomy,
'term' => $term->slug,
'nopaging' => true, // to show all posts in this taxonomy, could also use 'numberposts' => -1 instead
));
foreach($posts as $post): // begin cycle through posts of this taxonmy
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
?>
<div class="col-md-3">
<?php
$image = get_field( 'hardware_main_image');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
<?php the_title(); ?>
</div>
<?php endforeach; ?>
</div>
<p>See all products from: <?php echo $term->name;?></p>
<?php endforeach; ?>
</div>
The parts I added to try and generate a link are these two lines:
$term_link = get_term_link( $term );
and
<h2><?php echo $term->name; ?></h2>
Ive tried a lot of other things, but I can't seem to get category link...
Can anyone tell me where I'm going wrong? Thanks for looking.
Could you try this:
$term_link = get_term_link( $term->term_id, "hardware_categories" );
Also try to modify your terms:
$terms = get_terms( array(
'taxonomy' => 'taxonomy_name',
'hide_empty' => false
) );
The way you're using get_terms is deprecated see the documentation
Your code should look like this
$terms = get_terms( array(
'taxonomy' => 'hardware_categories',
'hide_empty' => false
) );
foreach( $terms as $term ):?>
<h2><?php echo $term->name; ?></h2>
<?php endforeach;?>
I have a WordPress loop that is pulling an ACF field. I need to determine if the field names are the same and if so I want to wrap them in a div. I have created a custom index page, but we want to be able to style fields with the same author name as a dropdown. So I need to somehow compare if the are the same.
This is the site I am working on http://test.improveyourenglish.com/library/
So for instance I would like to wrap "Jane Austin" in a div so that I can style it as a dropdown.
Thanks so much any help is greatly appreciated.
This is the code I am currently using
add_action('genesis_loop', 'book_archive_page');
function book_archive_page() {
echo '<div class="left-side">';
echo '<p>The following titles are sorted by author surnames.</p>';
?><div class="enter"><a href="#$term->name"><?php echo $term->name; ?>
</div></a><?php
$post_type = 'book';
// 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 ) : ?>
<section class="category-section">
<div class="row">
<div class="span12">
<a name="<?php echo $term->name; ?>"><h2 style="padding-
top: 300px; margin-top: -300px;"><?php echo $term->name; ?></h2>
</a>
</div>
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1, //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="span4">
<article class="inner-post clearfix">
<div class="inner-content">
<div class="author-archive-text"><?php the_field('author_full_name'); ?></div><div class="title-archive-book"><?php echo get_the_title(); ?></div>
</div>
</article>
</div>
<?php endwhile; endif; ?>
</div>
<hr>
</section>
<?php endforeach;
endforeach; ?>
<?php
}
echo '</div>';
If I understand what you want correctly, what you need to do is to record the "current" author name in a variable, and use it in your loop to compare it to the next author name. If it is a different author, then end the previous author's wrapper and start a new wrapper for the next author.
$current_author = ""; // variable to store the current author
$posts = new WP_Query($args); ?>
<div class="author-wrapper"> <!-- start the wrapper div for the first author -->
<?php if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<?php
// check if we have a new author
if (the_field('author_full_name') != $current_author) {
// update current_author var to make the new author the current one
$current_author = the_field('author_full_name');
// close the previous author's wrapper and start a new one
?>
</div>
<div class="author-wrapper">
<?php } ?>
<div class="span4">
<article class="inner-post clearfix">
<div class="inner-content">
<div class="author-archive-text"><?php the_field('author_full_name'); ?></div><div class="title-archive-book"><?php echo get_the_title(); ?></div>
</div>
</article>
</div>
<?php endwhile; endif; ?>
</div> <!-- end the wrapper div for the last author -->
I had this query:
$args=array(
'post_type' => array('product'),
'order' => 'DESC',
'orderby' => 'rand',
'showposts' => 24
);
query_posts( $args );
if(have_posts()) : while(have_posts()) : the_post();
?>
<div class="col-xs-12 col-s-6 col-sm-4 col-md-3 our-products">
<a class="products-img" href="<?php the_permalink();?>"><?php if ( has_post_thumbnail() ) {the_post_thumbnail('product-size',array('class' => 'img-responsive'));}?></a>
<a class="products-tit" href="<?php the_permalink();?>">
<h2><b><?php the_title();?></b></h2>
</a>
</div>
<?php
endwhile; endif;
wp_reset_query();
?>
I need to get tax term of product belong to it.
Example:
I had Category: Products -> Men fashion/Women fashion -> T-shirt -> product A.
How can i get the term of T-shirt which product A belong to it?
You can use WordPress get_the_terms() function for this purpose.
Here is the example:
$_terms = get_the_terms( $post->ID , 'Your Taxonomy' );
In "Your Taxonomy" you put the name of the taxonomy that you just registered e.g. product-category
So just like this:
$_terms = get_the_terms( $post->ID , 'product-category' );
And then
<?php
foreach($_terms as $_term) {
?>
<a href="<?php echo get_term_link($_term->slug, 'product-category') ?>">
<?php echo $_term->name ?>
</a>
<?php
}
?>
This function get_term_link() is use for term link.
Hope this will help you.
I'm trying to get an unformatted list (preferably a list of slugs) of the categories for a single post in a custom post type loop. This list will eventually serve as a class for a div ($CATEGORYSLUGSWILLEVENTUALLYGOHERE).
I've found a few different methods of getting a list of ALL of the categories for a custom post type, but not the ones specific to a single one. Here's what I have so far:
<?php $projects_loop = new WP_Query( array( 'post_type' => 'projects', 'orderby' => 'menu_order' ) ); ?>
<?php while ( $projects_loop->have_posts() ) : $projects_loop->the_post(); ?>
<div class="box <?php $CATEGORYSLUGSWILLEVENTUALLYGOHERE; ?>">
<div class="port-item-home">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
<p><?php the_title(); ?></p>
</div>
</div>
<?php endwhile; ?>
And here's what I've tried so far to get the category list:
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'taxonomy' => 'project-type'
);
$categories = get_categories( $args );
echo '<p> '.print_r(array_values($categories)).'something</p>'
?>
I have it returning the array - but the array is showing that it'll display all categories instead of the ones pertaining to that specific post.
I also tried:
<?php
//list terms in a given taxonomy (useful as a widget for twentyten)
$taxonomy = 'project-type';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo $tax_term->name;
}
?>
And that also displays all categories instead of the ones pertaining to the post.
Any suggestions??
Got it! Found this article that helped me out:
https://wordpress.org/support/topic/how-to-get-the-category-name-for-a-custom-post-type
<!-- The Query -->
<?php
$args = array(
'post_type' => 'my_post_type',
'posts_per_page' => -1,
'orderby' => 'menu_order' );
$custom_query = new WP_Query( $args );
?>
<!-- The Loop -->
<?php
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
$terms_slugs_string = '';
$terms = get_the_terms( $post->ID, 'my_post_type' );
if ( $terms && ! is_wp_error( $terms ) ) {
$term_slugs_array = array();
foreach ( $terms as $term ) {
$term_slugs_array[] = $term->slug;
}
$terms_slugs_string = join( " ", $term_slugs_array );
}
?>
<div class="box<?php echo $terms_slugs_string ?>">
<div class="port-item-home">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
</a>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</div>
<?php endwhile; ?>