Wordpress query category name and related posts - wordpress

I am trying to query Wordpress custom posts and categories they are related to.
Query acts strangely, displays category name and all customs posts, not even related to that category (keeps repeating till all categorie names are displayed.)
Example image : Wordpress custom query display
My query code:
<div id="page-content-wrapper">
<div class="container-fluid">
<div class="lookbook-header">
<div class="wrap">
<p class="text-left">lookbook</p>
</div>
</div>
<?php
$taxonomy = 'lookbook_categories';
$terms = get_terms($taxonomy);
$args=array(
'taxonomy' => 'lookbook_categories'
'post_type' => 'lookbook',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if ( $terms && !is_wp_error( $terms ) ) :
foreach ( $terms as $term ) { ?>
<div class="lookbook-category">
<p class="text-center">
<?php echo $term->name; ?>
</p>
</div>
<?php
if( $my_query->have_posts() ) {
echo '';
$count=0;
while ($my_query->have_posts()) : $my_query->the_post();
if($count == 3) {?>
<div class="row">
<?php }
?>
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4 no-padding">
<div class="lookbook-item">
<div class="hvrbox">
<?php
$image = get_field('lookbook_image');
if( !empty($image) ): ?> <img src="<?php echo $image['url']; ?>" alt="news" class="img-responsive" />
<?php endif; ?>
<div class="hvrbox-layer_top">
<div class="hvrbox-text">
<div class="separator"></div>
<h3><?php the_title();?></h3>
<div class="separator"></div>
<p>
<?php the_field('excerpt');?>
</p>
</div>
</div>
</div>
</div>
</div>
<?php
$count++;
if($count == 3) echo '</div>';
endwhile;
}
}
endif;
wp_reset_query();
?>
</div>
</div>
I would like to display category name + posts related to category (not all posts over and over again)

You want to pass in an array to get_terms like:
$terms = get_terms( array(
'taxonomy' => 'lookbook_categories',
'hide_empty' => false,
) );
you can read more about it here: https://developer.wordpress.org/reference/functions/get_terms/

Related

Problem showing post for the term of the current page Wordpress

I'm trying to list the posts related to the current taxonomy page but it shows me all the posts instead. When I output the current term name is correct so really I don't know why this happens. I'm pasting here the code if anyone can put me in the right direction.
<?php
$term = get_queried_object();
// Define the query
$args1 = array(
'post_type' => 'properties-for-sale',
'taxonomy' => $term->term_id
);
$query = new WP_Query($args1);
if ($query->have_posts()) { ?>
<h2 class="region-listing-title"><span class="thin-span">Luxury Properties for sale in</span> <?php echo $term->name; ?></h2>
<div class="swiper myswiper swiper-h">
<div class="swiper-wrapper">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="swiper-slide">
<div class="favorite-button"><?php the_favorites_button($post_id, $site_id); ?></div>
<div class="swiper myswiper2 swiper-v">
<div class="swiper-wrapper">
<?php
$images = get_field('listing_gallery');
if ($images) : ?>
<?php foreach ($images as $image) : ?>
<div class="swiper-slide highlight-img" style="background-image: url(<?php echo $image['url']; ?>)"></div>
<?php endforeach; ?>
<?php endif;
?>
</div>
<div class="swiper-pagination"></div>
</div>
<div class="listing-informations">
<div id="lp-region-sale" class="listing-information-price">
<span><?php the_field('property_price'); ?> €</span>
</div>
<div>
<?php the_title(); ?>
</div>
<div class="listing-informations__listing-details">
<div class="listing-information-label"><span></span><?php the_field('number_of_bedrooms'); ?> Beds</div>
<div class="listing-information-label"><span></span><?php the_field('number_of_bathrooms'); ?> Baths</div>
<div class="listing-information-label"><span></span><?php the_field('interior_square_meters'); ?> Int Sqm</div>
<div class="listing-information-label"><span></span><?php the_field('lot_square_meters'); ?> Lot Sqm</div>
</div>
</div>
</div>
<?php endwhile;
} ?>
</div>
</div>
<?php wp_reset_postdata();
?>
Try with:
<?php
$term = get_queried_object()->term_id;
// Define the query
$args1 = array(
'post_type' => 'properties-for-sale',
'taxonomy' => $term
);
$query = new WP_Query($args1);
if ($query->have_posts()) { ?>
<h2 class="region-listing-title"><span class="thin-span">Luxury Properties for sale in</span> <?php echo $term->name; ?></h2>
<div class="swiper myswiper swiper-h">
<div class="swiper-wrapper">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="swiper-slide">
<div class="favorite-button"><?php the_favorites_button($post_id, $site_id); ?></div>
<div class="swiper myswiper2 swiper-v">
<div class="swiper-wrapper">
<?php
$images = get_field('listing_gallery');
if ($images) : ?>
<?php foreach ($images as $image) : ?>
<div class="swiper-slide highlight-img" style="background-image: url(<?php echo $image['url']; ?>)"></div>
<?php endforeach; ?>
<?php endif;
?>
</div>
<div class="swiper-pagination"></div>
</div>
<div class="listing-informations">
<div id="lp-region-sale" class="listing-information-price">
<span><?php the_field('property_price'); ?> €</span>
</div>
<div>
<?php the_title(); ?>
</div>
<div class="listing-informations__listing-details">
<div class="listing-information-label"><span></span><?php the_field('number_of_bedrooms'); ?> Beds</div>
<div class="listing-information-label"><span></span><?php the_field('number_of_bathrooms'); ?> Baths</div>
<div class="listing-information-label"><span></span><?php the_field('interior_square_meters'); ?> Int Sqm</div>
<div class="listing-information-label"><span></span><?php the_field('lot_square_meters'); ?> Lot Sqm</div>
</div>
</div>
</div>
<?php endwhile;
} ?>
</div>
</div>
<?php wp_reset_postdata();
?>
Just use tax query.
<?php
$term = get_queried_object()->term_id;
$args = array(
'post_type' => 'properties-for-sale',
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'term_id',
'terms' => $term->term_id
)
)
);
$query = new WP_Query( $args ); ?>

