Calling variable inside loop based on post count - wordpress

I am creating a shortcode in a wordpress theme which needs to show properties on area based it was working fine. I have tried to keep 10 area title which was custom one. For that i called varable as $area_title1 to 10 in the wordpress loop also i have added a counter and called the counter with the variable for getting $area_title1 i called variable as $area_title.$counter which was without counting it shows exactly the value for example 1 , 2, etc my code is below.
function home_category_with_image_session($atts){
ob_start();
$atts = shortcode_atts(
array(
'propertytype' => '',
'area' => '',
'area1' => '',
'area2' => '',
'area3' => '',
'area4' => '',
'area5' => '',
'area6' => '',
'area7' => '',
'area8' => '',
'area9' => '',
'area_title' => '',
'area1_title' => '',
'area2_title' => '',
'area3_title' => '',
'area4_title' => '',
'area5_title' => '',
'area6_title' => '',
'area7_title' => '',
'area8_title' => '',
'area9_title' => '',
),
$atts,
'home_category_type'
);
$area=$atts['area'];
$area1=$atts['area1'];
$area2=$atts['area2'];
$area3=$atts['area3'];
$area4=$atts['area4'];
$area5=$atts['area5'];
$area6=$atts['area6'];
$area7=$atts['area7'];
$area8=$atts['area8'];
$area9=$atts['area9'];
$area_title=$atts['area_title'];
$area_title1=$atts['area1_title'];
$area_title2=$atts['area2_title'];
$area_title3=$atts['area3_title'];
$area_title4=$atts['area4_title'];
$area_title5=$atts['area5_title'];
$area_title6=$atts['area6_title'];
$area_title7=$atts['area7_title'];
$area_title8=$atts['area8_title'];
$area_title9=$atts['area9_title'];
$property_type=$atts['propertytype'];
$custom_terms = get_terms(array ('taxonomy' => 'property_city_taxonomy',
'orderby' => 'count',
'order' => 'ASC',
'name' => array ($area, $area1, $area2, $area3, $area4, $area5, $area6, $area7, $area8, $area9 ),
) );
$counter = -1;
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => $property_type,
'tax_query' => array(
array(
'taxonomy' => 'property_city_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts())
{
$counter++ ?>
<div class="col-md-3 col-sm-4 col-xs-6"><a href="<?php echo get_term_link($custom_term); ?>?property-type=<?php echo "$property_type"; ?>"><div class="top-locations"><div class="image-holder">
<?php $custom_term_id = $custom_term->term_id;
$custom_term_meta = get_term_meta( $custom_term_id, 'uploadimage_61032', true );
$num = $loop->post_count;
$area_title_display = $area_title.$counter;
?>
<img src="<?php echo $custom_term_meta; ?>" alt="<?php echo $term->name; ?>" class="img-responsive" />
<?php echo $area_title_display; ?>
<div class="home-banner-overlay"></div>
<div class="area-name"><?php echo $custom_term->name; ?></div>
</div><div class="city-text-container text-center"><?php echo $num; ?> Properties Listed</div></div></a></div>
<?php
} }
// Restore original post data.
wp_reset_postdata();
return ob_get_clean(); }
add_shortcode('home_category_type', 'home_category_with_image_session');
//Featured Post Home
function home_featured_posts($atts){
ob_start();
$atts = shortcode_atts(
array(
'no' => '',
'posttype' => '',
),
$atts,
'home-featured'
);
?>
<div class="session-featured-title">
<h5>Featured Property <?php echo $atts['posttype']; ?></h5>
<div class="related-control"><a class="btn prev"><i class="fa fa-arrow-left"></i></a><a class="btn next"><i class="fa fa-arrow-right"></i></a></div>
</div>
<div id="featured" class="owl-carousel owl-theme">
<?php
$args_1= array(
'post_type' => $atts['posttype'],
'posts_per_page' => $atts['no'],
'meta_query' => array(
array(
'key' => 'we_recommend_make-featured-property',
'value' => '1'
)
)
);
query_posts($args_1); if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="item"><a href="<?php the_permalink(); ?> " title="<?php the_title_attribute(); ?>">
<div class="featured-image">
<?php the_post_thumbnail('full', array('class' => 'img-responsive') ); ?></div>
<div class="featured-text-holder">
<div class="recent-post-title"><?php the_title(); ?></div>
<div class="featured-meta-detail">
<div class="py-list-price"><?php ro_price(); ?></div>
<div class="py-list-bed"><?php ro_bedroom(); ?></div>
<div class="py-list-bath"><?php ro_bathroom(); ?></div>
</div>
</div>
</a></div>
<?php endwhile; endif; wp_reset_query(); ?>
</div>

Here is your questions answer in php Refer This
And modified code of yours is below.
function home_category_with_image_session($atts){
ob_start();
$atts = shortcode_atts(
array(
'propertytype' => '',
'area' => '',
'area1' => '',
'area2' => '',
'area3' => '',
'area4' => '',
'area5' => '',
'area6' => '',
'area7' => '',
'area8' => '',
'area9' => '',
'area_title' => '',
'area1_title' => '',
'area2_title' => '',
'area3_title' => '',
'area4_title' => '',
'area5_title' => '',
'area6_title' => '',
'area7_title' => '',
'area8_title' => '',
'area9_title' => '',
),
$atts,
'home_category_type'
);
$area=$atts['area'];
$area1=$atts['area1'];
$area2=$atts['area2'];
$area3=$atts['area3'];
$area4=$atts['area4'];
$area5=$atts['area5'];
$area6=$atts['area6'];
$area7=$atts['area7'];
$area8=$atts['area8'];
$area9=$atts['area9'];
$area_title[0]=$atts['area_title'];
$area_title[1]=$atts['area1_title'];
$area_title[2]=$atts['area2_title'];
$area_title[3]=$atts['area3_title'];
$area_title[4]=$atts['area4_title'];
$area_title[5]=$atts['area5_title'];
$area_title[6]=$atts['area6_title'];
$area_title[7]=$atts['area7_title'];
$area_title[8]=$atts['area8_title'];
$area_title[9]=$atts['area9_title'];
$property_type=$atts['propertytype'];
$custom_terms = get_terms(array ('taxonomy' => 'property_city_taxonomy',
'orderby' => 'count',
'order' => 'ASC',
'name' => array ($area, $area1, $area2, $area3, $area4, $area5, $area6, $area7, $area8, $area9 ),
) );
$counter = -1;
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => $property_type,
'tax_query' => array(
array(
'taxonomy' => 'property_city_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts())
{
$counter++ ?>
<div class="col-md-3 col-sm-4 col-xs-6"><a href="<?php echo get_term_link($custom_term); ?>?property-type=<?php echo "$property_type"; ?>"><div class="top-locations"><div class="image-holder">
<?php $custom_term_id = $custom_term->term_id;
$custom_term_meta = get_term_meta( $custom_term_id, 'uploadimage_61032', true );
$num = $loop->post_count;
$area_title_display = '$area_title'.$counter;
echo $area_title[$counter];
?>
<img src="<?php echo $custom_term_meta; ?>" alt="<?php echo $term->name; ?>" class="img-responsive" />
<?php echo $area_title_display; ?>
<div class="home-banner-overlay"></div>
<div class="area-name"><?php echo $custom_term->name; ?></div>
</div><div class="city-text-container text-center"><?php echo $num; ?> Properties Listed</div></div></a></div>
<?php
}
}
// Restore original post data.
wp_reset_postdata();
return ob_get_clean(); }
add_shortcode('home_category_type', 'home_category_with_image_session');
//Featured Post Home
function home_featured_posts($atts){
ob_start();
$atts = shortcode_atts(
array(
'no' => '',
'posttype' => '',
),
$atts,
'home-featured'
);
?>
<div class="session-featured-title">
<h5>Featured Property <?php echo $atts['posttype']; ?></h5>
<div class="related-control"><a class="btn prev"><i class="fa fa-arrow-left"></i></a><a class="btn next"><i class="fa fa-arrow-right"></i></a></div>
</div>
<div id="featured" class="owl-carousel owl-theme">
<?php
$args_1= array(
'post_type' => $atts['posttype'],
'posts_per_page' => $atts['no'],
'meta_query' => array(
array(
'key' => 'we_recommend_make-featured-property',
'value' => '1'
)
)
);
query_posts($args_1); if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="item"><a href="<?php the_permalink(); ?> " title="<?php the_title_attribute(); ?>">
<div class="featured-image">
<?php the_post_thumbnail('full', array('class' => 'img-responsive') ); ?></div>
<div class="featured-text-holder">
<div class="recent-post-title"><?php the_title(); ?></div>
<div class="featured-meta-detail">
<div class="py-list-price"><?php ro_price(); ?></div>
<div class="py-list-bed"><?php ro_bedroom(); ?></div>
<div class="py-list-bath"><?php ro_bathroom(); ?></div>
</div>
</div>
</a></div>
<?php endwhile; endif; wp_reset_query(); ?>
</div>
I am sure this will works for you.

Related

Wordpress: authors page with custom query

On a wordpress site I am coding an authors page. Therefore I use a custom query that I also use in several other parts of the site. Partly it works fine, but as it has a major bug, I cannot use it:
I receive not only a chosen autors post. All posts of all authors are listet.
If I use the main loop, I do not have that problem. But because of the pagination which should be the same everywhere, I would like to use the custom loop. Where is my mistake?
Here my code:
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
?>
<section class="tk_fullwidth">
<div id="primary" class="content-area">
<h1 class="page-title">Beiträge von: <?php echo $curauth->nickname; ?></h1>
<div id="main" class="site-main" role="main">
<div class="grid" data-masonry='{ "itemSelector": ".grid-item", "columnWidth": 285, "gutter": 20 }'>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '50',
'paged' => $paged,
'order' => 'DESC',
'orderby' => 'date',
) );
?>
<!-- begin loop -->
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="grid-item">
<a class="xyz" href="<?php the_permalink(); ?>"></a>
<h3 class="entry-title"><?php the_title(); ?></h3>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
}
?>
</div><!-- grid-item -->
<?php endwhile; ?>
<!-- end loop -->
<div class="tk_pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( '<', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( '>', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
</div><!-- #main -->
</div><!-- #primary -->
</section><!--tk_fullwidth-->

don't show a specific woocommerce category

I have this code for showing new product as hscroll in mobile theme
but I want to don't show a category product with 1230 id
I add think by adding some code like: $products->category-> != 1230 to first if
please guide
<?php
// new arrivals products
$new_args = array(
'post_type' => 'product',
'posts_per_page' => 8,
'orderby' =>'date',
'order' => 'DESC'
);
$products = new WP_Query( $new_args );
?>
<?php if ( $products->have_posts() ) { ?>
<div class="title-intro content-block-title"><?php _e( 'New Arrivals', 'woomobify' ); ?></div>
<div class="product-hscroll swiper-container swiper-init" data-auto-height="true" data-free-mode="true" data-slides-per-view="auto">
<div class="swiper-wrapper">
<?php while ( $products->have_posts() ) : $products->the_post(); global $product; ?>
<div class="swiper-slide">
<div class="card">
<div class="card-content">
<a href="<?= get_the_permalink(); ?>">
<?php
if ( has_post_thumbnail($products->post->ID) ) {
echo get_the_post_thumbnail( $products->post->ID, 'shop_catalog' );
} else {
echo '<img src="'.wc_placeholder_img_src().'"/>';
} ?>
</a>
<div class="title"><?php the_title(); ?></div>
<div class="item-text product-price">
<span class="price"><?= wc_price( $product->get_price() ); ?></span>
</div> </div> </div>
<?php endwhile; ?>
I think the solution you are looking for is described at
https://docs.woocommerce.com/document/exclude-a-category-from-the-shop-page/
or to make it more easy, use the next arguments to retrieve all products except if the product is in the category(id) 1230
$args = array(
'posts_per_page' => -1,
'orderby' =>'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array('1230'),
'operator' => 'NOT IN')));

