I need to echo the category slug for each post for the data-filter value. Can someone advise what I'm doing wrong?
<div id="container" class="isotope">
<?php
$args = array(
'post_type' => 'Photos',
);
$_posts = new WP_Query($args);
?>
<?php
if ( $_posts->have_posts() ) : while ( $_posts->have_posts() ) : $_posts->the_post(); ?>
<!-- Here is the issue -->
<div class="grid-item" data-filter="
<?php
$categories = get_the_category(get_the_id());
foreach ($categories as $category){
echo $category->slug.' ';}?>"
>
<a onClick='showDialog()' data-target="#lightbox">
<img src="<?php the_field('image'); ?>" alt="">
</a>
</div>
<?php
endwhile; endif;
?>
</div>
Please change code like this
<?php
$categories = get_the_category(get_the_ID());
foreach ($categories as $category){
$slug = $category->slug;
?>
<div class="grid-item" data-filter="<?php echo $slug; ?>">
<?php
}
?>
Related
I have been trying to customize my site but I have met a problem... As I have stated in the title, what shall I add in order to make it possible? I will like the make the category with the latest post move to the first. I have tried for 5 hours and still failed to do it. Please teach me how to fix it.
<?php
//Get the desired categories and order by ID
$cat_args = array(
'orderby' => 'id'
);
//For each category show a random post
$categories = get_categories($cat_args);
foreach ($categories as $category) {
?>
<?php
$post_args = array(
'numberposts' => 1,
'category' => $category->term_id,
);
$posts = get_posts($post_args);
foreach ($posts as $post) {
?>
<article <?php post_class('post-list animated fadeIn'); ?> role="article">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<figure class="eyecatch<?php if (!has_post_thumbnail()) : ?> noimg<?php endif; ?>">
<?php the_post_thumbnail('home-thum'); ?>
<?php archivecatname(); ?>
</figure>
<section class="entry-content cf">
<h1 class="h2 entry-title"><?php the_title(); ?></h1>
<div class="byline entry-meta vcard">
<?php if (get_option('post_options_authordisplay', 'author_off') == 'author_on') : ?><span class="writer name author"><?php echo get_avatar(get_the_author_meta('ID'), 30); ?><span class="fn"><?php the_author(); ?></span></span><?php endif; ?>
</div>
<div class="description"><?php the_excerpt(); ?></div>
</section>
</a>
</article>
<?php get_template_part('loop'); ?>
<?php
}
}
?>
Query Arguments
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Running the Query
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
I have created a Custom Post Type product and for this CPT I've also created a taxonomy with the name products_types.
Now on my overview page of all the products I would like to echo out the product type that was given to the product. But I keep getting bool(false).
My code:
<div class="row">
<?php
$loop = new WP_Query( array( 'post_type' => 'product') );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-4 col-lg-3 work">
<div class="category">
<?php
$category = get_the_terms('product', 'products_types');
var_dump($category);
echo $category;
?>
</div>
<a href="<?php the_permalink() ?>" class="work-box"> <img src="<?= get_field('image'); ?>" alt="">
<div class="overlay">
<div class="overlay-caption">
<p><?php echo the_title() ?></p>
</div>
</div>
</a>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
Anyone can help me out here please?
You need to pass the Post ID or object in first parameter of get_the_terms(). Used get_the_ID() which return the post ID.
Example:
foreach (get_the_terms(get_the_ID(), 'products_types') as $cat) {
echo $cat->name;
}
How to print taxonomy terms of custom post type in WordPress loop?
<div class="row">
<?php
$loop = new WP_Query( array( 'post_type' => 'product') );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-4 col-lg-3 work">
<div class="category">
<?php
$terms = get_the_terms( get_the_ID(), 'products_types' );
if ( $terms && ! is_wp_error( $terms ) ) :
$category_links = array();
foreach ( $terms as $term ) {
$category_links[] = $term->name;
}
$categories = join( ", ", $category_links );
?>
<?php printf( esc_html( $categories ) ); ?>
<?php endif; ?>
</div>
<a href="<?php the_permalink() ?>" class="work-box"> <img src="<?= get_field('image'); ?>" alt="">
<div class="overlay">
<div class="overlay-caption">
<p><?php echo the_title() ?></p>
</div>
</div>
</a>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
So I have a php page in wordpress and when I add a part of the code it doesn't work. So to explain a bit more. I have a php template and most of it works, but when I add some additional code at the bottom that doesn't show up.
<?php get_header(); ?>
<div class="main_color container_wrap_first container_wrap">
<menu id="nav">
<ul><?php
$category_id = 2;
while( $category_id < 7 ) { ?>
<li>
<a href="<?php echo get_category_link( $category_id ); ?>">
<?php
$cat_id = $category_id;
echo get_cat_name( $cat_id );
$category_id++;
?>
</a>
</li>
<?php } ?>
</ul>
</menu>
<?php
$args = array(
'post_type' => 'projekti',
'posts_per_page' => -1,
'category' => '4',
);
$posts = get_posts($args);
if( $posts ):
$i = 1; ?>
<div class="custom-posts-grid">
<?php foreach($posts as $post): setup_postdata( $post ); ?>
<?php if( have_rows('logotipi') ): ?>
<?php while( have_rows('logotipi') ): the_row();
// vars
$image = get_sub_field('thumbnail_for_logotipi');
$content = get_sub_field('project_name');
$link = get_sub_field('url_logotipi');
$count = count($posts);
?>
<div class="post-grid-logotipi">
<a href="<?php echo $link; ?>">
<div class="post-title-hover"><?php echo $content ?></div>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" class="image-overlay-post" />
</a>
</div>
<?php
$i++;
if( $i == 6 ):
$i++;
?>
<div class="sodelujmo-post-grid"><div class="sodelujmo-post"><p class="sodelujmo-text">Vam je všeč,<br>kar vidite?<br>...<p><div class="button-bg"><a class="sodelujmo-link" href="../u3nek/sodelujmo/">Sodelujmo</a>
</div></div></div>
<?php
endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<!-- after this nothing I add can be shown -->
</div>
</div>
I have no idea what is wrong. So if anyone encountered anything similar before do let me know.
this is my code
<?php
$portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
foreach($portfolio_categories as $portfolio_category)
echo '<li data-filter=".' .$portfolio_category->slug. '">' .$portfolio_category->name. '</li> ';
?>
this shows the filters created by the category's name inside $portfolio_categories
then, there is the creation of client's list
<ul id="list" class="portfolio_list clearfix responsive">
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' ) );
?>
<?php if (have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php $terms = wp_get_object_terms($post->ID, 'portfolio_category'); ?>
<?php $pf_bimg = wp_get_attachment_url( get_post_thumbnail_id() );?>
<?php $pf_simg = aq_resize( $pf_bimg, 420, 450, true ); ?>
<li class="span3 list_item <?php foreach ($terms as $term) { echo $term->slug.' '; } ?>">
<div class="recent-item">
<figure>
<div class="touching medium">
<img src="<?php echo $pf_simg; ?>" alt="<?php the_title(); ?>" />
<!-- <i class="icon-search"></i>
<i class="icon-link"></i> -->
</div>
<figcaption class="item-description">
<h5><?php the_title(); ?></h5>
<span><?php $i = 0; foreach ($terms as $term) { if($i)echo " / "; echo $term->name; $i=1; } ?></span>
</figcaption>
</figure>
</div>
</li>
<?php endwhile; ?> <?php endif; ?>
<?php wp_reset_query();?>
</ul>
What i 'm trying to do is to display a different element for every different category .
I thought to insert an if condition before the tag , like this
<?php
$portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
if ($portfolio_category = 'category1'): ?>
<figure>1</figure>
<?php else: ?>
<figure>2</figure>
<?php endif
?>
Thank all for your time,
Dan
You need to store last category, otherwise you'll have <figure> tag before each row:
<?php
$portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
?>
<?php if ($portfolio_category != $prev_portfolio_category): ?>
<?php if ($portfolio_category = 'category1'): ?>
<figure>1</figure>
<?php else: ?>
<figure>2</figure>
<?php endif; ?>
<?php endif; ?>
<?php
$prev_portfolio_category = $portfolio_category;
?>
How can I get the custom post type category link in the while have_posts loop?
Here is the code I am using to get the category name:
<?php query_posts(array(
'post_type' => 'portfolio'
)); ?>
<?php while (have_posts()):
the_post(); ?>
<?php $feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>
<div class="col-md-3 col-md-3-portfolio">
<div class="portfoliobox">
<img src="<?php echo $feat_image; ?>" />
<div class="portfolio-title-block">
<h2><?php the_title(); ?></h2>
<?php
$terms = get_the_terms($post->ID, 'portfolio-category');
if ($terms && !is_wp_error($terms)):
$tslugs_arr = array();
foreach ($terms as $term)
{
$tslugs_arr[] = $term->slug;
}
$terms_slug_str = join(" ", $tslugs_arr);
endif;
$idObj = get_category_by_slug('portfolio-category');
$id = $idObj->term_id;
?>
<p><?php echo $terms_slug_str; ?></p>
</div>
</div>
</div>
<?php
endwhile; ?>