After going several google searches i've accomplish this code (Not too good on PHP)
<div>
<?php
$args = array(
'post_type' => 'post'
);
$categories = get_categories( $args );
$catlinks = get_category_link( $categories);
foreach ( $categories as $category ) {
echo ' <h2>' . $category->name .'</h2>';
$args['category'] = $category->term_id;
} ?>
</div>
This code displays a loop of Wordpress Post Categories, im trying to get each category link, but i'm still not getting the right link.
Any help in advance would be great.
Thanks Rodrigo
You had it pretty close.
You'll want to run get_category_link() against the ID of the $category in your foreach loop.
That looks like this:
<?php
foreach ( $categories as $category ) {
echo ' <h2>' . $category->name . '</h2>';
}
?>
So, all together, your whole code should read:
<div>
<?php
$args = array(
'post_type' => 'post'
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo ' <h2>' . $category->name . '</h2>';
}
?>
</div>
Use get_permalink function. See the reference on Wordpress site
Related
I'm trying to display in the sidebar the current page's category and it's subcategories. The title should be the current category's name and linked to the current category as well. An example of what I'm trying to achieve can be seen here in the sidebar: https://food52.com/shop/pantry
This is my current site as an example:https://farmtofrank.wpengine.com/product-category/prepared-foods/
This is the code I've created so far:
<?php
$terms = get_terms([
'taxonomy' => get_queried_object()->taxonomy,
'parent' => get_queried_object_id(),
]);
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
echo '<div>';
foreach ( $terms as $term) {
echo '<p class="filters">' . $term->name . '</p>';
}
echo '</div>';
?>
It works but it puts the parent link at the bottom of the list. How can I keep the parent link at the top above the subcategories?
I suppose that this is related to product category archive pages. In this case, you are very near, try:
$queried_object = get_queried_object();
if ( is_product_category() && is_a($queried_object, 'WP_Term') ) {
$taxonomy = $queried_object->taxonomy;
echo '<h2 class="shop__sidebar-heading">
' . $queried_object->name . '
</h2>';
$children_terms = get_terms(['taxonomy' => $taxonomy, 'parent' => $queried_object->term_id]);
if ( ! empty($children_terms) ) {
echo '<ul class="'.$queried_object->slug.' child-terms">';
// Loop through children terms
foreach ( $children_terms as $term) {
echo '<li class="filters">' . $term->name . '</li>';
}
echo '</ul>';
}
}
Tested and works. It will require some styling (CSS).
I'm wondering: is there any way I can load all the categories and store each one in a different array?
So i've got this code:
<?php
$cat_args = array('orderby' => 'name','order' => 'ASC');
$categories = get_categories($cat_args);
foreach($categories as $category) {
$args = array(
'showposts' => -1,
'category__in' => array($category->term_id),
'caller_get_posts' => 1
);
$posts = get_posts($args);
if ($posts) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post);
?> <p><?php the_title(); ?></p> <?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>
I want to make it so that I can use it later like that (outside the loop):
<?php echo $category[0] -> name . $category[1] -> name . $category[2] -> name; ?>
If you just need an array of category names, you can alter your code as follows; (PLEASE NOTE:caller_get_posts and showposts has been depreciated a couple of years ago. They where replaced by ignore_sticky_posts and posts_per_page respectively)
<?php
$cat_args = array('orderby' => 'name','order' => 'ASC');
$categories = get_categories($cat_args);
$category_names = array();
foreach($categories as $category) {
$category_names[] = $category->name;
$args = array(
'posts_per_page' => -1,
'category__in' => array($category->term_id),
'ignore_sticky_posts' => 1
);
$posts = get_posts($args);
if ($posts) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post);
?> <p><?php the_title(); ?></p> <?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>
$category_names will now hold an array of category names.
If you need to display a list of categories with posts below them, you should check out my post here on WPSE. Your method is very resource intensive and slow. My method is superfast, use transients, and at optimum does only 2 db queries in under 0.002 seconds
Note: You already have the data in this format in $categories variable.
So you can use anywhere like this
<?php echo $categories[0] -> name . $categories[1] -> name . $categories[2] -> name; ?>
I'm trying to get a category and loop through its sub categories getting one post from each of those sub-categories. Below is my code:
<?
$homepage_cat = get_category_by_slug( 'home-page-slider' );
$id = $homepage_cat->cat_ID;
print($id);
$sub_cat = get_categories('hide_empty=0&child_of=' . $id);
print_r($sub_cat);
foreach ($sub_cat as $key => $cat)
{
echo $cat->term_id;
query_posts('cat=' . $cat->term_id);
if ( have_posts() )
{ echo '<h1> HELL YEAH </h1>';
while ( have_posts() )
{
echo '<h1>' get_the_title(); '</h1>';
} // end while
} // end if
} //end foreach
?>
The code is not returing any posts as HELL YEAH is not being echoed. Can anyone suggest a solution?
Use get_posts() not query_posts, it is better for these kinds of situations.
$args = array('posts_per_page' => 1, 'category' => $cat->term_id);
$posts = get_posts($args);
foreach($posts as $post) : setup_postdata( $post ) ?>
<h1><?php get_the_title(); ?></h1>
<?php endforeach; ?>
There are quite a few problems here
First of all, never use query_posts to construct custom queries. It breaks the main query, is unreliable and outright fails in most cases in pagination
Secondly, you have to reset your postdata after every custom query
Thirdly, never use short tags. Always use full tag ie <?php and not just <?
Lastly, you are missing the_post() which should return post objects
Your query should look something like this
<?php
$homepage_cat = get_category_by_slug( 'home-page-slider' );
$id = $homepage_cat->cat_ID;
print($id);
$sub_cat = get_categories('posts_per_page=1&hide_empty=0&child_of=' . $id);
print_r($sub_cat);
foreach ($sub_cat as $key => $cat)
{
echo $cat->cat_ID;
$q = new WP_Query('cat=' . $cat->cat_ID);
if ( $q->have_posts() )
{ echo '<h1> HELL YEAH </h1>';
while ( $q->have_posts() )
{
$q->the_post();
echo '<h1>' get_the_title(); '</h1>';
} // end while
} // end if
wp_reset_postdata();
} //end foreach
?>
Replace this
$post_args = array(
'showposts' => 1,
'cat' => $cat->term_id
);
with this.
$post_args = array(
'posts_per_page' => 1,
'category' => $cat->term_id
);
I hope it'll work.
Either this is harder than it needs to be or I am just not understanding WordPress/PHP very well :( All I want to do is show the child/sub categories of a specific parent category...but only if the post is in those subcategories. Specific example:
I am building a wine reviews website and these are the categories:
BrandSubcategory 1Subcategory 2etc.
RegionSubcategory 1Subcategory 2etc.
GrapeSubcategory 1Subcategory 2etc.
The parent categories will never change, and every post will have at least 1 subcategory selected under each parent, so in the LOOP I can just list the parents by name. But I am needing to dynamically output the subcategories, something like this:
Brand: <?php list_post_subcategories('brand'); ?>
Region: <?php list_post_subcategories('region'); ?>
Grape: <?php list_post_subcategories('grape'); ?>
Is there any easy way like this? It seems like this should be a basic function in Wordpress? I've looked at the functions 'get_categories' and 'in_category' but they don't seem to be able to do this.
<?php $post_child_cat = array();
foreach((get_the_category()) as $cats) {
$args = array( 'child_of' => $cats->cat_ID );
$categories = get_categories( $args );
if( $categories ) foreach( $categories as $category ) {
echo $category->cat_name; }
} ?>
try this
I posted to Wordpress Answers to get more help and #Milo gave a great code solution:
// get top level terms
$parents = get_terms( 'category', array( 'parent' => 0 ) );
// get post categories
$categories = get_the_terms( $post->ID, 'category' );
// output top level cats and their children
foreach( $parents as $parent ):
// output parent name and link
echo '' . $parent->name . ': ';
// initialize array to hold child links
$links = array();
foreach( $categories as $category ):
if( $parent->term_id == $category->parent ):
// put link in array
$links[] = '' . $category->name . '';
endif;
endforeach;
// join and output links with separator
echo join( ', ', $links );
endforeach;
You can use array_map so you can return only the categories that you want. For example:
array_map( function( $cat ) {
if ( $cat->parent != 0 ) {
return $cat;
}
},
get_the_category()
);
I want to accomplish a nice portfolio listing by the help of the http://mixitup.io/ jquery plugin.
So I created a custom post type, 'portfolio' and in the archive-portfolio.php I created the filters by listing the taxonomy terms of 'portfolio'
<?php $terms = get_terms("portfolio_categories");
$count = count($terms);
if ( $count > 0 ){
echo '<ul class="list-group">';
foreach ( $terms as $term ) {
echo '<li class="list-group-item" data-filter="' . $term->slug . '">' . $term->name . '<span class="badge"> ' . $term->count . '</span></li>';
}
echo '</ul>';
}
?>
So in the first instance I need to get the post count of each term and replace it with what I used and didn't function: $term->count
After creating the filters I need to create the grid so I made a loop to get all the posts in the custom post 'portfolio'
<?php $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 99 );
$loop = new WP_Query( $args );
$post_number = 0; ?>
<ul id="portfolio-grid">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class=""><?php echo the_title() ?> </li>
<?php endwhile; ?>
</ul>
Here I need to assign the category slug of the term in the empty class quotes of the li so that I can make the plugin work properly.
Any chance you can help me with that?