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
Related
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 created a new template page and I am displaying custom post type in that page as follows,
<div class="col-sm-4">
<?php $i = 1 ?>
<?php $posts = get_posts(array(
'post_type' => 'astroalbums',
'posts_per_page' => -1
));
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 1): ?>
<?php $link = get_permalink($post->ID); ?>
<?php the_title( '<h3 class="entry-title">', '</h3>' );?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
<?php if($i == 3){$i = 1;} else {$i++;} ?>
<?php endforeach; ?>
My custom post type is "astroalbums" and I want to use it dynamically. I have 4 custom post types. I want to create new page in dashboard and assign the above page template i have created. and each page will call different custom post type.
It will be really great help
Thank you,
Trupti
You're retrieving the posts correctly, but it's seems that the problem is inside the foreach loop. As the default WordPress loop is not being used, you need to call functions which receive the post id as parameter or use the properties present in the $post object (which is an instance of the WP_POST class) in order to display the data.
One possible solution:
<?php
$posts = get_posts([
'post_type' => 'astroalbums',
'posts_per_page' => 1
]);
?>
<?php foreach( $posts as $post ): ?>
<?php $link = get_permalink( $post->ID ); ?>
<h3 class="entry-title">
<a href="<?php echo esc_url( $link ); ?>" rel="bookmark">
<?php echo get_the_title( $post->ID ); ?>
</a>
</h3>
<a href="<?php echo esc_url( $link ); ?>">
<?php echo get_the_post_thumbnail( $post->ID ); ?>
</a>
<?php endforeach; ?>
How to get all posts from one category. i tried this code , it's not showing any output.Is it correct or any correction is here? Thanks.
include('wp-config.php');
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result = array(
"id"=>$args['ID'],
"type"=>$args['post_type'],
"url"=>$args['guid']);
endforeach;
wp_reset_postdata();
print($result);
Try below :-
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result[] = array(
"id"=>$post->ID, // changed $args to $post
"type"=>$post->post_type,
"url"=>$post->guid);
endforeach;
wp_reset_postdata();
print_r($result);
If you want to display posts from a specific category in the category page, you can use the below given code in your theme's category.php file.
<?php
if(have_posts()) :
while (have_posts()) : the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
?>
If you want to display the same in pages other than category page, just add the following code to the corresponding files.
<?php
$posts = new WP_Query();
$posts->query( "category_name='{enter your category slug here}'&posts_per_page=-1" );
if($posts->have_posts()) :
while ($posts->have_posts()) : $posts->the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
are you just trying to get posts from a category?
This is a handy code from the Codex that I keep around. Use this on any custom category page or anywhere on any page, for that matter, to start the loop. Make sure you put your category slug into the right spot in the code.
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
// YOUR STUFF LIKE the_title(); or the_content();
endwhile; endif;
This is NOT a fix to your code, but it answers the question you asked. I think your problem may be in the use of $args inside the loop (seems odd), but if you want me to make sure I might need more of the code or a working example I can see.
http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category
AHEM...yeah... I'm an idiot... don't go pasting this around. Use WP_Query!! thanks Pieter.
On your theme directory, search category.php. If it doesn't contain, create a new file category.php and paste this code:
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_title();?>
<?php the_excerpt();?>
<?php endwhile; else :?>
<?php endif;?>
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; ?>
I have two different custom post types: "Movies" and "Press" with different custom fields.
Now I want to display both custom fields in the single.php.
home.php:
...
$query = array(
'post_type' => 'movies',
'posts_per_page' => 3,
);
$loop = new WP_Query( $query );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="movie_cover">
<img src="<?php the_field('cover'); ?>" title="<?php echo get_the_title() ?>" />
</div>
...
single.php:
get_header(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'single', 'film' ); ?>
<?php endwhile; // end of the loop. ?>
<?php get_footer(); ?>
single-film.php:
...
<img src="<?php the_field('cover'); ?>" title="<?php echo get_the_title() ?>" />
...
the field 'cover' is from "Movies", but now i want to list the field 'release' from "Press".
Does I need a second loop?
if you add movie_id custom field to press, and set it to the ID of the movie
then you can do something like inside single-film.php
query_posts('meta_key=movie_id&meta_value=' . get_the_ID());
and do another loop for the reviews
while have_posts() ...
There's a lot of additional stuff but this should get you going in the
right direction.
I'd consider using get_posts instead of query_posts to not override wordpress' main query loop, but then you'd have to do your own manual loop:
$posts = get_posts('meta_key=movie_id&meta_value=' . get_the_ID());
foreach ($posts as $p) {
echo $p->post_title;
...
}