Get the name of the first category - wordpress

I am trying to create a single page that lists the content of each category. I have managed to create the list. I now need to get the name of the category. I have the following code:
<ul>
<li> CATEGORY NAME HERE </li>
<?php query_posts('cat=0'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php echo get_permalink(); ?>">
<?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
How to call the name of the first category (0)?
Current edit: Why won't multiple works?
<div class="first-col">
<ul>
<?php query_posts('cat=0'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<li> <?php $category = get_the_category();
echo $category[0]->cat_name;
?> </li>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
</ul>
</div>
<div class="first-col">
<ul>
<li> <?php $category = get_the_category();
echo $category[0]->cat_name;?> </li>
<?php query_posts('cat=3'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
</ul>
</div>

You have to get the array of categories and echo the first one from the array.
http://codex.wordpress.org/Function_Reference/get_the_category
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>

According to wordpress developers codex:
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo '' . esc_html( $categories[0]->name ) . '';
}
This will give you the first category and also link it to that category's page.

there is also a shortcodes plug in that will help create lists based on categories, terms, etc. http://wordpress.org/plugins/display-posts-shortcode/

Related

Pulling the category ID of the post on the page I am on Wordpress and listing the articles belonging to that category

In a post (single.php) I want to list other posts in the category that post is included in.
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
?>
<ul>
<?php query_posts( 'showposts=5&orderby=date&cat=$cat_id' ); ?>
<?php while (have_posts()) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
Articles in all categories come with this code. However, I only want the articles in the category to which that post belongs to be listed.
I found the solution.
<ul>
<?php
foreach((get_the_category()) as $category) :
$cat= $category->cat_ID
?>
<?php $args = 'cat=' . $cat . '&orderby=date&order=ASC'; ?>
<?php query_posts( $args );?>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php
the_title(); ?></a></li>
<?php endwhile; endforeach; ?>
</ul>
Everything about query_post () is here : https://developer.wordpress.org/reference/functions/query_posts/

Displaying a specific post category in Wordpress

I am trying to display posts from a specific category on my website which currently displays all posts. Here is the code I have on the section of the page below:
<?php
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>18)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Change your code inside the loop to something like below code and here cat = 3 determines the category 3.
<ul>
<?php
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<li>
<h3>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a>
</h3>
<?php the_content(); ?>
</li>
<?php endwhile; ?>
</ul>
pass the argument cat = 3 or category id for which you want to show post

woocommerce retriving category name as div class?

ok I am trying to retrive the category name of a woocommerce product displayed in a wordpress loop and use it as the class for a li also inside the loop i've tried this:
<div id="isocontent" class="products">
<ul><?php while (have_posts()) : the_post(); ?>
<li class="<?php echo $product->get_categories(); ?> box">
<?php echo the_post_thumbnail(); ?>
<p><?php the_title(); ?></p>
<span href="<?php the_permalink(); ?> " class="amount price" data-original="<?php echo get_woocommerce_currency(); ?><?php echo $product->get_price(); ?>" data-price="<?php echo $product->get_price(); ?>" title="Original price: <?php echo $product->get_price(); ?>"><?php echo get_woocommerce_currency(); ?><?php echo $product->get_price(); ?></span>
Add to Cart
</li>
<?php endwhile; ?>
</ul>
</div>
this being the part i'm trying to retrive the class with:
<li class="<?php echo $product->get_categories(); ?> box">
but it just outputs this:
<li class="<a href=" http:="" localhost.no="" fanny="" kategori="" interior-sv="" "="" rel="tag">
which does retrieve the category but also screws with the markup breaking the loop.
I've also tried this:
<li <?php post_class('box'); ?>
but because woocommerce uses taxonmys it retrives the tags but not the product category.
any help is much appriciated
Kind regards
Chris
It's not quite as easy as making a single call - get_categories() is designed to show an HTML representation of the product categories. The product categories are actually a custom taxonomy, so you have to use get_the_terms() to get at it.
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_id = $term->term_id;
$category_name = $term->name;
$category_slug = $term->slug;
break;
}

Loop in Sidebar Done Incorrectly?

I'm trying to add a "Movie Reviews" Section to the sidebar, but it's also altering my main loop. Heres the Code I have:
//Movie All Reviews
function display_movie_reviews( $query ) {
if ( $query->is_category('movies') ) {
$query->set( 'tag', 'movie-reviews' );
}
}
add_action( 'pre_get_posts', 'display_movie_reviews' );
It's displaying the reviews like I want, but the category page at site.com/movies/ is only displaying only the reviews
here is the loop for the sidebar:
<ul>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php add_action( 'pre_get_posts', 'display_movie_reviews' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'sidebar-reviews', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
</ul>
and sidebar-reviews:
<li>
<a class="sidebar-thumb" href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'sidebar-thumb',array('title' => "")); ?></a>
<div class="sidebar-text">
<a class="sidebar-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="sidebar-rating"><!-- rating here --></span>
</div>
</li>
Try wp_reset_query(); http://codex.wordpress.org/Function_Reference/wp_reset_query
This function destroys the previous query used on a custom Loop.
Function should be called after The Loop to ensure conditional tags
work as expected.

WordPress categories and posts priting

What is the syntax for printing the sequence of category name and its follow up posts in my new template in a WordPress site? I have tried a lot via Google but all are not working properly.
There are many ways to do this, but here is a simple one (you will have to improve it with the category and posts links at least):
<?php $categories= get_categories();
if( !empty($categories) ):
?>
<ul>
<?php foreach ($categories as $category): ?>
<li>
<?=$category->cat_name?>
<?php $posts = get_posts($category->cat_id);
if( !empty($posts) ):
?>
<ul>
<?php foreach( $posts as $post ): ?>
<li><?= $post->post_title; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Resources