So I have some post types that are assigned a category, which have a parent category. How would I go about displaying the parent category in a list along with its child and post? Something like this:
Parent Cat
-> Child Cat
-> -> Post
This is what I have written, but it displays ALL of the categories and not the associated posts.
<?php
$args = array(
'post_type' => 'job_listing'
);
$categories = get_categories( $args );
$posts = get_posts($args);
foreach ( $categories as $category ) {
echo '<h4>' . $category->name . '</h4>';
?>
<div class="menu-items-container">
<?php foreach($posts as $post) { ?>
<?php if (in_category($category)) { ?>
<p><?php the_post_title(); ?></p>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
Related
I have a WP website with a portfolio section.
On portfolio page it shows project categories.
I would like to show only child categories of parent category with id=63.
This is the current code:
<?php $terms = get_the_terms( $post->ID , 'portfolio_category', 'string' ); ?>
<?php $num_of_terms = count($terms); ?>
<?php if($terms) { ?>
<div class="meta-column">
<strong class="caps"><?php esc_html_e( 'Artist', 'onioneye' ); ?><span class="colon">:</span></strong>
<span>
<?php
$i = 0;
foreach($terms as $term) {
if($i + 1 == $num_of_terms) {
echo esc_html($term -> name);
}
else {
echo esc_html($term -> name . ', ');
}
$i++;
}
?>
</span>
</div>
<?php } ?>
Could you try this:
$args = array('child_of' => 63);
$terms = get_categories( $args );
in place of this:
$terms = get_the_terms( $post->ID , 'portfolio_category', 'string' );
I'm trying to set an active class on a tag list with the below code:
<?php
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
$args = array( 'category' => $category_id, 'post_type' => 'post' );
$postslist = get_posts($args); ?>
<div class="archieve__list">
<ul class="row" data-equalizer data-equalize-on="medium">
<?php foreach ($postslist as $post) : setup_postdata($post); ?>
<?php the_tags( '
<li class="column small-12 medium-4 text-center float-left archieve__item"><span class="archieve__link" data-equalizer-watch>', '</span></li>
'); ?>
<?php endforeach; ?>
</ul>
</div>
I've looked into using get_tags() however this pulls back all of the tags where I only want it to pull back the tags of the current category. This code below does what I want, setting an active state on the tag in use but I again need it to just output the tags of that category:
<ul id="blog-tags">
<?php
$tags = get_tags();
if ( $tags ) {
foreach ( $tags as $tag ) {
echo '<li>';
if ( (int) $tag->term_id === get_queried_object_id() )
echo "<b>$tag->name</b>";
else
printf(
'%2$s',
get_tag_link( $tag->term_id ),
$tag->name
);
echo '</li>';
}
}
?>
</ul>
I really would like the latter option to work as it feels a lot cleaner than the first option.
I'm not sure if you actually mean "Tags of the current category" as Categories and Tags are both taxonomies. But if you want to pull the tags of the current post you can use wp_get_post_tags()
I'm working on a custom WordPress theme.
I'm trying to show a list of categories (as headings) for a custom post type, and underneath each category heading I would like to list the post titles and a single custom field. I'm using the Types plugin. I know a little php and have some WordPress experience, but not quite enough to properly figure this out.
Example:
Custom Post Type: Menu Item
Custom Field for This Post Type: Item Price
Category: Sandwich Fillings
Category: Soups
Desired result:
Sandwich Fillings
Cheese - £#.##
Ham - £#.##
Tuna - £#.##
...
Soups
Tomato
Chicken
Vegetable
The idea will be for new categories to be added on the fly (for example, one day they might start selling melts), and for WP to iterate through the categories as new ones are added, saving from having to hard-code in new categories on the page as they are added.
Here's what I have so far:
<?php
$args = array(
'post_type' => 'menu-item',
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
$posts = get_posts($args);
$item_price = types_render_field( "item-price" );
echo '<h2>' . $category->name . '</h2>';
?>
<ul><?php
foreach($posts as $post) {
?>
<li><?php the_title(); ?>, <?php echo $item_price; ?></li>
<?php
}
}
?>
What I'm getting is this:
Sandwiches
Tomato,
Swiss Cheese,
French Brie,
French Brie,
...
Soups
Tomato, £2.60
Swiss Cheese, £2.60
French Brie, £2.60
French Brie, £2.60
Any help would be appreciated!
UPDATE
This seems to have helped:
<?php
$args = array(
'post_type' => 'menu-item'
);
$categories = get_categories( $args );
$posts = get_posts($args);
foreach ( $categories as $category ) {
echo '<h2>' . $category->name . '</h2>';
?>
<div class="menu-items-container">
<?php foreach($posts as $post) { ?>
<?php $item_price = types_render_field( "item-price" ); ?>
<?php if (in_category($category)) { ?>
<p><?php the_title(); ?>, <?php echo $item_price; ?></p>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
And it's giving me the results that I was looking for. Stumbled upon this fix through experimentation and happy accident, so I'm aware that it may not be best practice - advice or suggestions for improvement would be welcome!
Something like this might be better, as it does not loop through all the posts for each category:
<?php
$args = array(
'post_type' => 'menu-item'
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '<h2>' . $category->name . '</h2>';
$args['category'] = $category->term_id;
$posts = get_posts($args); ?>
<div class="menu-items-container">
<?php foreach($posts as $post) { ?>
<?php $item_price = types_render_field( "item-price" ); ?>
<p><?php the_title(); ?>, <?php echo $item_price; ?></p>
<?php } ?>
</div>
<?php } ?>
I want to ask the Portfolio Page Template to exclude one portfolio category from being displayed in the portfolio. So when it shows the categories: All | Travel | Macro | Archive, etc. I need to exclude Archive (slug archive) from both the top filter and from the All feed, because I want to place that on a separate page called Archive.
How do I do that?
<?php
/*
Template Name: Portfolio number 1
*/
?>
<?php get_header(); ?>
<div class="container">
<div id="homecontent">
<ul id="portfolio-filter" class="filter clearfix">
<li class="active">All</li>
<?php
// Get the taxonomy
$terms = get_terms('categories');
$term_list = '';
// set a count to the amount of categories in our taxonomy
$count = count($terms);
// set a count value to 0
$i=0;
// test if the count has any categories
if ($count > 0) {
// break each of the categories into individual elements
foreach ($terms as $term) {
// increase the count by 1
$i++;
// rewrite the output for each category
$term_list .= '<li>' . $term->name . '</li>';
// if count is equal to i then output blank
if ($count != $i)
{
$term_list .= '';
}
else
{
$term_list .= '';
}
}
// print out each of the categories in our new format
echo $term_list;
}
?>
</ul>
<div style="clear: both;"></div>
<ul id="portfolio-list" class="filterable-grid clearfix centerrow filter-posts">
<?php
// Set the page to be pagination
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
// Query Out Database
$wpbp = new WP_Query(array( 'post_type' => 'myportfoliotype', 'posts_per_page' =>'99', 'paged' => $paged ) );
?>
<?php
// Begin The Loop
if ($wpbp->have_posts()) : while ($wpbp->have_posts()) : $wpbp->the_post();
?>
<?php
// Get The Taxonomy 'Filter' Categories "categories"
$terms = get_the_terms( get_the_ID(), 'categories' );
?>
<?php
$large_image = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'fullsize', false, '' );
$large_image = $large_image[0];
$another_image_1 = get_post_meta($post->ID, 'themnific_image_1_url', true);
$video_input = get_post_meta($post->ID, 'themnific_video_url', true);
$price = get_post_meta($post->ID, 'themnific_item_price', true);
$ribbon = get_post_meta($post->ID, 'themnific_class', true);
?>
<li class="centerfourcol filter" data-id="id-<?php echo $count; ?>" data-type="<?php foreach ($terms as $term) { echo strtolower(preg_replace('/\s+/', '-', $term->slug)). ' '; } ?>">
<?php get_template_part('/includes/folio-types/home_carousel'); ?>
</li>
<?php $count++; // Increase the count by 1 ?>
<?php endwhile; endif; // END the Wordpress Loop ?>
<?php wp_reset_query(); // Reset the Query Loop?>
</ul>
<?php
/*
* Download WP_PageNavi Plugin at: http://wordpress.org/extend/plugins/wp-pagenavi/
* Page Navigation Will Appear If Plugin Installed or Fall Back To Default Pagination
*/
if(function_exists('wp_pagenavi'))
{
wp_pagenavi(array( 'query' => $wpbp ) );
wp_reset_postdata(); // avoid errors further down the page
}
?>
<div style="clear: both;"></div>
</div><!-- #homecontent -->
</div>
<?php get_footer(); ?>
I found the answer, the code below only allows one categorie to be displayed in the portfolio.
‘tax_query’ => array( array( ‘taxonomy’ => ‘categories’, ‘field’ => ‘slug’, ‘terms’ => ‘archive’, ‘operator’ => ‘IN’) ),
I'm to list categories with images in the sidebar this is how I do it (and it works) I do this because I've certain categories I do not want to display!
<?php $latests = new WP_Query('posts_per_page=2&ignore_sticky_posts=1&cat=12'); ?>
<?php echo get_cat_name(12); ?>
<?php while ($latests->have_posts()) : $latests->the_post(); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata(); ?>
but I need to copy past that code for every single category... and all this code for only changing a number is a good practice I guess. Is there another way it could be done?
I've tried with a foreach but it seems to be wrong
<?php $latests = new WP_Query('posts_per_page=2&ignore_sticky_posts=1&cat=12'); ?>
<?php foreach($latests as $latest) :?>
<?php while ($latests->have_posts()) : $latests->the_post(); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata(); ?>
<?php endforeach; ?>
Well, You can do it like this:
<ul>
<?php
$cat_args=array(
// 'include' => '3,6,9', // display only these categories
'exclude' => '3,6,9', // display all categories except categories 3,6,9
'orderby' => 'name', // the order
'order' => 'ASC' // asc or desc
);
$categories=get_categories($cat_args);
foreach($categories as $category) {
$args=array(
'showposts' => 2, // how many posts you want to display
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
$posts=get_posts($args);
if ($posts) {
echo '<h3> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in: %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h3> ';
foreach($posts as $post) {
setup_postdata($post);
?>
<li>
<div>
<div><?php if ( has_post_thumbnail() ) { the_post_thumbnail('sidebarcat'); } ?></div>
<div><?php the_title(); ?></div>
</div>
</li>
<?php
} // close foreach
} // close if
} // close foreach
?>
</ul>
The easiest way is like that
<?php wp_list_categories('orderby=name&exclude=3,5,9,16'); ?>
So, this will return you all the categories excluding the one you specified. After this, you can get the actual image you want for your categories and all.