Taxonomy Custom field image is not showing

I want to show taxonomy custom field images on loop on the home page
"developers" is taxonomy name
and "developer_logo" is image field slug
I used that code only name is showing but image is not showing
<?php
$terms = get_terms( array( 'taxonomy' => 'developers', 'hide_empty' => false ) );
$image = get_field('developer_logo');
foreach ( $terms as $term ) : ?>
<div class="col-lg-4 col-md-6">
<div class="product product-zoom product--card">
<div class="product__thumbnail">
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo $term->name; ?>">
</div>
<div class="product-desc">
<h4 class="text-center mb-2"><?php echo $term->name; ?></h4>
</div>
</div>
</div>
<?php
endforeach
?>
I just want to show the images of taxonomy custom field
**In the below code, i have added the term Id as second parameter in get_field() function to get image URL & replaced the $image variable from outside of loop to inside the foreach loop.**
<?php
$terms = get_terms(array(
'taxonomy' => 'developers',
'hide_empty' => false
));
foreach ($terms as $term) {
$image = get_field('developer_logo', $term->id); ?>
<div class="col-lg-4 col-md-6">
<div class="product product-zoom product--card">
<div class="product__thumbnail">
<img src="<?php echo $image; ?>" alt="<?php echo $term->name;?>">
</div>
<div class="product-desc">
<h4 class="text-center mb-2"><?php echo $term->name;?></h4>
</div>
</div>
</div>
<?php
}
?>

WordPress get posts by category_name

I have a CPT in the single-portfolio.php file where I would like to list blog posts according to their category.
That is, the items I have in my CPT called Portfolio are "India", "USA", "Mexico", "Spain" and "Italy". The blog categories are exactly the same.
How can I get the news in the India page to return to me with the India category? And on the USA page the news with category USA...etc
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => 'usa'
);
$query = new WP_Query( $args );
?>
<section class="news-slider">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="slider-container">
<div class="slick-slider">
<?php while( $query->have_posts() ) : $query->the_post() ?>
<div class="slick-slide" style="background-image:url(<?= the_post_thumbnail_url() ?>)">
<a class="permalink" href="<?= the_permalink() ?>"></a>
<div class="title-container">
<span class="meta-category"><?php the_category( ', ' ); ?></span>
<h3 class="title"><?php the_title() ?></h3>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
</div>
</div>
</div>
</div>
</section>
If I understood your question, you are trying to show regular Posts on your CPT post where the name of the CPT Post is the same as the posts category name.
If that's the case you can do this (new lines are commented):
<?php
// Get the post object;
global $post;
// Get the post_name (e.g. slug) of the post
$slug = $post->post_name;
// As long as the slug of your CPT post is the same as the slug of your post category, this works.
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
// Use the $slug variable here.
'category_name' => $slug,
);
$query = new WP_Query( $args );
?>
<section class="news-slider">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="slider-container">
<div class="slick-slider">
<?php while( $query->have_posts() ) : $query->the_post() ?>
<div class="slick-slide" style="background-image:url(<?= the_post_thumbnail_url() ?>)">
<a class="permalink" href="<?= the_permalink() ?>"></a>
<div class="title-container">
<span class="meta-category"><?php the_category( ', ' ); ?></span>
<h3 class="title"><?php the_title() ?></h3>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
</div>
</div>
</div>
</div>
</section>

