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' );
Related
There is a cycle that outputs products
<?php
$args = array('post_type' =>
'product');
$loop = new WP_Query($args);
foreach ($loop->posts as $have_post) : ?>
<div class="swiper-slide">
<a href="
<?php
$permalink = get_post_permalink($have_post->ID);
echo $permalink;
?>
" class="goods__item">
<div class="goods__text">
<?php
echo $have_post->name; ?>
</div>
<div class="goods__img">
<?php
echo get_the_post_thumbnail($have_post->ID, 'full', array('alt' => $have_post->post_title, 'data-product_id' => $have_post->ID)); ?>
</div>
<div class="goods__name"><?php echo $have_post->post_title; ?></div>
<div class="goods__price">
<?php
$price = get_post_meta($have_post->ID, '_price', true);
echo $price; ?> руб.
</div>
</a>
</div>
<?php endforeach; ?>
Need to show the category of current product here. I cant get the category. Please help
<div class="goods__text"><?php echo $have_post->name; ?></div>
You could use get_the_terms to get the correct terms. I believe it's product_cat the correct taxonomy.
$terms = get_the_terms( $have_post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
$product_cat_name = get_cat_name($product_cat_id);
break;
}
Try get_the_category( $post->ID );
I use Wordpress and I want to get recent posts with thumbnail and description.
So I use wp_get_recent_posts.
<?php $args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts($args);?>
<ul class="main-slider">
<?php foreach( $recent_posts as $recent ){?>
<li>
<?php echo '<div class="textoverlay">
<h1>' . $recent["post_title"].'</h1>
<p>'.get_the_excerpt().'</p>
</div> ';
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
} ?>
</li>
<?php
}?>
</ul>
I can get post title and link, but I can't get description:
<p>'.get_the_excerpt().'</p>
You have just missing of post_id in description without post_id it can't display anything.
<?php $args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts($args);?>
<ul class="main-slider">
<?php foreach( $recent_posts as $recent ){?>
<li>
<?php echo '<div class="textoverlay">
<h1>' . $recent["post_title"].'</h1>
<p>'.get_the_content($recent["ID"]).'</p>
</div> ';
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
} ?>
</li>
<?php
}?>
</ul>
echo wpautop( $recent['post_content'] );
I'm trying to capitalise the Wordpress portfolio category string of "Corporate identity".
I have tried using this post: Capitalise every word of a string in PHP?, but have had no luck.
URL: http://utopia.gerandeklerk.com/
On the home page, hover over the first portfolio item (Steam Africa 2014), you will notice on the hover-state it lists the portfolio categories "Collateral" and "Corporate identity". The client wants wants it to display "Corporate Identity", as it does in the category filter menu above it.
I added the following string - can someone tell me where I went wrong?
$portfolio_category[] = ucwords(strtolower($term->slug)); // Capitalise Portfolio Category String
Below is the code from portfolio.php
<div class="filter-container">
<ul data-drop-content class="filter-list">
<li>All</li>
<?php
$terms = get_terms("portfolio_category");
foreach ( $terms as $term ) {
echo "<li><a href='#' data-filter='" . "." . strtolower($term->slug) . "'>" . $term->name . "</a></li>";
};
?>
</ul>
<span>| More filters</span> <img src="<?php echo get_template_directory_uri(); ?>/images/down-arrow.png">
<?php
$desired_width = 440;
$desired_height = 440;
if(is_tax()) { // is category page
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array( 'post_type' => 'portfolio', 'portfolio_category' => $term -> slug, 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order'=>'ASC' );
}
else { // is main portfolio page
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => -1, 'orderby' => 'menu_order','order'=>'ASC' );
}
$loop = new WP_Query( $args );
if($loop->have_posts()) {
?>
<div class="portfolio-gallery group isotope">
<?php
//output the latest projects from the 'my_portfolio' custom post type
while ($loop->have_posts()) : $loop->the_post();
?>
<?php
$terms = get_the_terms( $post->ID, 'portfolio_category' );
if ( $terms && ! is_wp_error( $terms ) ) {
$portfolio_category = array();
foreach ( $terms as $term ) {
$portfolio_category[] = ucwords(strtolower($term->slug)); // Capitalise Portfolio Category String
}
$the_tax = join( " ", $portfolio_category );
} else {
$the_tax = "";
};
?>
<div class="portfolio-item <?php echo $the_tax; ?> portfolio-item-<?php the_ID(); ?>">
<a class="project-link" href="<?php the_permalink(); ?>">
<div class="thumb-container">
<div class="portfolio-thumb">
<?php if( get_field('gray_scale_image') ): ?>
<img src="<?php the_field('gray_scale_image'); ?>" class="grey-scale-image" />
<?php endif; ?>
<?php if( get_field('gray_scale_image') ): ?>
<img src="<?php the_field('colour_image'); ?>" class="colour-image" />
<?php endif; ?>
</div>
</div><!-- /.thumb-container -->
</a><!-- /.project-link -->
<h3 class="portfolio-category"><?php echo str_replace('-',' ',ucwords(join(", ", explode(" ", $the_tax))) ); ?></h3>
<h3 class="portfolio-title"><?php the_title(); ?></h3>
</div><!-- /.portfolio-item -->
<?php endwhile; ?>
</div><!-- .portfolio-gallery -->
<?php } // end if ?>
It goes wrong on this line
<h3 class="portfolio-category"><?php echo str_replace('-',' ',ucwords(join(", ", explode(" ", $the_tax))) ); ?></h3>
On this line you can try the following function as you mentioned earlier
$bar = ucwords(strtolower($bar)); // Hello World!
I Didn't tested the code
I use this code to display other posts of same category in my page:
<?php
global $post;
$categories = get_the_category();
$ceker=false;
foreach ($categories as $category) {
if ($ceker == false){
$ceker=true;
?>
<h3 class="naslovostalih">Ostali članci iz ove kategorije:</h3>
<ul class="clanciostalih">
<?php
$args = array(
'numberposts' => 10,
'offset'=> 1,
'category' => $category->term_id,
'exclude' => $post->ID
);
}
$posts = get_posts($args);
foreach($posts as $pz) { ?>
<li>
<?php
$title = $pz->post_title;
$link = get_permalink($pz->ID);
printf('<a class="linkpost" title="%s" href="%s">%s</a>', $title, $link, $title);
the_post_thumbnail('thumb-232');
echo '<div id="excerptcu">';
echo excerpt(25);
echo '</div>';
?>
<p class="more-link-wrapper2"><?php _e( 'Opširnije »', 'fearless' ); ?></p>
</li>
<?php } // end foreach ?>
</ul>
How can I exclude current post that a client is viewing from query, so it doesn't display in "Other posts from this category" list?
Many thanks!
There is a param "exclude" with get_posts
$args = array(
'numberposts' => 10, 'offset'=> 1,
'category' => $category->term_id,
'exclude' => $post->ID
);
$posts = get_posts($args);
Use another variable in your foreach() loop because it's ambiguous with global $post
Edit
<?php
global $post;
$categories = get_the_category();
$ceker=false;
foreach ($categories as $category) {
if ($ceker == false){
$ceker=true;
?>
<h3 class="naslovostalih">Ostali članci iz ove kategorije:</h3>
<ul class="clanciostalih">
<?php
$args = array(
'numberposts' => 10,
'offset'=> 1,
'category' => $category->term_id,
'exclude' => $post->ID
);
$posts = get_posts($args);
foreach($posts as $pz) { ?>
<li>
<?php
$title = $pz->post_title;
$link = get_permalink($pz->ID);
printf('<a class="linkpost" title="%s" href="%s">%s</a>', $title, $link, $title);
echo get_the_post_thumbnail($pz->ID, 'thumbnail');
echo '<div id="excerptcu">';
echo substr($pz->post_content, 0, 25);
echo '</div>';
?>
<p class="more-link-wrapper2"><?php _e( 'Opširnije »', 'fearless' ); ?></p>
</li>
<?php } // end foreach ?>
</ul>
<?php } // end if
} //end foreach
?>
You can use continue to skip the loop of current category. Compare the selected category's id in the loop and skip the loop.
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’) ),