loop to display taxonomy terms and description - php

So I am attempting to build the first page in my navigation heirarchy to choose product categories.
I have a custom taxonomy called collections. I would like to create a page that loops through all terms in the collections taxonomy and displays the Term +link and description.
I created a file called taxonomy-collections.php and put the following code in. But it is not doing the trick.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'collections' );
echo '<ul>';
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
// We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
echo term_description($post->ID,$term);
}
echo '</ul>';
?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
This is almost straight from the codex so I'm guessing I am missing something.
currently the code has each term displayed as a list, but I do want to change it to a grid format if someone could help with that too.
So each term and description will be wrapped in a div and right aligned with the next.
Thanks

Like this?
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'collections' );
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
// We successfully got a link. Print it out.
echo '<div>' . $term->name . '';
echo term_description($post->ID,$term).'</div>';
}
?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Related

Wordpress Portfolio changes using PHP

I have added project-types to my website portfolio https://littleseabear.com/portfolio but it lists all possible tags, and not the tags directly linked to the portfolio.
For example, Cast should only be film, screenplay and work in progress, while When the Boys Come Home should be Radio and Produced.
Here is my code:
$taxonomy = 'jetpack-portfolio-type' ;
$tax_terms = get_terms( 'jetpack-portfolio-type' );
if( is_archive() || is_home() || is_front_page() ){
?>
<div class="post-wrapper">
<?php
infinity_news_post_thumbnail();
}
?>
<div class="post-thumbnail">
<?php
foreach ( $tax_terms as $tax_term ) {
echo '<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a>, ';
}
?>
<div class="article-details <?php if( is_single() ){ echo 'single-article-details'; } ?>">
Also... is there a way to get rid of the commas? Thanks
get_terms retrieves all available tags, not only the ones associated to the current post. Instead, modify the get_terms line at the top like so:
$taxonomy = 'jetpack-portfolio-type' ;
$tax_terms = get_the_terms( get_the_ID(), $taxonomy );
This will collect terms of the $taxonomy associated to the current post.
Edit:
I missed the question about the commas. To totally get rid of those, you can just remove them from the output:
foreach ( $tax_terms as $tax_term ) {
echo '<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a> ';
}
I removed the , right behind the closing </a>, so it will now only be separated by whitespaces. If you want to have a special separator only between items, you can achieve this like so:
$separator = ''; // initialize empty
foreach ( $tax_terms as $tax_term ) {
echo $separator.'<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a>';
if (empty($separator)) {
// Choose a custom separator HERE:
$separator = ', ';
}
}

Get the current post category name inside while loop

I created a custom post type "stm_media_gallery"
And three category inside this custom post type.
I want to display category name associated with each post.
<?php $gallery_query = new WP_Query( array('post_type' =>
'stm_media_gallery', 'posts_per_page' => -1) );
if( $gallery_query->have_posts() ) :
while( $gallery_query->have_posts() ) : $gallery_query->the_post(); ?>
--Display post name and its category name
<?php endif; ?>
<?php endwhile; ?>
You just need to put following code inside loop :
<div>
<?php
foreach((get_the_category()) as $category){
echo $category->name."<br>";
echo category_description($category);
}
?>
</div>
Update in existing code
<?php $gallery_query = new WP_Query(
array('post_type' => 'stm_media_gallery',
'posts_per_page' => -1) );
if( $gallery_query->have_posts() ) :
while( $gallery_query->have_posts() ) : $gallery_query->the_post();
$gallery_category = get_the_category( get_the_ID() );
the_title( '<h3>', '</h3>' );
echo "<br>";
<?php foreach ( $gallery_category as $key => $value) { echo $value->category_nicename; } ?>
<?php endif; ?>
<?php endwhile; ?>
You can use the pre-made WordPress function the_category( $separator, $parents, $post_id ) to print the post categories as links.
Further information on the WordPress Codex: Function Reference: the_category
Edit: Print only the names:
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo esc_html( $categories->name );
}
Put this inside While Loop
global $post;
$postcat = get_the_category( $post->ID );

Get only categories from a specific custom post type

I am trying to get categories from a specific post type : member .
I am using this code
<?php
$taxonomy = 'category';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
But the problem is : when i am adding a category to member then it is also added to post type : post and when deleted it also deleted from both post type.
What can i do now?
try this way this will display categories from specific post and retrieve the terms for a post.
<?php
$postArg = array('post_type'=>'post',
'posts_per_page'=>-1,
'order'=>'desc',
);
$getPost = new wp_query($postArg);
global $post;
if($getPost->have_posts()){
echo '<ul>';
while ( $getPost->have_posts()):$getPost->the_post();
echo "<h2>".$post->post_title."</h2>";
$terms = get_the_terms($post->ID, 'category' );
foreach ($terms as $term) {
echo "<li>".$term_name = $term->name.'</li>';
}
endwhile;
echo '</ul>';
}
?>
Second way
<?php
$category = get_terms('category');//custom category name
foreach ($category as $catVal) {
echo '<h2>'.$catVal->name.'</h2>';
}
?>