Taxonomy images from taxonomy list in WordPress

I want to list all categories of my custom post type with its images. I tried many codes, but I can't get it working. With this given code I'm getting all categories as a list, but I'm not getting the value of my custom field. Someone please help me with this.
$post_type = 'product';
$tax = 'productcat';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args=array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$term_id = $tax_term->term_id;
$term_meta = get_option( 'taxonomy_' . $term_id );
$my_cf = $term_meta[ 'category_image' ];
echo $my_cf;
}
}
I think here is the answer for this problem.
<?php
foreach((get_the_category()) as $category) {
echo '<img src="http://example.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />';
}
?>
For more information please click here.
Another answer for it, it is also here. it might be useful for you.
<?php
$args = array( 'type' => 'product',
'child_of' => 16,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'product_cat',
'pad_counts' => false
);
$categories = get_categories( $args );
foreach($categories as $category):
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
?>
<div class="col-xs-6 col-sm-4 product-box"><img src="<?php echo $image; ?>"></div>
<?php
endforeach;
?>
Thank you so much for your code,
I got answer using you code, i did some modification also. this is the code
<div class="row">
<?php
$post_type = 'product';
$tax = 'productcat';
$args = array( 'type' => 'product',
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'productcat',
'pad_counts' => false
);
$categories = get_categories( $args );
foreach($categories as $category):
$term_id = $category->term_id;
$variable = get_field('category_image', 'productcat_'.$term_id);
$imageURL = $variable['sizes']['medium'];
?>
<div class="col-sm-4 alt-no-padding-r-x">
<div class="products_item">
<div class="product_head">
<p><?php echo $category->name; ?></p>
</div>
<div class="top-img">
<img src="<?php echo $imageURL; ?>" />
</div>
<div class="product_desc">
<p><?php echo $category->description; ?></p>
</div>
<div class="product_list text-left no-decor">
<ul>
<?php
$args=array(
'post_type' => $post_type,
"$tax" => $category->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();?>
<li><?php the_title(); ?> </li>
<?php endwhile;
}
?>
</ul>
</div>
<div class="readmore-product no-decor">
Read More
</div>
</div>
<div class="plan-shadow"></div>
</div>
<?php
endforeach;
wp_reset_query();
?>
</div>

