i make two wp query loop one is within the loop something went wrong i cant figure out here is the code
the inner wp query for image cpt related with main query cpt.
$maincontest = new WP_Query(array(
'posts_per_page' => 3,
'post_type' => 'contest',
'post_status' => 'any'
));
while($maincontest->have_posts()){
$maincontest->the_post(); ?>
<div class = "listloopmain">
<a href="<?php the_permalink(); ?>">
<div class = "listcontestitem">
<div class = "listimage">
<?php
$toplogo = new WP_Query(array(
'posts_per_page' => 1,
'post_type' => 'logo',
'orderby' => 'rand'
));
while($toplogo->have_posts()){
$toplogo->the_post();
$image = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<div class ="listim">
<img src="<?php echo $image ?>"/>
</div>
<?php }
wp_reset_postdata();
?>
</div>
<div class = "title_des">
<div class = "listtitle">
<h4>
<?php the_title(); ?>
</h4>
</div>
<div class = "listdescription">
<?php echo wp_trim_words(get_the_content(), 20); ?>
</div>
</div>
<div class = "listcontestprice">
$ <?php the_field('price'); ?>
</div>
<div class = "listtime">
5 Days
</div>
</div>
</a>
</div>
<?php } ?>
</div>
<div class ="my-pagination">
<?php
echo paginate_links(array(
'prev_text' => __( '<' ),
'next_text' => __( '>' )
));
wp_reset_postdata();
?>
Problem is main query permalink is correct but content is same for each loop. Image query is working fine. Can anybody help?
Thanks in advance
hi i got the solution want main custom query only for showing private post. achieved by filtering pre_get_posts and set method. thank u all
function contest_adjust_query($query){
if (!is_admin() AND is_post_type_archive('contest') AND $query->is_main_query()) {
$query->set('post_status', 'private');
$query->set('post_status', array('publish', 'private'));
}
}
add_action('pre_get_posts', 'contest_adjust_query');
At this part of your code -
while($toplogo->have_posts()){
$toplogo->the_post();
$image = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<div class ="listim">
<img src="<?php echo $image ?>"/>
</div>
<?php }
**wp_reset_postdata();**
Use $toplogo->reset_postdata();
Reference for the reset_postdata function you can find here - https://developer.wordpress.org/reference/classes/wp_query/reset_postdata/.
Also, here's a nice article explaining how it's used - https://neliosoftware.com/blog/the-problems-of-using-nested-loops-in-wordpress/.
Related
I know this question was asked so many times, but none of the solutions are working for me.
I want to display my categories on the website with their thumbnails together.
I tried to use category imagies plugin, but for some reason this plugin stopped working and doesn't give me the opportunity to attach picture to the category (the add image button isn't shown in the category settings).
After this I created custom field, where I made a taxonomy is equal to category. With this I could pair images with categories, but I don't know how to add it to the code.
Here is my code:
<section id="categories">
<div class="center-box">
<div class="categories">
<?php
$categories = get_categories(array ('parent' => 0));
foreach($categories as $category) { ?>
<div class="category-box">
<a class="category-img" href="<?php echo get_category_link($category->term_id); ?>">
<img class="img" src="<?php echo z_taxonomy_image_url($category->term_id); ?>"/>
<div class="category-name">
<h3> <?php echo $category->name; ?> </h3>
</div>
</a>
</div>
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'date',
'order' => 'DESC',
'category' => $category->term_id, );
?>
<?php } ?>
</div>
</div>
</section>
It still has the previous category imagies plugin code which doesn't work anymore.
Someone could help me how to display the categories with image?
Thank you!
I'd suggest using the Advanced Custom Fields plugin for this, if you are not already. Under the working assumption that you are, you could do the following:
<section id="categories">
<div class="center-box">
<div class="categories">
<?php
$categories = get_categories(array ('parent' => 0));
foreach($categories as $category) { ?>
<div class="category-box">
<a class="category-img" href="<?php echo get_category_link($category->term_id); ?>">
<img class="img" src="<?php echo wp_get_attachment_image_src( get_field( 'thumbnail', "term_$category->term_id" ), 'small' ) ?>"/>
<div class="category-name">
<h3> <?php echo $category->name; ?> </h3>
</div>
</a>
</div>
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'date',
'order' => 'DESC',
'category' => $category->term_id, );
?>
<?php } ?>
</div>
</div>
How can i get dynamic data of image and title from this following code? I am currently working on this code but unable to retrieve data. I am attaching screenshot of slider where nothing is displaying as well as i am also adding screenshot of demo slider.
my front-page.php:
<?php $our_services = get_our_services_list(5); ?>
<div class="col-sm-12">
<div class="our-services-slider">
<?php
if(!empty($our_services) && count($our_services) > 0){
for($i=0; $i<count($our_services); $i++) {
?>
<div class="slider-tile">
<div class="tile-img">
<img src="<?php echo $services_image; ?>" alt="">
</div>
<div class="tile-text">
<h4>
<b><?php echo $post_title; ?></b>
</h4>
<p><?php echo $post_excerpt; ?></p>
<div class="text-center">
<a class="btn btn-mehroon" href="<?php echo $services_button_link; ?>">
<?php echo $services_button; ?></a>
</div>
</div>
</div>
<?php } } ?>
my functions.php:
function get_our_services_list($per_page_record = -1){
$args = array(
'posts_per_page' => $per_page_record,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'our_services'
);
return get_posts($args);
}
Demo page
my page:
you should try this :
<?php
$args = array(
'post_type' => 'client_testimonial',
'post_status' => 'publish',
'posts_per_page'=> 6,
'order' => 'DESC',
'orderby' => 'date'
);
$query = new WP_Query($args);
?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
// you can get image like this :
<?php $img = get_the_post_thumbnail_url(get_the_ID()); ?>
// get title like this:
echo get_the_title(get_the_ID());
I've created shortcode in the function to display promotions on page/post using shortcode in the editor.
//Testimonial shortcode
add_shortcode('promotions', 'adenPromotions');
function adenPromotions($attr, $content)
{
ob_start();
get_template_part('./templates/promotions-loop');
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
Here's the code in the template file I have.
<?php
if ($_SESSION['selectLocation'] != '') {
$args = array( 'post_type' => 'promotion',
'meta_key' => 'builder',
'posts_per_page' => 3,
'meta_value' => $_SESSION['selectLocation']);
} else {
$args = array( 'post_type' => 'promotion',
'posts_per_page' => 3,
'meta_value' => 0);
};
if ($_SESSION['selectLocation'] != ''):
?>
<div class="container-fluid px-0">
<div class="row">
<div class="col-md-12">
<div class="offer-section-page-ah">
<?php
$the_query = new WP_Query( $args );?>
<?php if ( $the_query->have_posts() ) : $i = 0; ?>
<?php while ( $the_query->have_posts() ) :
$the_query->the_post(); $i++ ?>
<div class="offer-loop-page block-<?php echo $i
?>">
<div class="offer-banner-ah"
style="background-image:url('<?php echo the_post_thumbnail_url('full') ;?>');"></div>
<div class="offer-right">
<h2 class="heading"><?php the_title() ;?></h2>
<div class="post-expirator-ah"><p><?php echo do_shortcode('[postexpirator]') ;?></p></div>
<div class="sub-heading"><p><?php the_excerpt() ;?></p></div>
<div class="more-btn-ah">Find out more</div>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<h2 class="heading text-center mb-0 m-0 py-4"> Sorry, no promotion available in your region.</h2>
<?php endif; ?>
</div>
</div>
</div>
get_permalink() should return the url of the post in the loop however it is returning the URL of the post where the shortcode is added, if that makes sense.
Hope the code makes sense. Here's the pastebin version if code here doesn't make sense. -> https://pastebin.com/MSNn6rKQ
Edit: Apparently the_excerpt() function was playing up, not sure why, removed that and it fixed the problem. Created ACF field for description instead.
May you should try this:
get_permalink(get_the_ID()) instead of get_the_permalink
In Post loop you can use get_the_ID() function..
With this function you can get ID of Post with in Loop..
And Use this ID to get Permalink of post in loop
$post_id = get_the_ID();
get_the_permalink($post_id);
To get information about the current post that your shortcode is used in, use:
global $post;
You can then
get_the_permalink($post->ID)
So basically, I'm working on a custom wordpress theme. What i'm trying to do is to set an icon for each category. If the loop starts and the post has a category, It'll show up with the icon that it has assigned. Right now it shows the correct icons, but the title and exerpt of the post keeps changing to the name of the page. Here is an example I have three posts math, english and history all of them have the correct icon, but display the name blog post page instead of math, english, or history.
<?php /* Template Name: News Blog Page */ get_header(); ?>
<div id="blog-post-wrapper" class="section_wrapper">
<div class="column three-fourth">
<?php $currentPage = get_query_var('paged');
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'posts_per_page' => 9,
'paged' => $currentPage
);
$the_query = new WP_Query($args);
if($the_query -> have_posts()):
while ($the_query -> have_posts()): $the_query -> the_post();
get_template_part('postloopcontent', get_post_format());
endwhile;
echo "<div class='pagination'>";
echo paginate_links(array(
'total' => $the_query -> max_num_pages
));
echo "</div>";
endif;
?>
</div>
<div class="column one-fourth">
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
the top one is my basic layout and it grabs my loop. the bottom one is my loop
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>
In your loop file, you're resting post data i.e. wp_reset_postdata(); outside the $imgquery loop/condition. If you could wrap the postdata rest function inside the condition, I think that should work.
You code must look like this
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
}
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>
I'm very new to WordPress, and I've been pulling hair trying to figure this one out.
I have a custom theme, the theme is very simple, which contains an image carousel built up from a collection of images in a gallery.
If I remove the carousel include file, the post ID on the rest of the page returns correctly, as soon as I add the carousel back, it uses the ID of the last category pulled in the code.
I removed all 'global' references, as I assumed this will override the ID for the rest of the page, but it's still wrong.
The code from the carousel.php file:
<div id="carousel">
<?php
$args = array(
'post_type' => 'gallery',
'post_status' => 'publish',
'name' => $wp_query->query_vars['name'],
'posts_per_page' => 1
);
$second_query = new WP_Query($args);
$gllr_options = get_option('gllr_options');
$gllr_download_link_title = addslashes(__('Download high resolution image', 'gallery'));
if ($second_query->have_posts()) : while ($second_query->have_posts()) : $second_query->the_post(); ?>
<div class="carousel-holder">
<?php
the_content();
$galleries = get_posts(array(
'showposts' => -1,
'what_to_show' => 'posts',
'post_status' => 'inherit',
'post_type' => 'attachment',
'orderby' => $gllr_options['order_by'],
'order' => $gllr_options['order'],
'post_mime_type' => 'image/jpeg,image/gif,image/jpg,image/png',
'post_parent' => $post->ID
));
if (count($galleries) > 0) { ?>
<ul id="carousel-gallery">
<?php foreach ($galleries as $attachment) {
$key = 'gllr_image_text';
$link_key = 'gllr_link_url';
$alt_tag_key = 'gllr_image_alt_tag';
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'photo-thumb' );
$image_attributes_large = wp_get_attachment_image_src( $attachment->ID, 'large' );
$image_attributes_full = wp_get_attachment_image_src( $attachment->ID, 'full' );
if ( 1 == $gllr_options['border_images'] ) {
$gllr_border = 'border-width: ' . $gllr_options['border_images_width'] . 'px; border-color:' . $gllr_options['border_images_color'] . '';
$gllr_border_images = $gllr_options['border_images_width'] * 2;
} else {
$gllr_border = '';
$gllr_border_images = 0;
}
if (($url_for_link = get_post_meta($attachment->ID, $link_key, true)) != "") { ?>
<li>
<img alt="<?php echo get_post_meta($attachment->ID, $alt_tag_key, true); ?>" title="<?php echo get_post_meta( $attachment->ID, $key, true ); ?>" src="<?php echo $url_for_link; ?>" />
</li>
<?php } else { ?>
<li rel="gallery_fancybox<?php if ( 0 == $gllr_options['single_lightbox_for_multiple_galleries'] ) echo '_' . $post->ID; ?>">
<img alt="<?php echo get_post_meta($attachment->ID, $alt_tag_key, true); ?>" title="<?php echo get_post_meta( $attachment->ID, $key, true ); ?>" src="<?php echo $image_attributes_large[0]; ?>" />
</li>
<?php }
$count_image_block++;
} ?>
</ul>
<div class="clearfix"></div>
<div id="arrows" class="arrows">
<a id="prev" class="prev" href="#"></a>
<a id="next" class="next" href="#"></a>
</div>
<div id="paginator" class="paginator"></div>
<?php } ?>
</div>
<?php endwhile; else: endif;?>
</div>
The code that is returning the erroneous ID:
<div class="left sidebar">
<?php echo the_ID(); ?>
</div>
This is within a template page, that looks like this:
<?php
/**
* Template Name: 2 Column Left
*/
get_header();
?>
<div id="maincontent">
<div id="content" class="site-content" role="main">
<div class="left sidebar">
<?php
echo the_ID();
?>
</div>
</div>
</div>
<?php get_footer(); ?>
Carousel.php is defined within header.php, and then gets called when get_header(); is called.
So, assuming I'm on the About page, with an ID of 16, and the carousel calls a gallery with page ID of 8, the echo in left-sidebar always returns 8.
I'm at a loss here, I've search high and low for an answer to this problem, but I've come up with nothing.
Try resetting the loop using the wp_reset_query() method.
Place this right after your carousel loop ends.
<?php wp_reset_query(); ?>