biglaugh
Forum Replies Created
-
Forum: Developing with WordPress
In reply to: Add Category names to post lists of custom post typeThank you very much both of you @bcworkz @tugbucket ?? for your time and help, I completed the code and it’s working fine. wish you all the best.
here’s the final code for others to use “ Showing Posts of Specific Custom Post Type and Display post name and post taxonomy “:
<?php $args = array( 'numberposts' => -1, 'post_type' => 'custom-post-type', 'orderby' => 'title', 'order' => 'ASC', array( 'taxonomy' => 'post-type-taxonomy-name', 'field' => 'slug', ), ); $myposts = get_posts($args); if($myposts): foreach ($myposts as $mypost): // get terms $terms = get_the_terms( $mypost->ID, 'post-type-taxonomy-name' ); // showing only first category of post $onecategory = $terms[0]; $category_link = get_category_link($onecategory->term_id); ?> <div class="row"> <div class="col-xs-9"> //trimmed the title to only 20 Characters, you can change it or remove it <a style="color:white;" href="<?php echo get_permalink($mypost->ID); ?>"> <?php ?> <?php echo $trimmed = mb_strimwidth( get_the_title($mypost->ID), 0, 20, ); ?> - <?php echo $onecategory->name; ?></a> </div> </div> <?php endif; endforeach; wp_reset_postdata(); ?> <?php endif; ?>
- This reply was modified 2 years, 4 months ago by biglaugh.
Forum: Developing with WordPress
In reply to: Add Category names to post lists of custom post type@bcworkz Thank you, I understand so setup_postdata() makes a short pass for ids,
forgive me for saying it incorrectly, yes my custom post type is named: ‘zero’ and its taxonomy name is: ‘zero_categories’ .
so for custom post type taxonomy we should call it by get_the_terms(), I guess its something like this?
<?php $args = array( 'numberposts' => -1, 'post_type' => 'zero', 'orderby' => 'title', 'order' => 'ASC', array( 'taxonomy' => 'zero_categories', 'field' => 'slug' ) );
- This reply was modified 2 years, 4 months ago by biglaugh.
Forum: Developing with WordPress
In reply to: Add Category names to post lists of custom post type@tugbucket I tested this code now, and it doesn’t work outputs nothing, can we somehow declare our zero_categories into this array, I renamed zero_categories to the category but it doesn’t work too, apparently each Custom post type must have a specific category name.
Original WordPress post -> category
custom post type work -> custom category name like zero_categoriesif we somehow manage to declare zero_categories to loop
- This reply was modified 2 years, 4 months ago by biglaugh.
Forum: Developing with WordPress
In reply to: Add Category names to post lists of custom post typeThank you dear @tugbucket, I’ve realized that I named my category when I was making my custom post type like this: zero_categories | is it related to why I cant get category names?
Custom post type name: Zero
Custom post type category name registered as: zero_categoriesSorry for asking too many questions, Thank you.
Forum: Developing with WordPress
In reply to: Add Category names to post lists of custom post typeThank you for your reply dear @bcworkz, so for post data i should do something like this:
// put above the code
global $post;
// then add this to array
'category' => 0,
// then add this to my loopforeach ($myposts as $mypost): setup_postdata( $mypost ); ?>
and after that i should
echo get_the_category ?
- This reply was modified 2 years, 4 months ago by biglaugh.
Forum: Developing with WordPress
In reply to: Add Category names to post lists of custom post typeThanks for the reply dear @tugbucket , I added both codes the code as follows here, but I got not results for the category name only the post title I received in output.
<?php // Set the arguments for the query $args = array( 'numberposts' => -1, // -1 is for all 'post_type' => 'custom post type name', // or 'post', 'page' 'orderby' => 'title', // or 'date', 'rand' 'order' => 'ASC', // or 'DESC' // 'category' => $category_id, // 'exclude' => get_the_ID() ); // Get the posts $myposts = get_posts($args); // If there are posts if($myposts): // Loop the posts foreach ($myposts as $mypost): $cats = get_the_category($mypost->ID); $all_cats = ""; foreach($cats as $cat){ $all_cats .= $cat->cat_name ." "; } ?> <div class="row"> <!-- Content --> <div class="col-xs-9"> <a href="<?php echo get_permalink($mypost->ID); ?>"> <?php echo get_the_title($mypost->ID); ?> <?php echo get_the_category($mypost->ID)[0]->cat_name; ?></a> <?php echo $all_cats; ?> </div> </div> <?php endforeach; wp_reset_postdata(); ?> <?php endif; ?>