wordpress: Have trouble displaying comments on single post from another post

I've an unique situation where I need to display same comments for 3 different posts. This 3 different posts are obviously linked together in DB. So what I need to do is to force use of 1 post ID for all languages to submit comment and retrieve it... instead of using 3 diff IDs I need to use 1 and stick to it.
I've $current_post_duplicate_id which is 9 for all 3 translated posts.
If I submit comment on original post which is originally post_ID 9 all works fine, I see comment and it gets submited. But when I switch to translated version nothing is displayed, but I'm forcing get_comments to use id 9 for displaying and submiting comments through $current_post_duplicate_id, so what is happening? why is not this working??
I cannot figure out what is happening, I figured some guru here could give me a hint.
<?php
//Uniting comments in all languages by setting default language for comments en in this case
$current_post_duplicate_id = icl_object_id (get_queried_object_id(), 'tribe_events', false, 'en');
?>
<div id="eventattend">
<?php if ('open' == $post->comment_status) : ?>
<span><?php _e('Are you intersted?'); ?></span>
<?php if ( $user_ID ) : ?>
<?php
$usercomment = get_comments( array( 'user_id' => $current_user->ID, 'post_id' => $current_post_duplicate_id ) );
if ( 1 <= count( $usercomment ) ) {
?>
<form method="post" action="">
<a class="eventno" href="javascript:;" onclick="parentNode.submit();"><?php _e('No'); ?></a>
<input type="hidden" name="delmycom" value="1" />
<?php
//
$delete_my_event_interest = $_POST['delmycom'];
//
if ( isset ($delete_my_event_interest) ) {
$current_user_comment = get_comments( array(
'post_id' => $current_post_duplicate_id,
'user_id' => get_current_user_id(),
'number' => 1,
'status' => 'approve',
'type' => 'comment'
) );
wp_delete_comment( $current_user_comment[0]->comment_ID );
}
//
?>
</form>
<?php } else { ?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post">
<?php comment_id_fields(); ?>
<?php do_action('comment_form', $current_post_duplicate_id); ?>
<input type="hidden" name="comment" value="I'm attending this Event!" />
<a class="eventyes" href="javascript:;" onclick="parentNode.submit();"><?php _e('Yes'); ?></a>
</form>
<?php } ?>
<?php else : ?>
<a class="eventyes loginosx" href="#"><?php _e('Yes'); ?></a>
<?php _e('*You need to be loged in to say YES.'); ?>
<?php endif; ?>
<?php endif; // if you delete this the sky will fall on your head ?>
<div class="clearfloat"></div><!-- Very Important -->
<!-- End #eventattend --></div>
<!-- End #eventsingle --></div>
<ul class="sidebarlists" id="interestedlist">
<li>
<h2><?php _e('Who\'s interested so far?'); ?> (<?php comments_number( '0', '1', '%' ); ?>)</h2>
<ul>
<?php
$recent_comments = get_comments( array(
'post_id' => $current_post_duplicate_id,
'number' => 25,
'status' => 'approve',
'type' => 'comment',
'order' => 'ASC'
) );
foreach ($recent_comments as $comment) {
?>
<li id="attendee-<?php echo $comment->comment_ID; ?>">
<?php echo get_avatar( $comment->comment_author_email, $size = '50', $alt = $comment->comment_author.' is attending this event' ); ?>
<span><?php echo $comment->comment_author; ?></span>
</li>
<?php
}
?>
</ul>
</li>
<!-- End #sidebarlists --></ul>
use this
<?php $comments = get_comments(array('post_id' => 1)); ?>
You can also pass the following parameters
<?php $defaults = array(
'author_email' => '',
'ID' => '',
'karma' => '',
'number' => '',
'offset' => '',
'orderby' => '',
'order' => 'DESC',
'parent' => '',
'post_id' => 0,
'post_author' => '',
'post_name' => '',
'post_parent' => '',
'post_status' => '',
'post_type' => '',
'status' => '',
'type' => '',
'user_id' => '',
'search' => '',
'count' => false,
'meta_key' => '',
'meta_value' => '',
'meta_query' => '',
); ?>

