I am creating a FAQ page using a custom post type and custom taxonomy. I'm trying to create an unordered list for each taxonomy in order to group the FAQ's. In that unordered list, I want the first listed item to be the taxonomy name and then repeat the second listed item for all the questions in the taxonomy. This is the page I'm working on link.
It's currently duplicating the posts instead of displaying in the rightful taxonomies.
<?php
// get all the categories from the database
$cats = get_terms( array(
'taxonomy' => 'faq_categories',
));
// loop through the categories
foreach ($cats as $cat) {
// setup the category ID
$cat_id = $cat->term_id;
?>
<!-- Make a header for the category -->
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
// create a custom wordpress query
query_posts( array(
'post_type' => 'faqs',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories', //or tag or custom taxonomy
'field' => 'slug',
'terms' => 'for-women'
)
)
));
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category
wp_reset_postdata();
?>
</ul>
<?php } // done the foreach statement ?>
Your query is not changing as you iterate through the $cats array. Perhaps changing the the value of the 'terms' array to $cat->slug would give you better results.
Thank you so much. You have both provided great insight as to what I was missing. I've resolved it now and this is how I went about solving it taking your suggestions into consideration.
<?php
$cats = get_terms(
array(
'taxonomy' => 'faq_categories',
'orderby' => 'term_id',
'order' => 'ASC'
)
);
foreach ($cats as $cat) :
?>
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
$questions = new WP_Query(
array(
'category_name' => $cat->slug
)
);
$questions = new WP_Query( array(
'post_type' => 'faqs',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories',
'field' => 'slug',
'terms' => array($cat->slug),
)
)
));
?>
<?php if ($questions->have_posts()) : while ($questions->have_posts()) : $questions->the_post();?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
In your query_post, your tax_query's field should be term_id and your terms be assigned to your $cat_id variable, instead of a hardcoded term.
Related
I'm trying to get the items from custom taxonomy on a page but not really going well. I would like to get the term name of custom taxonomy and custom field from custom post to show on the page. If there is no applicable item, would like to get the item from parent.
For example, As a category of food type,
**“Food> Italian> Pizza" (page category)**
On the other hand, in Recipes of custom post, If there was only
**"Food> Italian" (custom taxonomy is recipe-cat)**
The food of Pizza page will list recipe in the “Italian" category.
page category slug and taxonomy term are the same.
I appreciate your help!
$cat = get_the_category();
$children = get_term_children($cat->cat_ID ,'category');
$parents_cat = get_category(!($children));
if(!($children)) :
$args = array(
'post_type' => 'recipe',
'posts_per_page' => '5',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'recipe-cat',
'field' => 'slug',
'terms' => $parents_cat->slug,
)
),
);
$recipe = new WP_Query( $args );
if($parents_cat): ?>
<div class="recipe-box">
<h2><span><?php $parents_cat->name; ?>s recipe</span></h2>
<ul class="recipe-info">
<?php while ( $recipe->have_posts() ) : $recipe->the_post(); ?>
<?php $title = get_field('title'); if( $title ): ?>
<li><?php echo $title; ?></li>
<?php endif; ?>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
<?php endif; ?>
I have created a foreach loop for the category section in my sidebar. Everything is working good, however, I don't want to allow one of the categories to show. I have not been able to find anything on how to null out an object in query or anything.
here is my code, the name of the category is "general information"
<aside class="sidebar">
<div class="category" data-aos="fade-left" data-aos-delay="300">
<h2>Popular Categories</h2>
<ul class="category-list">
<?php
$args = array(
'orderby' => 'count',
'order' => 'DESC'
);
$terms = get_terms('category', $args); /*Name Of category*/
foreach (array_slice($terms, 0, 5) as $term ):
?>
<li class="list-items" data-aos="fade-left" data-aos-delay="400">
<a href="<?php echo site_url('/general');?>">
<?php echo
$term->name ;?> </a>
<span>(<?php echo $term->count ;?>)</span>
</li>
<?php
endforeach;
wp_reset_query();
?>
</ul>
</div>
I am guessing that there is a call I can put in the $args(). Any help would be greatly appreciated.
$args = array(
'orderby' => 'count',
'order' => 'DESC'
'exclude' => array( 77 ), // ID of Category which you don't want to show
);
You can exclude like this.
I have this code in category.php and I want to show posts from the category in which I am, "$cat" and the category referred to id 63. I wrote something but do not want to work :/
<?php
$cat->term_id;
$my_query_args = array(
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array(63, $cat),
'operator' => 'AND'
)
)
);
$my_query = new WP_Query($my_query_args);
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
Can someone tell me why do not incur the currently displayed category to $cat?
Thanks in advance for your help!
$category = get_category( get_query_var( 'cat' ) );
echo $cat_id = $category->cat_ID;
This will give you the current category id.
Now place the below query before the starting of wordpress loop in category.php
query_posts( 'cat='.$cat_id);
If thats normal category you dont want to make it complex with too many args. This will work dynamically by checking your current category id.
may be you mess with 'field' arguments in your tac_query.
'field' => 'term_id',
field- Select taxonomy term by. Possible values are 'term_id', 'name', 'slug' or 'term_taxonomy_id'. Default value is 'term_id'.
probably your $cat variable is an object. Try to change 'terms' => array( 63, $cat ), into 'terms' => array( 63, $cat->ID ), or 'terms' => array( 63, $cat->id ),
try this set your cat id or name at cat =..
<?php
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><h3><?php the_title(); ?></h3>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>
How to display all categories of a custom post type on home screen without listing the items.
I already created the custom post type and it's categories, now I need to display all the categories on my home page as links to the each category page. Can someone help please?
You can use now get_categories
Here is an example of code:
<?php
$args = array(
'taxonomy' => 'Your Taxonomy Name',
'hide_empty' => 0,
'orderby' => 'name'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link($cat->slug); ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
Remember write your taxonomy name as you registered, in here 'Your Taxonomy Name'
e.g. product_cat, blog_cat etc
Hope this will help you.
$cat_args = array(
'taxonomy' => 'your-custom-post', // your custom post type
);
$custom_terms = get_categories($cat_args);
echo print_r($custom_terms);
<?php
$terms = get_terms( 'taxonamy_name', array(
'orderby' => 'count',
'hide_empty' => 0
) );
foreach($terms as $term)
{
echo $term->name;
}?>
</ul>
</div>
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
I would like to know if it's possible to show the sticky posts inside the wp_query and sort them out according to their respective categories:
loop(
- the first sticky post has the category 1
- the second sticky post has the category 2
- the third sticky post has the category 1
)
and that should display:
- category 1:
- the first sticky post
- the third sticky post
- category 2:
the second sticky post
with this html:
<div class="flex-6">
<h4><?php
$category = get_the_category();
echo $category[0]->cat_name;
?></h4>
<ul class="list">
<li><?php the_title(); ?></li>
</ul>
</div>
I have the correct loop for the sticky posts:
$sticky = get_option('sticky_posts');
$query = new WP_Query(array('post__in' => $sticky));
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
$category_name = get_the_category();
$category_name = $category_name[0]->cat_name;
endwhile; endif;
To have this final result
<div class="flex-6">
<h4>Category 1</h4>
<ul class="list">
<li>The first title</li>
<li>The third title</li>
</ul>
</div>
<div class="flex-6">
<h4>Category 2</h4>
<ul class="list">
<li>The secondtitle</li>
</ul>
</div>
Any idead?
Thanks for your time
The most straightforward way would be to get the categories first:
<?php
$cat_args = array(
'child_of' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'taxonomy' => 'category'
);
$cats = get_categories($cat_args);
and then cycle through them getting the posts:
$sticky = get_option('sticky_posts');
foreach ($cats as $cat) :
$args = array(
'post_type' => 'post',
'post__in' => $sticky,
'posts_per_page' => -1,
'orderby' => 'title', // or whatever you want
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $cat->slug
)
)
);
$posts = get_posts($args);
if ($posts) :
?>
<div class="flex-6">
<h4><?php echo $cat->cat_name; ?></h4>
<ul class="list">
<?php foreach ($posts as $post) : ?>
<li><?php echo get_the_title($post->ID); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php
endif;
endforeach;