I am learning to create a Portfolio section. I already created the custom post type and a custom taxonomy for the portfolio categories. The categories are working ok, i can choose which category i want for every portfolio item.
I am trying to loop within the post_type portfolio to get the items and it works ok, but i cannot get the categories of each item.
<?php $loop = new WP_Query( array( 'post_type' => 'portfolio') ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="panel">
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<p><?php the_category(); ?></p>
</div>
<?php endwhile; wp_reset_query(); ?>
I am using the above code, but the categories dont show up.
I tried separately to display the categories and are working ok with this code:
<?php
$args = array( 'taxonomy' => 'portfolio_categories', );
$categories = get_categories($args);
foreach($categories as $category) { ?>
<?php echo $category->name;?>
<?php }
?>
So how can i display each portfolio item categories in the loop ?
Try following code, this will print all custom taxonomies of post.
<?php
$terms = get_the_terms( $post->ID, 'portfolio_categories' );
if ( $terms && ! is_wp_error( $terms ) ) :
$taxonomies = array();
foreach ( $terms as $term ) {
$taxonomies[] = $term->name;
}
$taxonomies = implode(", ", $taxonomies );
?>
<p class="Custom-Taxonomies">
Custom Taxonomies: <span><?php echo $taxonomies; ?> </span>
</p>
<?php endif; ?>
Related
I have a custom post type that uses ACF for image galleries. All works as it should on individual custom posts, now I want to display all the image galleries together like one big master image gallery through a loop on a page. It's kind of working - but only one image gallery is being displayed. What can I do to make them all show (currently there are 3 galleries but will grow over time)?
Here is the code that's almost there:
<?php
$loop = new WP_Query( array( 'post_type' => 'galleries', 'nopaging' => true) );
while ( $loop->have_posts() ) : $loop->the_post();
$images = get_field('image_gallery');
endwhile;
if( $images ): ?>
<?php foreach( $images as $image ): ?>
<img class="gallery-image" src="<?php echo esc_url($image['url']); ?>" alt="">
<?php endforeach; ?>
<?php endif; ?>
The problem is that in the while loop, you only assign the gallery to the $images variable, which effectively overwrites that variable with each iteration. So what you get at the end is the gallery for the last post.
In this case, simply include the output in a while loop:
<?php
$loop = new WP_Query( array( 'post_type' => 'galleries', 'nopaging' => true) );
while ( $loop->have_posts() ) : $loop->the_post();
$images = get_field('image_gallery');
if( $images ) :
foreach( $images as $image ) :
?><img class="gallery-image" src="<?php echo esc_url($image['url']); ?>" alt=""><?php
endforeach;
endif;
endwhile;
?>
I am creating a new template that will get all the custom post type (Case Studies) content, including the taxonomies values associated with it.
So far I got the following:
<section>
<h1><?php _e( 'posts', 'casestudies' ); ?></h1>
<?php get_template_part('loop'); ?>
<?php
$args = array('post_type' => 'casestudies', 'posts_per_page' => 3);
$query = new WP_Query($args);
while($query -> have_posts()) : $query -> the_post();
?>
<h2><?php the_title(); ?></h2>
<p>Meta: <?php the_meta(); ?></p>
<p>Excerpt: <?php the_excerpt(); ?></p>
<p>what_to_put_here_to_get_taxonomies_values????</p>
<?php endwhile; ?>
<?php get_template_part('pagination'); ?>
</section>
How do I get the taxonomy of it? I have tried multiple things but all seemed to fail and just getting more confused.
Check this function: wp_get_post_terms()
Assuming your custom post type Case Study supports two taxonomies called country and subject, you can try something like this:
<?php $terms = wp_get_post_terms( $query->post->ID, array( 'country', 'subject' ) ); ?>
<?php foreach ( $terms as $term ) : ?>
<p><?php echo $term->taxonomy; ?>: <?php echo $term->name; ?></p>
<?php endforeach; ?>
Your output would be something like:
Country: United Kingdom
Subject: Biology
Subject: Chemistry
Subject: Neurology
assume: I register a taxonomy with custom post type name publication_category.
On your custom post type template write :
$terms = get_the_terms( $post->ID, 'publication_category' );
if ($terms) {
foreach($terms as $term) {
echo $term->name;
}
}
Have you tried using <?php get_taxonomies() ?> ?
If your looking for specific taxonomies that function has optional arguments you can pass in to control the output. See documentation here : http://codex.wordpress.org/Function_Reference/get_taxonomies
Just in case if it could help someone, I used "the_taxonomies()" function inside a loop of a custom post type.
<?php
while ( have_posts() ) : the_post();
$custom_post = get_post_meta( get_the_ID() );
//
?>
//html
//and stuff
<?php the_taxonomies(); ?>
<?php
endwhile;
?>
the result was:
Taxonomy-name: {Taxonomy-term}. <-- as a link
I am using the following code to load all the posts from a custom post type, in this loop I show a title but I also want to show the categories (terms) that are connected to this particular post but I can't seem to make it work
Loop:
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I tried:
<?php echo $term->name; ?>
You need to use get_the_terms() for getting category
you can get this by below code...
you need to put second argument a custom post-type's category slug
you can refer this link https://developer.wordpress.org/reference/functions/get_the_terms/
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
<?php
$terms = get_the_terms( get_the_ID(), 'category-slug' ); // second argument is category slug of custom post-type
if(!empty($terms)){
foreach($terms as $term){
echo $term->name.'<br>';
}
}
?>
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I have added tags to my Custom Post Type.
Now I want to use them to create a isotope portfolio, I can load all tags with this code:
<?php $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 24;
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="all <?php echo $tag->slug; ?>">
<?php echo the_post_thumbnail(); ?>
<p><?php the_title(); ?></p>
</div>
<?php endwhile; ?>
But now I want to add the all tags that from each portfolio item to the class="".
With <div class="<?php $tag->slug; ?>"> I just get the last tag of all the tags that are used.
I know there are already a lot of posts about this problem, but every post I have found does not seem to work for me.
It now works with the following code:
<?php $tags = get_the_tags();
$tag = wp_list_pluck( $tags, 'slug' );
$tagToClass = implode(" ", $tag);
?>
And then use <?php echo $tagToClass ?>
I am having a problem with custom post type and custom taxonomies. I created a custom post type called 'galleries' and taxonomies named 'gallery_type'. And I have created a page called Gallery in which i loaded all the posts from galleries. And then I have created 'garden' & 'house' taxonomy terms and assigned 3-3 posts to each 'garden' & 'house'. And I linked the taxonomy terms 'garden' and 'house' as sub-menu to gallery which is my current parent. I want to display the posts in their respective terms. But it is not loading. What can I do ??
My code is: '
$tax = 'gallery_type';
$terms = get_the_terms($post->ID, $tax);
if($terms){
foreach($terms as $term){
$single = $term->name;
?>
<?php
$args = array('post_type'=>'galleries',
'posts_per_page'=> -1,
'tax_query'=>array(
array('taxonomy'=>'gallery_type',
'terms'=> $single)
),
'order_by'=>'post_date');
$query = new WP_Query($args);
if($query->have_posts()){
while($query->have_posts()):
$query->the_post();
$post_id = $post->ID;
?>
<li class="thumb <?php echo $post_id; ?>" onclick="changeContent(<?php echo $post_id; ?>);">
<?php if(has_post_thumbnail())
the_post_thumbnail('gallery-thumb');
?>
<?php
endwhile;
wp_reset_query();
}
else{
_e('No image found in gallery');
}
'
Any fix???
Try using the slug instead on the name of the taxonomy term.
$tax = 'gallery_type';
$terms = get_the_terms($post->ID, $tax);
if($terms){
foreach($terms as $term){
$single = $term->slug;
// Also instead of this query for the taxonomy, as you are on the taxonomy page, you should use $single = $wp_query->query_vars['taxonomy_name']; <- This is the slug of the current viewed taxonomy
?>
<?php
$args = array('post_type'=>'galleries',
'posts_per_page'=> -1,
'tax_query'=>array(
array('taxonomy'=>'gallery_type','field' => 'slug',
'terms'=> $single)
),
'order_by'=>'post_date');
$query = new WP_Query($args);
if($query->have_posts()){
while($query->have_posts()):
$query->the_post();
$post_id = $post->ID;
?>
<li class="thumb <?php echo $post_id; ?>" onclick="changeContent(<?php echo $post_id; ?>);">
<?php if(has_post_thumbnail())
the_post_thumbnail('gallery-thumb');
?>
<?php
endwhile;
wp_reset_query();
}
else{
_e('No image found in gallery');
}