How to display custom taxonomy parent and child including the posts - wordpress

I'm currently working on a Wordpress-project which has to display all the categories, subcategories and posts within these subcategories of a custom post type & taxonomy.
I should become something like this:
Category 1
subcategory
post 1
post 2
subcategory
post 3
Category 2
subcategory
post 4
At this moment the code returns a list of all the categories and subcategories
in the taxonomy between the h3-tags. Only the parent categories should be displayed here.
<?php
$terms = get_terms('resource_category', array('hierarchical' => false));
foreach ($terms as $term) {
$cat_slug = $term->slug;
$cat_id = $term->term_id;
$subcats = get_categories('child_of='.$cat_id.'&taxonomy=resource_category');
if ( have_posts() ) :
/* CATEGORY */ ?>
<div class="resources">
<?php echo '<h3>'.$term->name.'</h3>';
/* SUBCATEGORY */
foreach ($subcats as $subcat) {
if ( have_posts() ) :
echo '<h4>' . $subcat->name .'</h4>';
query_posts('post_type=resources&resource_category='.$subcat->cat_name.'&hide_empty=1'); ?>
<?php while ( have_posts() ) : the_post();
/* SUBCATEGORY POSTS */?>
<div class="resource-item">
<ul>
<li><?php the_title(); ?></li>
</ul>
</div>
<?php endwhile; endif; wp_reset_query();} ?>
</div>
<?php endif; wp_reset_query(); } ?>
Big thanks if anyone can help me with this!

$terms = get_terms('mobile_category', array('hide_empty'=> 0,'orderby'=> 'count','parent' => 0));
foreach ($terms as $term) {
$cat_slug = $term->slug;
$cat_id = $term->term_id;
$subcats = get_categories('child_of='.$cat_id.'&taxonomy=mobile_category');
// if ( have_posts() ) :
/* CATEGORY */ ?>
<div class="resources">
<?php echo '<h3>cat pr1 -- '.$term->name.'</h3>';
/* SUBCATEGORY */
foreach ($subcats as $subcat) {
if ( have_posts() ) :
echo '<h4>sub 2 ' . $subcat->name .'</h4>';
query_posts('post_type=mobile&mobile_category='.$subcat->cat_name.'&hide_empty=1'); ?>
<?php while ( have_posts() ) : the_post();
/* SUBCATEGORY POSTS */?>
<div class="resource-item">
<ul>
<li><?php the_title(); ?></li>
</ul>
</div>
<?php endwhile;
endif;
wp_reset_query();}

Related

Display custom post type categories (terms) in loop

I am using the following code to load all the posts from a custom post type, in this loop I show a title but I also want to show the categories (terms) that are connected to this particular post but I can't seem to make it work
Loop:
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I tried:
<?php echo $term->name; ?>
You need to use get_the_terms() for getting category
you can get this by below code...
you need to put second argument a custom post-type's category slug
you can refer this link https://developer.wordpress.org/reference/functions/get_the_terms/
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
<?php
$terms = get_the_terms( get_the_ID(), 'category-slug' ); // second argument is category slug of custom post-type
if(!empty($terms)){
foreach($terms as $term){
echo $term->name.'<br>';
}
}
?>
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

Wordpress ACF Repeater Field - Only pull in first 2 fields

Topic kinda says it all. I need to only pull in the first two fields of an ACF repeater field.
Here is what I am trying to use but it is obviously not right:
<?php $args = [ 'posts_per_page' => 2, 'order' => 'desc']; ?>
<?php $ar = new WP_Query( $args ); ?>
<?php if( $ar->have_rows('prodImgs') ): while ( $ar->have_rows('prodImgs') ) : $ar->the_row(); ?>
<img src="<?php the_sub_field('prodImg'); ?>" alt="">
<?php endwhile; ?>
<?php endif; ?>
How am I supposed to do this?
<?php
// check if the repeater has data
if( have_rows('prodImgs') ) {
//counter
$i=0;
//loop through the rows
while( have_rows('prodImgs') ) {
the_row();
//check if 2 subfields have been shown
if ( $i > 1 ) { break; }
echo "<img src='" . get_sub_field('prodImg_sub') . "' alt='Lorem ipsum'>";
$i++;
}
}
?>
You are mixing apples and pears, WP_Query and ACF Repeater field. WP_Query returns the post data, whilst the ACF function have_rows( $repeater_field_name, $post_id ); checks whether there are any data in the custom field repeater that is on your page/post (or if you specify $post_id on the relevant post/page). More info at https://www.advancedcustomfields.com/resources/repeater/
<?php $rows = get_field('prodImgs'); ?>
<?php $i = 0; if ($rows):?>
<?php foreach($rows as $row): ?>
<img src="<?php echo $rows[$i][/* name of first field */]; ?>" alt="">
<?php echo $rows[$i][/* name of Second field */]; ?>
<?php $i++; endforeach; ?>
<?php endif; ?>

Is there a wordpress function that allows a page to display all posts from some specific category?

We have a wordpress function to display all the posts like-
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php }
}
?>
Is there any function in wordpress that will displays only the posts from some specific category?
like -
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post(); ?>
<h2><?php the_title(); ?></h2> //from Category = X
<?php }
}
?>
You can simply fetch it with the function :
<?php query_posts( 'cat=x' ); ?>
where "x" is your category ID
This function offert multiple advantages as you can filter and order the result
Get category 3 posts from 2004
<?php query_posts( 'cat=3&year=2004' ); ?>
Get category 1 posts from 2004 and order by date ASCENDING.
<?php query_posts( 'cat=1'.'&year=2004&orderby=date&order=asc'); ?>
Want to retrieve the actual category ? Simply use :
<?php
$catId = get_cat_ID('MYCATEGORY'); //get current category id
query_posts( 'cat='.$catId ); // query the posts
if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<h1>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h1>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>

