I have a little script that display any categories that contain posts and displays them (like a little menu)
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
echo '<a class="blog-panel-cat-menu bg-color-1" href="' . get_permalink( get_option( 'page_for_posts' ) ) . '">All</a>';
foreach( $categories as $category ) {
if ($category->count > 0){
$category_link = sprintf(
'<a class="blog-panel-cat-menu bg-color-1" href="%1$s" alt="%2$s">%3$s</a>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name )
);
echo $category_link;
}
}
?>
When clicking on any link it will redirect to a page that show the posts for that category eg mywebsite/news/category/blog/ or mywebsite/news/category/news/ (mywebsite/news being the blog homepage which displays all categories)
When I'm on mywebsite/news/category/blog/ I want the "blog" link in the menu to have the class blog-cat-focus after the current class in the foreach loop like this <a class="blog-panel-cat-menu bg-color-1 blog-cat-focus" href="%1$s" alt="%2$s">%3$s</a>
Try this code. Added two more lines to check current category.
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
$category = get_category( get_query_var( 'cat' ) );
echo $cat_id = $category->cat_ID;
echo '<a class="blog-panel-cat-menu bg-color-1" href="' . get_permalink( get_option( 'page_for_posts' ) ) . '">All</a>';
foreach( $categories as $category ) {
if ($category->count > 0){
$cust_class = '';
if($category->term_id==$cat_id){$cust_class = 'blog-cat-focus';}
$category_link = sprintf(
'<a class="blog-panel-cat-menu bg-color-1 %4$s>" href="%1$s" alt="%2$s">%3$s</a>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name ),
$cust_class
);
echo $category_link;
}
}
?>
Related
So I'm using wordpress and ACF. I created a CPT called products and a custom taxonomy named product_types. I have a loop that displays all product_type sections and each section contains products tied to it. So I'm looking for ways to easily filter new products or popular products thats why I come up with adding a dedicated category for it but I dont want it to exclude it from my post loop. pls see img attached
enter image description here
Here's the code I'm working with atm
<?php
$cpt_name = 'products';
$taxonomy_name = 'product_type';
// Retrieve the terms in the above taxonomy.
$terms = get_terms( $taxonomy_name );
if ( empty( $terms ) || is_wp_error( $terms ) ) {
return;
}
echo '<div class="products-group">';
foreach( $terms as $term ) {
echo '<div class="products-category">';
echo '<h2 class="title-category">' . $term->name . '</h2>';
$args = array(
'post_type' => $cpt_name,
'tax_query' => array(
array(
'taxonomy' => $taxonomy_name,
'field' => 'slug',
'terms' => array( $term->slug ),
),
),
'posts_per_page' => -1,
'category__not_in' => array( 30 ),
);
$query = new WP_Query( $args );
echo '<div class="products-list">';
while ( $query->have_posts() ) {
$query->the_post();
$attachment_id = get_field('header_bg_image');
$size = "wide-thumbnail"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
if ( ! empty( $image ) ) {
$image_html = esc_url( $image[0] );
} else {
$image_html = 'https://rasons.ltd/wp-content/uploads/2020/02/interior-exterior-finish-800x400.jpg';
}
printf( '%s', get_the_permalink(), esc_attr( the_title_attribute( 'echo=0' ) ), get_the_title() );
}
echo '</ul>';
wp_reset_query();
echo '</div></div>';
} // end foreach.
echo '</div>';
?>
I need to get all the categories in indexed form according to the alphabet. I'm attaching picture output image of that i need. . is there someone that can help me out?
Advance Thanks
Try this:
/**
* This code should be added to functions.php of your theme
**/
add_filter('init', 'custom_default_catalog_orderby');
function custom_default_catalog_orderby() {
$result = array();
$categories = get_categories( array(
'hide_empty' => 0,
'orderby' => 'name',
'order' => 'ASC'
) );
// Store all category alphabet wise in $result array
foreach( $categories as $cat ) {
$first_char = mb_substr($cat->name, 0, 1);
$result[$first_char][] = $cat;
}
// Print all categories alphabetically
foreach($result as $alphabet => $c ) {
echo "<h3>". ucfirst($alphabet) . "</h3>";
// You can change HTML structure accordingly and manage like echo or return
foreach($c as $category ) {
$category_link = sprintf(
'%3$s',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name )
);
echo '<p>' . sprintf( esc_html__( '%s', 'textdomain' ), $category_link ) . '( '
. sprintf( esc_html__( 'Post Count: %s', 'textdomain' ), $category->count ) . ')</p>';
}
}
}
You can change action and HTML structure accordingly.
I've had trouble getting the correct date of each post to display. It is displaying the date of the first entry even though I have used 'new WP_Query'.
I also can't get the category type to display.
<?php
$blogloop = new WP_Query( array( 'numberposts' => '3' ) );
$recent_posts = wp_get_recent_posts( $blogloop );
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
foreach( $recent_posts as $recent ){
$category_link = sprintf(
'%3$s',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name )
);
echo ' <div class="col-md-4 col-xs-12">
<div class="blog-item"><div class="home-blog-img" style="background-image: url('.get_the_post_thumbnail_url($recent["ID"]).')"></div><div class="home-blog-content"><h3>'. $recent["post_title"] .'</h3><p>'. get_the_date('D M j',$recent) .'</p><span> | </span><p>'. sprintf( esc_html__( 'Category: %s', 'textdomain' ), $category_link ) .'</p></div></div></div> ';
}
?>
<?php wp_reset_query();?>
You have missed "ID" in
get_the_date('D M j',$recent),
Please change it to
get_the_date('D M j',$recent["ID"]),
so it will display each posts dates
I'm trying to get all the tags that are within my custom post type "resource".
The problem is that I'm outside of the loop and struggling to find a way to get the functionality to work with the custom post type.
I have the category setup also as "resource_category"
My current code is:
$tax = 'post_tag';
$terms = get_terms( $tax );
$count = count( $terms );
if ( $count > 0 ): ?>
<div class="post-tags">
<?php
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $tax );
echo '' . $term->name . ' ';
} ?>
</div>
<?php endif;
Can anyone help?
you are asking for the tags right, because the answer from 2015 is listing categories not tags
$args = array(
'type' => get_post_type(),
'orderby' => 'name',
'order' => 'ASC'
);
$tags = get_tags($args);
foreach($tags as $tag) {
var_dump($tag->name);
}
Tags are also WordPress taxonomy. So, you can get all the tags as like you get all terms
Read More
$tags = get_terms([
'taxonomy' => 'YOUR_CUSTOM_TAXONOMY',
'hide_empty' => false
]);
var_dump($tags);
You can also copy custom post taxonomy from Tags page URL.
http://localhost/wp-admin/edit-tags.php?taxonomy=YOUR_CUSTOM_POST_TAG_TAXONOMY_NAME&post_type=custom-post-type
$args = array(
'type' => 'resource',
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
}
I am able to list the taxonomy children:
<?php
$taxonomy = 'store_name';
$tax_terms = get_terms($taxonomy);
?>
<ul class="split-list2">
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
</ul> ?>
And I found a plugin to make thumbnails for categories/taxonomies:
https://wordpress.org/plugins/taxonomy-images/
But I can't figure out how to add the code to include the thumbnails to the taxonomy category grids.
Does anyone know how to do this
Read the plugin doc perfactly it gives what you want
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'after' => '</div>',
'after_image' => '</span>',
'before' => '<div class="my-custom-class-name">',
'before_image' => '<span>',
'image_size' => 'detail',
'post_id' => 1234,
'taxonomy' => 'post_tag',
) );
Second parameter in apply_filters( 'taxonomy-images-get-terms', '' ); is array as show above
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
print '<ul>';
foreach( (array) $terms as $term ) {
print '<li><a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' ) . '</li>';
}
print '</ul>';
}