WordPress Getting Page Images

Having a little trouble here, I'm wanting to get all images from posts and display them in two separate locations, one location showing one image and the second location showing all the image.
If i use the Query it grabs all post image and with out using Query it grabs one image from the page. any ideas on how to get all images from page instead of posts???
<div id="fp_gallery" class="fp_gallery">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $args = array( 'numberposts' => 1, 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' =>
$post->ID ); $attachments = get_posts($args); if ($attachments) { foreach ( $attachments as $attachment ) { ?>
<img src="<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" alt="" class="fp_preview" style=""/>
<?php }}?>
<?php endwhile; endif; ?>
<div class="fp_overlay"></div>
<div id="fp_loading" class="fp_loading"></div>
<div id="fp_next" class="fp_next"></div>
<div id="fp_prev" class="fp_prev"></div>
<div id="outer_container">
<div id="thumbScroller">
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $args = array( 'numberposts' => 9999, 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts($args); if ($attachments) { foreach ( $attachments as $attachment ) { ?>
<div class="content">
<div>
<a href="#" title="<?php the_title(); ?>">
<img src="<?php echo get_bloginfo('template_url');?>/js/timthumb.php?src=<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>&h=120&w=150&zc=1" alt="<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" class="thumb"/>
</a>
</div>
</div>
<?php }}?>
<?php endwhile; endif; ?>
</div>
</div>
</div>
</div>
<div id="fp_thumbtoggle" class="fp_thumbtoggle" title="View Thumbs">↑</div>
Any help would be great : )
Make numberposts -1 it will collect all posts under category
$args = array( 'numberposts' => 1, 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' =>
$post->ID );
But i'm not sure about jQuery thing you are talking about.

Resources