Display custom categories in custom post loop

I am learning to create a Portfolio section. I already created the custom post type and a custom taxonomy for the portfolio categories. The categories are working ok, i can choose which category i want for every portfolio item.
I am trying to loop within the post_type portfolio to get the items and it works ok, but i cannot get the categories of each item.
<?php $loop = new WP_Query( array( 'post_type' => 'portfolio') ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="panel">
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<p><?php the_category(); ?></p>
</div>
<?php endwhile; wp_reset_query(); ?>
I am using the above code, but the categories dont show up.
I tried separately to display the categories and are working ok with this code:
<?php
$args = array( 'taxonomy' => 'portfolio_categories', );
$categories = get_categories($args);
foreach($categories as $category) { ?>
<?php echo $category->name;?>
<?php }
?>
So how can i display each portfolio item categories in the loop ?
Try following code, this will print all custom taxonomies of post.
<?php
$terms = get_the_terms( $post->ID, 'portfolio_categories' );
if ( $terms && ! is_wp_error( $terms ) ) :
$taxonomies = array();
foreach ( $terms as $term ) {
$taxonomies[] = $term->name;
}
$taxonomies = implode(", ", $taxonomies );
?>
<p class="Custom-Taxonomies">
Custom Taxonomies: <span><?php echo $taxonomies; ?> </span>
</p>
<?php endif; ?>

How do I sort category results on this code?

I have this code on the category.php file and I need to sort the results by ASC cause right now results are in the reverse order.
Thanks in advance.
<ol class="search-results-list">
<?php
// Return Event Items
$i = 0;
while (have_posts()) : the_post();
if( get_post_type() == 'researcher' ) {
$i++; ?>
<li><strong><?php the_title(); ?></strong><br /> <?php print_excerpt(200); ?></li>
<?php }
endwhile;?>
<?php if( $i == 0 ) { ?><li><?php _e( 'No results were found.', 'qns' ); ?></li><?php } ?>
<!--END .search-results-list -->
</ol>
Add below code into your functions.php file
if ( !function_exists( 'get_cat_id_by_slug' ) ) {
function get_cat_id_by_slug ($get_slug) {
$CatId = get_term_by( 'slug', $get_slug, 'category' );
$CatId = $CatId->term_id;
return $CatId;
}
}
Add below code into your category.php
<?php
$category_id = get_cat_id_by_slug('researcher');
$args = 'cat=' . $category_id . 'order=asc';
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post();
?>
<ol class="search-results-list">
<li>
<strong>
<?php the_title(); ?>
</strong><br />
<?php print_excerpt(200); ?>
</li>
</ol>
<?php
endwhile;
else :
?>
<ol class="search-results-list">
<li><?php _e( 'No results were found.', 'qns' ); ?></li>
</ol>
<?php
endif;
?>

Resources