On this SO question I have found the following
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'number' => 20 // how many categories
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>›';
}
That's a step but that it isn't creating a hierarchy. I have read get categories in wp codex but I couldn't work it out.
My structure is:
CAT 1
cat 1.1
cat 1.2
CAT 2
cat 2.1
cat 2.2
Tried this but the output is simply a list of items with no hierarchy at all:
public function walk_taxonomy( $type = "checkbox", $args = array() ) {
$args['walker'] = new Search_Filter_Taxonomy_Walker($type, $args['name']);
$output = $argo = array(
'hierarchical' => 1
);
$categories = get_categories($argo);
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>›';
}
if ( $output )
return $output;
}
you have to add this to the args:
'hierarchical' => 1,
in your foreach you have to check if $category->parent is empty or not.. if not empty indent by adding spaces or applying a class to your li like
<li class="indent">
Related
Hey everyone I am attempting to order my categories by most recent post date. This means when I go to add a post and mark it under that category I want the category to show at the top from most recent to old. Also if you have a way to hide a certain category id I would love to know how. I'm new with PHP and would love some help, thanks everyone.
CODE:
<?php
$cat_args = array(
'orderby' => 'date',
'post_type' => 'products',
'order' => 'ASC',
'child_of' => 0,
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<dl>';
echo '<h3 class="category-name">' . $category->name.'</h3>';
$post_args = array(
'numberposts' => -1,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<dd><a class="article" target="_blank" href="<?php the_field('article_link') ?>"><?php the_title(); ?></a><span class="news-source"> - <?php the_field('news_source') ?></span><p class="important"><?php the_field('important') ?></p></dd>
<?php
}
//echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></dd>';
echo '</dl>';
}
?>
First get recent posts and from that get categories after getting categories run loop through categori it.
$args = array( 'numberposts' => '-1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<dl>';
echo '<h3 class="category-name">' . get_the_category_list( ', ', '', $recent["ID"] ).'</h3>';
$post_args = array(
'numberposts' => -1,
'category' => $recent["ID"]
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<dd><a class="article" target="_blank" href="<?php the_field('article_link') ?>"><?php the_title(); ?></a><span class="news-source"> - <?php the_field('news_source') ?></span><p class="important"><?php the_field('important') ?></p></dd>
<?php
}
//echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></dd>';
echo '</dl>';
}
wp_reset_query();
I'm current building a website for a non-profit, and am using an older edition of the Wordpress theme, Savior.
There is a custom post type of Donations, and it allows the admin to set up different "Causes", each of which is a post with a specific ID.
By default, the theme has a custom Visual Composer element (Screenshot 1 - https://i.stack.imgur.com/cmhTu.png) that allows you to feature the most recent "Cause"; the only customization option you have is for the Title of the VC element.
I'm trying to update this custom VC element so that the admin can specify the exact ID for the "Cause" they would like to feature on a page/post vs. only showing the latest "Cause."
I've adjusted the VC mapping for the cause.php template where commented below:
<?php
class STM_VC_Causes {
function __construct() {
add_action( 'init', array( $this, 'integrateWithVC' ) );
add_shortcode( 'stm_causes', array( $this, 'render' ) );
}
public function integrateWithVC() {
if ( function_exists( 'vc_map' ) ) {
vc_map( array(
'name' => __( 'Causes', STM_DOMAIN ),
'base' => 'stm_causes',
'category' => __( 'STM', STM_DOMAIN ),
'params' => array(
array(
'type' => 'textfield',
'class' => '',
'heading' => __( 'Title', STM_DOMAIN ),
'param_name' => 'title',
'value' => __( 'Our Causes', STM_DOMAIN )
),
/** ========================================
* Qing Custom 8-6-2018
* Allow admin to select Cause to feature
======================================== **/
array(
'type' => 'textfield',
'class' => '',
'heading' => __( 'Cause ID', STM_DOMAIN ),
'param_name' => 'id',
'value' => __( '', STM_DOMAIN )
)
)
) );
}
}
public function render( $atts, $content = null ) {
/** ========================================
* Qing Custom 8-6-2018
* Display selected Cause ID
======================================== **/
$title = '';
$id = '';
extract( shortcode_atts( array(
'title' => '',
'id' => ''
), $atts ) );
$fea_cause = $atts['id'];
$donations = new WP_Query( array( 'post_type' => 'donation', 'posts_per_page' => 1, 'post__in' => $fea_cause ) );
$output = '';
$output .= '<ul class="donation-grid first clearfix">';
while ( $donations->have_posts() ) {
$donations->the_post();
$target_amount = ( get_post_meta( get_the_ID(), 'donation_target', true ) == '' ) ? 0 : get_post_meta( get_the_ID(), 'donation_target', true );
$raised_amount = ( get_post_meta( get_the_ID(), 'donation_raised', true ) == '' ) ? 0 : get_post_meta( get_the_ID(), 'donation_raised', true );
$currency = ( get_post_meta( get_the_ID(), 'donation_currency', true ) == '' ) ? '$' : get_post_meta( get_the_ID(), 'donation_currency', true );
$donors = ( get_post_meta( get_the_ID(), 'donation_donors', true ) == '' ) ? 0 : get_post_meta( get_the_ID(), 'donation_donors', true );
$target_amount_percent = ( $raised_amount / $target_amount ) * 100;
$output .= '<li id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">';
$output .= '<div class="donation-thumbnail">';
$output .= '<a href="' . get_the_permalink() . '">';
if ( has_post_thumbnail() ) {
$output .= get_the_post_thumbnail( get_the_ID(), 'thumb-150x150' );
}
$output .= '</a>';
$output .= '</div>';
$output .= '<div class="donation-content">';
$output .= '<h4>' . get_the_title() . '</h4>';
$output .= '<div class="progress_bar"><span style="width: ' . $target_amount_percent . '%;"></span></div>';
$output .= '<div class="donation-stat">';
$output .= '<span><i class="fa fa-child"></i> ' . __( 'Raised', STM_DOMAIN ) . '<br/>' . $currency . $raised_amount . '</span>';
$output .= '<span><i class="fa fa-users"></i> ' . __( 'Donors', STM_DOMAIN ) . '<br/>' . $donors . '</span>';
$output .= '<span><i class="fa fa-thumbs-up"></i> ' . __( 'Goal', STM_DOMAIN ) . '<br/>' . $currency . $target_amount . '</span>';
$output .= '</div>';
$output .= '<div class="donate_now">';
$output .= '' . __( 'DONATE NOW', STM_DOMAIN ) . '';
$output .= '</div>';
$output .= '</div>';
$output .= '</li>';
}
$output .= '</ul>';
wp_reset_query();
return $output;
}
}
if( defined( 'WPB_VC_VERSION' ) ){
new STM_VC_Causes();
}
?>
The custom VC element is showing up correctly for me on the admin backend side of the site (Screenshot 2 - https://i.stack.imgur.com/zKCgs.png), but I cannot figure out how to get the admin-inputted ID to show up on the frontend - no matter what, it still shows the most recent "Cause." Screenshot 3 (https://i.stack.imgur.com/13Qw4.png) is simply an example screenshot of what the individual "Cause" looks like when a page with the custom VC element is pushed live.
I've contacted the support team for the theme, but they only suggested this Post Types Order WP plugin, which only allows you to change the displayed "Cause" across the entire site, instead of allowing you to specify it in a page-by-page/post-by-post basis. I've also scoured Google/StackOverflow and tried various queries in the WP Codex, building out a custom shortcode (the custom VC element itself is a custom shortcode: [stm_causes]), but it just displays the most recent "Cause."
Edit 8/7/18:
I've made several edits to the causes.php template within the Savior theme, but for some reason, the updated WP_Query loop ended up not pulling in any data (Screenshot 3: https://i.stack.imgur.com/JLibO.png ).
The only exception is if I omit any ID in the VC editor backend; If I don't enter an ID, it defaults to the most recent Cause. However, if I enter any ID, even if it's the same ID as the most recent post, nothing shows up...
Any idea what could be wrong with my logic?
Thank you!
render function Fixed!
$fea_cause = $atts['id'];
$donations = new WP_Query( array( 'post_type' => 'donation', 'posts_per_page' => 1, 'post__in' => array($fea_cause )));
Huge props to Nilesh Sanura for his help!
I am attempting to display the latest x posts from all categories using a custom taxonomy called 'case-studies' in WordPress.
I have managed to output all the category names as heading links I have also outputted some posts below each category but they are all the same, I need a way to filter the post output by the category, see PHP comments below for details.
<?php if (have_posts()) :
$tax = 'case-studies';
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_terms($tax, $cat_args);
foreach($categories as $category) {
echo '<p><a href="' . get_term_link( $category, $tax ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></p>';
// Post output is not working, outputs all posts using case-studies taxonomy for each category, I need a way to filter this query by category, 'category' => $category->term_id does not work, nor does 'category' => $category
$post_args = array(
'posts_per_page' => 10,
'post_type' => 'case-studies'
);
$posts = get_posts($post_args);
foreach($posts as $post) { ?>
<?php echo get_the_title(); ?><br/>
<?php }
echo '<p><a href="' . get_term_link( $category, $tax ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></p>';
}
endif; ?>
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>';
}