I have registered a custom post type "Projects" and also registered a custom taxonomy for that Post Type called "Project Categories". On my home page I have a div in which I would like to list all projects and terms fro the "Project Categories" taxonomy. Currently, I am only able to get the lists of the terms. Can someone tell me why I am unable to get the terms to display. Currently, I have:
<div class="list-container">
<?php
query_posts( array( 'post_type' => 'projects' ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<li><?php the_title(); ?></li>
<?php endwhile; endif; wp_reset_query(); ?>
<?php $taxonomy = 'project_categories';
$tax_terms = get_terms($taxonomy);
?>
<?php foreach ($tax_terms as $cat): ?>
<li><?php $cat; ?></li>
<?php endforeach; ?>
</div><!--end list-container-->
Another question I have is, is it better to include the taxonomies inside or outside of the query_posts loop?
get_terms($taxonomy) returns a array of objects (see get_terms() in WP Codex), so in order to print the name you should use <?php echo $cat->name ?> (and don't forget the echo).
I tried to correct your code. See comments within the code block for details:
<?php
// keep your queries outside the loop for more readable code
query_posts( array( 'post_type' => 'projects' ) );
$taxonomy = 'project_categories';
$tax_terms = get_terms($taxonomy);
?>
<!-- <li> should be enclosed in <ul> or <ol> -->
<ul class="list-container">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; endif; ?>
<?php foreach ($tax_terms as $cat): ?>
<li><?php echo $cat->name; ?></li>
<?php endforeach; ?>
</ul><!--end list-container-->
<?php wp_reset_query(); ?>
Sidenote: Either you use <?php the_permalink(); ?> or you use <?php the_title(); ?>. The former will do all the magic automatically, and is recommended in this case.
Related
I have a movie list to display in a page. Each movie is a post-object (which I repeat with an ACF repeater).
But in these movies, there is another post-object for the authors.
I can not see the name of the author. Can you help me ?
Here is my code
<?php while ( have_rows('sc_movies') ) : the_row(); ?>
<?php $post_object = get_sub_field('sc_movies_movie'); ?>
<?php if($post_object): ?>
<?php $post = $post_object; setup_postdata( $post ); ?>
<article class="movie">
<div class="movie__content">
<h3 class="movie__title"><?= the_title(); ?></h3>
<?php $post_object = get_field('film_author'); ?>
<?php if( $post_object ): ?>
<?php $post = $post_object; setup_postdata( $post ); ?>
<span class="movie__director">Par <?= the_title() ;?> </span>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</article>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
please make sure if your "sc_movies" repeater has "sc_movies_movie" post object with "Select multiple values? = false " and also in movie post "film_author" post object is "Select multiple values? = false" then your code is correct. If you still gettting issue try add "wp_reset_query()" before while loop because may be some other post object is conflict in page.
I am using the relationship field within a custom post type, but my content is not showing up. I have doubled checked against the documentation but I cant see why it wouldn't be working.
Any help would be great!
Documentation here: http://www.advancedcustomfields.com/resources/relationship/
My html is:
<?php
// The Query
$args = array(
'post_type' => 'top-car'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$posts = get_field('top-cars');
if( $posts ): ?>
<ul id="recom">
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<div class="recom-single">
<h2><?php the_field( 'model' ); ?></h2>
</div>
<!--Close Recom Single-->
</li>
<!--Close First Car-->
<?php endforeach; ?>
</ul>
<!--Close Slider-->
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, we have no cars available at this time, please contact us.'); ?></p>
<?php endif; ?>
Hey any one know how to add a specific category posts in wordpress single post.m giving a link of website that is using it http://www.animetv.org/anime/one-piece-dub/ it is a post that contains posts of a particular category.It is demanded as same i.e. with post thumbnails.Thanks.
Just put this code at your single.php file you will get your desired result:
<?php $category = get_the_category(); ?>
<ul>
<?php
$args = array('category' => $category[0]->term_id );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_post_thumbnail();?><br/>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
And put css for ul li as per your choice like li{float : left;}
If you want to display the posts from a specific category in single.php page, please add the following code to the the file.
<?php
$posts = new WP_Query();
$posts->query( "category_name='{enter your category slug here}'&posts_per_page={enter the number of posts to display here}" );
if($posts->have_posts()) :
while ($posts->have_posts()) : $posts->the_post();
?>
<div>
<?php the_post_thumbnail(); ?>
</div>
<div>
<?php the_title(); ?>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
I have the following code, and basically want it to display the term's slug in the data-type="HERE" part. This is on a static html page outside of the wordpress installation.
I have it displaying the custom post types as a list, but can't get it to display the terms from the taxonomy of 'categories'.
<?php $args = array( 'post_type' => 'case_study' ); ?>
<?php require($_SERVER['DOCUMENT_ROOT'] . '/news/wp-load.php'); query_posts($args ); if (have_posts()) : while (have_posts()) : the_post(); ?>
<li data-id="id-<?php the_ID(); ?>" data-type="DISPLAY TAXONOMY OF CATERGORIES TERMS HERE">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail('case-study-thumb'); ?>
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
<?php else: ?>
<li>No Case Studies found</li>
<?php endif; ?>
Any help would be greatly appreciated!
Many thanks
You can simply use get_the_category, for example :
$data_type = '';
$categories= get_the_category();
if (is_array($categories)) foreach($categories as $cat) {
$data_type .= ', '.$cat->cat_name;
}
I discovered an answer on the forum, works a charm!
<?php $terms = get_the_terms( $post->ID , 'categories' ); foreach( $terms as $term ) { print $term->slug; unset($term); }?>
Where 'print $term->slug;' is, the slug can be changed to name to print the name of the taxonomy term :-)
I have a custom template I am using for my page and in that custom template I get the_content() then I use a custom query. I want to be able to only get 6 of my posts then be able to go to prev and next to see the rest of the posts.
Here is some of my code:
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry-content">
<div id="the_content">
<?php the_content(); ?>
</div>
<?php
$args = array('numberposts'=> 6,
'post_type'=>'project'
);
$posts = get_posts( $args );
if($posts){
foreach( $posts as $post ) : setup_postdata($post);
//do stuff
wp_reset_postdata();
endforeach;
endwhile;
</div>
</article>
?>
Try this
<?php posts_nav_link();
Reference: http://codex.wordpress.org/Next_and_Previous_Links