admin管理员组文章数量:1023263
I have a query that checks for a custom field set on that page and then creates a carousel to loop them with a background image.
What I then want to do, is query a post type that uses a custom taxonomy with the same values as the custom field. Basically, for example if I've set the custom field 'homepage-fruit' as 'apples', but I also have a custom taxonomy called 'fruit' that has 'apples' as one of its values.
Essentially every time the user clicks 'next' on the carousel I want to run this query again, so changing the top carousel to show 'bananas' runs a new query for posts tagged with 'bananas' as their fruit. I hope this makes sense?
I think I might need some combination of AJAX and javascript but I'm very new to using AJAX so haven't quite got the hang of it yet. My instinct would be to store the 'homepage-fruit' as a variable using javascript each time the slider arrows are clicked, however as far as I know you can't use a javascript variable when you're making a query with PHP because of the order that everything runs, so that won't work.
Below is a code snippet of what I've got so far:
<ul class="slider owl-carousel" id="homepagelistcarousel">
<?php if( have_rows('homepage_fruit') ):
$query1 = new WP_Query( $args );
while ( have_rows('homepage_fruit') ) : the_row();
$fruit = get_sub_field('fruit');
$opener = get_sub_field('opener');
?>
<li class="homepagefruitlistitem" data-fruittype="<?php echo $fruit; ?>" style="color:white;padding:20px;background-image:url('<?php the_sub_field('bg_img'); ?>')">
<div class="openertextblock">
<span class="post-title" style="font-size:3rem;line-height:80px;"><?php echo $fruit;?></span>
<span class="txt-style-2" style="text-transform:none;font-size:1.5rem"><?php echo $opener; ?></span></div></li>
<?php endwhile;
else :
// no rows found
wp_reset_postdata();
endif;?>
</ul>
/*owl-carousel adds nav-arrows here*/
<div id="content">
<ul class="postslider owl-carousel" id="post-carousel">
<?php
$query2 = new WP_Query( $args2 );
$args2 = array(
'post_type' => 'healthy-eating-guide',
'category_name' => $cat_slug,
'posts_per_page' => 10,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
'tax_query' => array(
array(
'taxonomy' => 'fruit',
'field' => 'name',
'terms' => 'apples',
)
)
);
if($query2->have_posts()) :
while($query2->have_posts()) :
$query2->the_post();
?>
<li>
<img class="post-slider-img" data-id="<?php the_ID();?>" src="<?php the_post_thumbnail_url();?>"><a href="<?php the_permalink();?>"><span class="post-title"><?php the_title();?></span></a></li>
<?php endwhile; endif;?>
</ul>
</div>
Edit: I should note here I'm not asking for people to do my work for me! I've just been going around in circles a bit trying to find the correct solution to this, so advice on what would be the best approach is welcomed!
I have a query that checks for a custom field set on that page and then creates a carousel to loop them with a background image.
What I then want to do, is query a post type that uses a custom taxonomy with the same values as the custom field. Basically, for example if I've set the custom field 'homepage-fruit' as 'apples', but I also have a custom taxonomy called 'fruit' that has 'apples' as one of its values.
Essentially every time the user clicks 'next' on the carousel I want to run this query again, so changing the top carousel to show 'bananas' runs a new query for posts tagged with 'bananas' as their fruit. I hope this makes sense?
I think I might need some combination of AJAX and javascript but I'm very new to using AJAX so haven't quite got the hang of it yet. My instinct would be to store the 'homepage-fruit' as a variable using javascript each time the slider arrows are clicked, however as far as I know you can't use a javascript variable when you're making a query with PHP because of the order that everything runs, so that won't work.
Below is a code snippet of what I've got so far:
<ul class="slider owl-carousel" id="homepagelistcarousel">
<?php if( have_rows('homepage_fruit') ):
$query1 = new WP_Query( $args );
while ( have_rows('homepage_fruit') ) : the_row();
$fruit = get_sub_field('fruit');
$opener = get_sub_field('opener');
?>
<li class="homepagefruitlistitem" data-fruittype="<?php echo $fruit; ?>" style="color:white;padding:20px;background-image:url('<?php the_sub_field('bg_img'); ?>')">
<div class="openertextblock">
<span class="post-title" style="font-size:3rem;line-height:80px;"><?php echo $fruit;?></span>
<span class="txt-style-2" style="text-transform:none;font-size:1.5rem"><?php echo $opener; ?></span></div></li>
<?php endwhile;
else :
// no rows found
wp_reset_postdata();
endif;?>
</ul>
/*owl-carousel adds nav-arrows here*/
<div id="content">
<ul class="postslider owl-carousel" id="post-carousel">
<?php
$query2 = new WP_Query( $args2 );
$args2 = array(
'post_type' => 'healthy-eating-guide',
'category_name' => $cat_slug,
'posts_per_page' => 10,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
'tax_query' => array(
array(
'taxonomy' => 'fruit',
'field' => 'name',
'terms' => 'apples',
)
)
);
if($query2->have_posts()) :
while($query2->have_posts()) :
$query2->the_post();
?>
<li>
<img class="post-slider-img" data-id="<?php the_ID();?>" src="<?php the_post_thumbnail_url();?>"><a href="<?php the_permalink();?>"><span class="post-title"><?php the_title();?></span></a></li>
<?php endwhile; endif;?>
</ul>
</div>
Edit: I should note here I'm not asking for people to do my work for me! I've just been going around in circles a bit trying to find the correct solution to this, so advice on what would be the best approach is welcomed!
Share Improve this question edited May 9, 2019 at 13:09 Hannah Johnson asked May 9, 2019 at 12:41 Hannah JohnsonHannah Johnson 11 bronze badge 1- Figured it out using Ajax! Still having some trouble getting the right attribute to pull through because it's a generated carousel, but otherwise got the linking to work. – Hannah Johnson Commented May 9, 2019 at 15:40
1 Answer
Reset to default -1Solved by creating an Ajax function in functions.php and an Ajax.js file to insert the carousel, then destroying and reinitialising owl-carousel
I have a query that checks for a custom field set on that page and then creates a carousel to loop them with a background image.
What I then want to do, is query a post type that uses a custom taxonomy with the same values as the custom field. Basically, for example if I've set the custom field 'homepage-fruit' as 'apples', but I also have a custom taxonomy called 'fruit' that has 'apples' as one of its values.
Essentially every time the user clicks 'next' on the carousel I want to run this query again, so changing the top carousel to show 'bananas' runs a new query for posts tagged with 'bananas' as their fruit. I hope this makes sense?
I think I might need some combination of AJAX and javascript but I'm very new to using AJAX so haven't quite got the hang of it yet. My instinct would be to store the 'homepage-fruit' as a variable using javascript each time the slider arrows are clicked, however as far as I know you can't use a javascript variable when you're making a query with PHP because of the order that everything runs, so that won't work.
Below is a code snippet of what I've got so far:
<ul class="slider owl-carousel" id="homepagelistcarousel">
<?php if( have_rows('homepage_fruit') ):
$query1 = new WP_Query( $args );
while ( have_rows('homepage_fruit') ) : the_row();
$fruit = get_sub_field('fruit');
$opener = get_sub_field('opener');
?>
<li class="homepagefruitlistitem" data-fruittype="<?php echo $fruit; ?>" style="color:white;padding:20px;background-image:url('<?php the_sub_field('bg_img'); ?>')">
<div class="openertextblock">
<span class="post-title" style="font-size:3rem;line-height:80px;"><?php echo $fruit;?></span>
<span class="txt-style-2" style="text-transform:none;font-size:1.5rem"><?php echo $opener; ?></span></div></li>
<?php endwhile;
else :
// no rows found
wp_reset_postdata();
endif;?>
</ul>
/*owl-carousel adds nav-arrows here*/
<div id="content">
<ul class="postslider owl-carousel" id="post-carousel">
<?php
$query2 = new WP_Query( $args2 );
$args2 = array(
'post_type' => 'healthy-eating-guide',
'category_name' => $cat_slug,
'posts_per_page' => 10,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
'tax_query' => array(
array(
'taxonomy' => 'fruit',
'field' => 'name',
'terms' => 'apples',
)
)
);
if($query2->have_posts()) :
while($query2->have_posts()) :
$query2->the_post();
?>
<li>
<img class="post-slider-img" data-id="<?php the_ID();?>" src="<?php the_post_thumbnail_url();?>"><a href="<?php the_permalink();?>"><span class="post-title"><?php the_title();?></span></a></li>
<?php endwhile; endif;?>
</ul>
</div>
Edit: I should note here I'm not asking for people to do my work for me! I've just been going around in circles a bit trying to find the correct solution to this, so advice on what would be the best approach is welcomed!
I have a query that checks for a custom field set on that page and then creates a carousel to loop them with a background image.
What I then want to do, is query a post type that uses a custom taxonomy with the same values as the custom field. Basically, for example if I've set the custom field 'homepage-fruit' as 'apples', but I also have a custom taxonomy called 'fruit' that has 'apples' as one of its values.
Essentially every time the user clicks 'next' on the carousel I want to run this query again, so changing the top carousel to show 'bananas' runs a new query for posts tagged with 'bananas' as their fruit. I hope this makes sense?
I think I might need some combination of AJAX and javascript but I'm very new to using AJAX so haven't quite got the hang of it yet. My instinct would be to store the 'homepage-fruit' as a variable using javascript each time the slider arrows are clicked, however as far as I know you can't use a javascript variable when you're making a query with PHP because of the order that everything runs, so that won't work.
Below is a code snippet of what I've got so far:
<ul class="slider owl-carousel" id="homepagelistcarousel">
<?php if( have_rows('homepage_fruit') ):
$query1 = new WP_Query( $args );
while ( have_rows('homepage_fruit') ) : the_row();
$fruit = get_sub_field('fruit');
$opener = get_sub_field('opener');
?>
<li class="homepagefruitlistitem" data-fruittype="<?php echo $fruit; ?>" style="color:white;padding:20px;background-image:url('<?php the_sub_field('bg_img'); ?>')">
<div class="openertextblock">
<span class="post-title" style="font-size:3rem;line-height:80px;"><?php echo $fruit;?></span>
<span class="txt-style-2" style="text-transform:none;font-size:1.5rem"><?php echo $opener; ?></span></div></li>
<?php endwhile;
else :
// no rows found
wp_reset_postdata();
endif;?>
</ul>
/*owl-carousel adds nav-arrows here*/
<div id="content">
<ul class="postslider owl-carousel" id="post-carousel">
<?php
$query2 = new WP_Query( $args2 );
$args2 = array(
'post_type' => 'healthy-eating-guide',
'category_name' => $cat_slug,
'posts_per_page' => 10,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
'tax_query' => array(
array(
'taxonomy' => 'fruit',
'field' => 'name',
'terms' => 'apples',
)
)
);
if($query2->have_posts()) :
while($query2->have_posts()) :
$query2->the_post();
?>
<li>
<img class="post-slider-img" data-id="<?php the_ID();?>" src="<?php the_post_thumbnail_url();?>"><a href="<?php the_permalink();?>"><span class="post-title"><?php the_title();?></span></a></li>
<?php endwhile; endif;?>
</ul>
</div>
Edit: I should note here I'm not asking for people to do my work for me! I've just been going around in circles a bit trying to find the correct solution to this, so advice on what would be the best approach is welcomed!
Share Improve this question edited May 9, 2019 at 13:09 Hannah Johnson asked May 9, 2019 at 12:41 Hannah JohnsonHannah Johnson 11 bronze badge 1- Figured it out using Ajax! Still having some trouble getting the right attribute to pull through because it's a generated carousel, but otherwise got the linking to work. – Hannah Johnson Commented May 9, 2019 at 15:40
1 Answer
Reset to default -1Solved by creating an Ajax function in functions.php and an Ajax.js file to insert the carousel, then destroying and reinitialising owl-carousel
本文标签: phpWPQuery based on another query on the page
版权声明:本文标题:php - WP_Query based on another query on the page 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745509340a2153753.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论