Vc addon not displaying properly as adde

I have made the visual composer addon to meet one of the requirement. However I have found that it always come above the other vc elements.
I have tried to add the class as per vc composer other elements
public function sim_product($attr)
{
?>
<div class="vc_column-inner"><div class="wpb_wrapper">
<div class="wpb_text_column wpb_content_element ">
<div class="wpb_wrapper">
<div class="more-project-list">
<?php
$select = $attr['sim_product_id'];
$query = new WP_Query( array( 'post_type' => 'products','post__in' => explode(",",$select) ));
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() )
{ $query->the_post(); ?>
<div class="single-blk" style='background-color: <?php echo the_field('colour');?>'>
<div class="title">
<?php the_title();?>
</div>
<figure>
<?php if (has_post_thumbnail( $query->ID ) )
{
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $query->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" alt="">
<?php }?>
</figure>
<div class="category">
<?php
$terms = wp_get_post_terms( $query->post->ID, array( 'Product_category') );
foreach ( $terms as $term ) { echo $term->name;}
?>
</div>
</div>
<?php }}?>
</div>
</div></div></div></div>
<?php
}
It isnt coming where I have placed it it always come on top

WP Query Multiple Shortcodes not working on same page or template page

I created a Shortcode slider which gets ids of different pages and show display slider. Shordcode works fine but issue is that whenever i am copy past shortcode multiple times on same page/ template page it shows only first one.
This issue occurs only only when i past same type of shortcode but if i past any other shortcode on same page it works fine.
Here is my code
add_shortcode( 'objectx-pages-list', 'objectx_pages_list_func' );
function objectx_pages_list_func( $atts ) {
global $post;
ob_start();
extract( shortcode_atts( array('ids' => '1186'), $atts ) );
$id_array = explode(',', $ids);
$pages_query = new WP_Query( array(
'post_type' => 'page',
'post__in' => $id_array,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $pages_query->have_posts() ) { ?>
<div class="carousel-wrapper">
<div class="owl-carousel owl-theme carousel-1" id="carousel-rooms">
<?php while ( $pages_query->have_posts() ) : $pages_query->the_post();
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div <?php post_class('item'); ?> id="post-<?php the_ID(); ?>">
<div class="row">
<div class="col-md-7">
<div class="img-rooms">
<a href="<?php the_permalink(); ?>">
<img class="img-responsive wp-post-image" src="<?php echo $featured_image; ?>"></a>
</div>
</div>
<div class="col-md-5">
<div class="detail-rooms">
<h2 class="title-room "><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php $myvariable_pages = ob_get_clean();
wp_reset_postdata();
return $myvariable_pages;
}
}
Here is shortcode
[objectx-pages-list id="15,16,17"]
[objectx-pages-list ids="25,26,27"]
here you can see live example
http://objextheme.wpengine.com/
This one working fine
ROOFTOP PATIO & LOUNGE
but this is not working
This Week At Vertigo Sky Lounge
Please guide me where i am doing mistake. Thanks
add_shortcode( 'objectx-pages-list', 'objectx_pages_list_func' );
function objectx_pages_list_func( $atts ) {
global $post;
ob_start();
extract( shortcode_atts( array('ids' => '1186'), $atts ) );
$id_array = explode(',', $ids);
$pages_query = new WP_Query( array(
'post_type' => 'page',
'post__in' => $id_array,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $pages_query->have_posts() ) { ?>
<div class="carousel-wrapper">
<div class="owl-carousel owl-theme carousel-1" id="carousel-rooms">
<?php while ( $pages_query->have_posts() ) : $pages_query->the_post();
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div <?php post_class('item'); ?> id="post-<?php the_ID(); ?>">
<div class="row">
<div class="col-md-7">
<div class="img-rooms">
<a href="<?php the_permalink(); ?>">
<img class="img-responsive wp-post-image" src="<?php echo $featured_image; ?>"></a>
</div>
</div>
<div class="col-md-5">
<div class="detail-rooms">
<h2 class="title-room "><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php $myvariable_pages = ob_get_clean();
wp_reset_postdata();
return $myvariable_pages;
}
}
Here is the error i noticed. id="carousel-rooms" Id repeating on
same page. that is why only one time it runs perfect.

Resources