Wordpress / PHP: Get value of variable outside the loop

In my WordPress template (that I am developing), I have the header.php and a template part named header-head.php (called in the header.php). At the index.php, I have other template parte named content.php (called in the index.php). I am trying to get a information of the first post of the taxonomy (named feeling) that I created and return the value in the header-head.php.
The content.php code:
$terms = get_the_terms($post->ID, 'feeling');
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'feeling' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
$term_return = 'Feeling ' . $term->name . '';
}
if ( has_term( '', 'feeling' ) AND $count == 0 ) {
$latest_feeling = "<p class='feeling-text'>" . $term_return . "</p>";
$GLOBALS['feeling_post'] = $latest_feeling;
}
The header-head.php code:
$feeling_return = $GLOBALS['feeling_post'];
echo $feeling_return;
You may have noticed the variable $count, it count the number of posts to check for the first post. I did it in the index.php file and then got the value in the content.php file using $GLOBALS, but now I am not getting...
The index.php loop code:
<?php if ( have_posts() ) : ?>
<?php $count = 0; ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $GLOBALS['count_post'] = $count++; ?>
<?php
/* Include the Post-Format-specific template for the content.
*/
get_template_part( 'partials/content', get_post_format() );
?>
<?php endwhile; ?>
<?php sensibus_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'partials/content', 'none' ); ?>
<?php endif; ?>
Can someone help me? What am I doing wrong?

How to display post category inside page.php if else statement?

I have set up a few categories and want to display specific ones based on is_page().
Inside page.php, I've created an if..else statement that checks the page name and prints out the specific category. My problem at the moment is that instead of just the_title being printed out the whole post is being printed.
Where am I going wrong with this?
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php if ( is_page( 'Greywater Recycling' ) ) { ?>
<div class="col">
<?php query_posts( 'category_name=Greywater Recycling&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
</div>
<?php } else if ( is_page( 'Stormwater Management' ) ) { ?>
<div class="col">
<?php query_posts( 'category_name=Stormwater Management&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
</div>
<?php } else if ( is_page( 'Rainwater Harvesting' ) ) { ?>
<div class="col">
<?php query_posts( 'category_name=Rainwater Harvesting&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
</div>
<?php } ?>
<?php endwhile; // end of the loop. ?>
Many problems with your code. For one, is_page does not work inside the loop.
Second, don't mess with query_posts: When should you use WP_Query vs query_posts() vs get_posts()?. Really, forget about it for secondary loops.
Your code can be simplified to the following. In functions.php, we drop one function to get the Category ID by its Name. And another to do a secondary loop using the ID. And then, in page.php a simple call to those functions.
Documentation: Class_Reference/WP_Query.
page.php
Notice that you don't need to open PHP tags at each line, that makes the code dreadly difficult to read.
Use it only to swap between PHP and HTML
<?php
get_template_part( 'content', 'page' );
// This page title
$the_title = get_the_title();
// Search for category with the same name of the page
$category = brsfl_get_category_id( $the_title );
// Category found, go to the secondary loop
if( $category ) {
brsfl_check_print_categories( $category );
}
functions.php
Always prefix your function names with something distinctive to avoid conflicts that may take the site down.
/**
* Get the category ID based on its name
*/
function brsfl_get_category_id( $cat_name )
{
$term = get_term_by( 'name', $cat_name, 'category' );
// term found, return its ID
if( $term ) {
return $term->term_id;
}
// not found
return false;
}
/**
* Print a loop based on a category ID
*/
function brsfl_check_print_categories( $cat_id )
{
$args = array(
'post_type' => 'post',
'cat' => $cat_id,
'posts_per_page' => 5
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() )
{
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
endwhile;
}
else
{
echo 'No posts found...';
}
}
Although I've answered within the proposed scope, I think there are better solutions to this, like:
a shortcode, just adapt the functions.
using Advanced Custom Fields to show a meta box where you can select a very specific category (don't relying in page and category names) and use only the WP_Query function to print it out in the template.
i think it's better to get the category by the slug name.
i've tried your solution, it works fine but in the case of one of my category has a name with special html characters like apostrophes or quotes it doesn't.
here is the piece of code edited from your solution :
in your functions.php
function brsfl_get_category_id($cat_name){
$term = get_term_by( 'slug', $cat_name, 'category' );
if( $term ) {
return $term->term_id;
}
return false;
}
in your page.php
$the_slug = get_post(get_the_ID())->post_name;
$category = brsfl_get_category_id( $the_slug );
if( $category ) {
brsfl_check_print_categories( $category );
}

Categories