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; ?>
Related
I try to do a shortcode to get all posts by following this link
My shortcode code put in child theme's functions.php file.:-
function myprefix_custom_grid_shortcode( $atts ) {
$atts = shortcode_atts( array(
'posts_per_page' => '-1',
'term' => '',
), $atts, 'myprefix_custom_grid' );
extract( $atts );
$output = '';
$query_args = array(
'post_type' => 'post', // Change this to the type of post you want to show
'posts_per_page' => $posts_per_page,
);
if ( $term ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'ID',
'terms' => $term,
),
);
}
// Query posts
$custom_query = new WP_Query( $query_args );
if ( $custom_query->have_posts() ) {
$output .= '<ul class="yj-trainingcamp-list clearfix">';
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
$categories = get_the_category();
$separator = ' ';
$output_cats = '';
if($categories){
foreach($categories as $category) {
$output_cats .= ''.$category->cat_name.''.$separator;
}
}
$output .= '<li class="yj-trainingcamp-item">
<a href="' . get_permalink() . '" title="' . get_the_title() . '" class="trainingcamp-atom-wrap" target="_blank">
<div class="cover-img-zone">
<div class="el-image cover-img"><img src="' .get_the_post_thumbnail_url($post->ID, 'full'). '" alt="" class="el-image__inner" style="object-fit: cover;"></div></div><div class="infos"><div class="camp-info"><div class="camp-name"><span class="camp-num">第10期</span>' . get_the_title() . '</div>
<div class="nums"><div class="sale-count">' .get_the_excerpt(). ' ' . trim($output_cats, $separator) . ' </div>
</div>
</div>
<div class="teacher-info">
<div class="teacher-img-zone">
<div class="el-image teacher-img">' . get_avatar( get_the_author_meta('user_email') , 32 ) . '
</div>
</div>
<div class="teacher-name">作者: ' .get_the_author(). ' | ' .get_the_modified_time('M j, Y'). '</div>
</div>
</div>
</a>
</li>';
}
$output .= '</ul>';
wp_reset_postdata();
}
return $output;
}
add_shortcode( 'myprefix_custom_grid', 'myprefix_custom_grid_shortcode' );
Here's the issue showing multiple div
wanna display post's categories'name and categories'link ( foreach ) in each post but doesn't works...
Below is the part I doubt...
$categories = get_the_category();
$separator = ' ';
$output_cats = '';
if($categories){
foreach($categories as $category) {
$output_cats .= ''.$category->cat_name.''.$separator;
}
}
not really sure right now, because I cannot debug it and I am not able to read the text on the picture but as described in the wordpress the documentation, you can set the post id for the function:
get_the_category( int $id = false )
A small example from the page:
Get the Post Categories From Outside the Loop
Therefore, I would suggest, that you can add the post ID to get_the_category like this:
$categories = get_the_category(get_the_ID());
$separator = ' ';
$output_cats = '';
if($categories){
foreach($categories as $category) {
$output_cats .= ''.$category->cat_name.''.$separator;
}
}
Otherwise, I guess, the foreach loop adds all categories (name and links) to each post because you are interating over all published categories.
I am busy to build a row that will show randomly 4 categories with the category title and the 3 latest posts under it. But i am stuck how i can get the categories in random order because the 'orderby' is not working...
Can somewone help me with that?
The code i am using:
<?php
//for each category, show all posts
$cat_args = array(
'orderby' => 'rand',
'order' => 'ASC'
);
$limit = 4;
$counter = 0;
$categories = get_categories($cat_args);
foreach ($categories as $category):
if ($counter < $limit) {
$args = array(
'showposts' => 3,
'category__in' => array(
$category->term_id
),
'caller_get_posts' => 1
);
$posts = get_posts($args);
if ($posts) {
echo '<h3><a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name . '</a> </h3>';
foreach ($posts as $post) {
setup_postdata($post);
?>
<p><?php the_title(); ?></p>
<?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>
<?php
$counter++;
endforeach;
?>
Remove below code. This will only work with posts.
$cat_args = array(
'orderby' => 'rand',
'order' => 'ASC'
);
Use the code as below:
$categories = get_categories();
shuffle ($categories); // Just add shuffle and you will get random categories.
you can use this for get random cat :
$categories = get_the_category($my_query->post->ID);
$catCount = count($categories);
//select a random category id
$id = rand(0,$catCount-1);
//cat id
$catId = $categories[$id]->term_id;
and replace to lines like
if(is_array($cat)) {
$categories = get_the_category($my_query->post->ID);
$catCount = count($categories);
//select a random category id
$id = rand(0,$catCount-1);
//cat id
$catId = $categories[$id]->term_id;
} else {
$catId = $cat;
}
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
I want to display all terms with there image of particular taxonomy. i got all term details using get_term() function.
<?php
$terms = get_terms( 'vehicle_type' );
foreach ($terms as $term) :
echo $term->slug;
$colors = apply_filters( 'taxonomy-images-get-terms', '', array(
'taxonomy' => 'vehicle_type',
'term_args' => array(
'slug' => $term->slug,
)
)
);
foreach( (array) $colors as $color) :
echo wp_get_attachment_image( $color->image_id, 'full', array('class' => 'alignnone'));
//echo $term->name;
endforeach;
endforeach;
?>
But it is showing same path for all images.
http://localhost/mototrader/wp-includes/images/media/default.png
How could i get actual path of image associated to that taxonomy.
Thanks in advance.
You can try by this way also
<?php
$cat_id = get_query_var('cat');
$catlist = get_categories('hide_empty=0&child_of=' . $cat_id);
echo "<ul>";
foreach($catlist as $categories_item)
{
echo '<h1><a href="' . get_category_link( $categories_item->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories_item->name ) . '" ' . '>' . $categories_item->name.'</a> </h1> ';
echo '<div class="categoryoverview clearfix">';
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
foreach( (array) $terms as $term ) {
if($term->term_id == $categories_item->term_id) {
print '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'thumbnail' );
echo '</a>';
}
}
echo '<p>'. $categories_item->description; echo '</p>';
}
echo '</div>';
}
echo "</ul>";
You can Add Advance custom field
<< using that create image field.
<< Assign that image each taxonmoy
<< you can easily get image of corresponding taxonmy image.
Refer this >> https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
I am using get_categories to show posts in a category in my frontpage, but I want to order categories as i defined,Like:
$cat_order = 5,2,4,1,3 //order by category ID
or
$cat_order = cat5,cat2,cat4,cat1,cat3 // order by cat name
below is my current code which order cat by name, ASC
<?php
//for each category, show all posts
$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); ?>
<article >
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</article>
<?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>
There's no way to sort this within the query itself. You'll have to sort it yourself once you get the results back. To sort an array by custom criteria, use the usort function. Here's an example:
$categories = get_categories();
$cat_order = array(5, 2, 4, 1, 3);
usort($categories, function ($a, $b) {
return ((int) array_search($a->term_id , $cat_order)) - ((int) array_search($b->term_id , $cat_order));
});
If you're sure that all the categories are in your $cat_order array, you can forgo the integer casting.
P.S. If you're using PHP < 5.2, use a named function instead:
function sort_categories ($a, $b) {
return ((int) array_search($a->term_id , $cat_order)) - ((int) array_search($b->term_id , $cat_order));
}
usort($categories, 'sort_categories');