I am unable to integrate my slider inside wordpress - php

I have built a slider using html and css. I want to integrate it into wordpress. My slider works fine. I have added the css and JS code in custom css and JS snippet plugin. While I am placing below php code inside my theme's function.php file. Now the first image gets displayed but others are not. Also slider is kind of messed up.
I hardcoded the html in functions.php file and the slider works fine. Can someone please check my php code below to see what am i doing wrong? (css and JS are working fine. im unable to integrate html in wordpress properly).
Also Im displaying the slider on page by putting below code inside a shortcode which is working.
I am using Custom Post Type UI plugin to get images. I have created 2 posts with featured images. Now im trying to get those two featured images into my slider.
<?php //functions.php file
function mySlider(){
$args = array(
'post_type' => array('mx-slides'),
'post_status' => array('publish'),
'posts_per_page' => array('-1'),
'order' => array('DESC'),
);
$query = new WP_Query( $args );
if($query-> have_posts()){
?>
<!-- Container for the image gallery -->
<div class="container">
<div class="mySlides">
<div class="numbertext">6 / 6</div>
<?php while ( $query->have_posts() ) {
$query->the_post();
$img = the_post_thumbnail();
?>
<?php For($i=0;$i<=2;$i++) {
?>
<img src="<?php $img; ?>" style="width:100%;">
<?php } ?>
<?php } ?>
</div>
<!-- Next and previous buttons -->
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
<!-- Image text -->
<div class="caption-container">
<p id="caption"></p>
</div>
<!-- Thumbnail images -->
<div class="row">
<?php while ( $query->have_posts() ) {
$query->the_post();
$img = the_post_thumbnail();
?>
<?php // foreach ($img as $imgx)
For($i=0;$i<=5;$i++)
{
?>
<div class="column">
<img class="demo cursor" src="<?php echo $img; ?>" style="width:100%" onclick="currentSlide(1)" alt="No Image">
</div>
<?php } ?>
<?php } ?>
</div>
</div>
<?php
}
wp_reset_postdata();
}
add_shortcode('mx-slider', 'mySlider');
?>
?>

Please check with my fixed $arg. the format of posts_per_page is integer.
<?php //functions.php file
function mySlider() {
$args = array(
'post_type' => 'mx-slides',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'DESC',
);
$query = new WP_Query($args);
if ($query->have_posts()) {
?>
<!-- Container for the image gallery -->
<div class="container">
<div class="mySlides">
<div class="numbertext">6 / 6</div>
<?php while ($query->have_posts()) {
$query->the_post();
$img = get_the_post_thumbnail_url();
?>
<?php for ($i = 0; $i <= 2; $i++) {
?>
<img src="<?php echo $img; ?>" style="width:100%;">
<?php } ?>
<?php } ?>
</div>
<!-- Next and previous buttons -->
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
<!-- Image text -->
<div class="caption-container">
<p id="caption"></p>
</div>
<!-- Thumbnail images -->
<div class="row">
<?php while ($query->have_posts()) {
$query->the_post();
$img = get_the_post_thumbnail_url();
?>
<?php // foreach ($img as $imgx)
for ($i = 0; $i <= 5; $i++) {
?>
<div class="column">
<img class="demo cursor" src="<?php echo $img; ?>" style="width:100%" onclick="currentSlide(1)" alt="No Image">
</div>
<?php } ?>
<?php } ?>
</div>
</div>
<?php
}
wp_reset_postdata();
}
add_shortcode('mx-slider', 'mySlider');

Related

How to add an image to an archived page from single

There is an archive page and a singe page for Custom Post Type. On a single page, I display an image using ACF. How can I display it on the archive page?
// single post img
<div class = "speaker_img" >
<img src = "<?php the_field('speaker_img'); ?>" >
</div>
//archive page
<?php
$args = array(
'post_type' => 'speakers',);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
$loop->the_post(); ?>
<li><a href="#">
<div class="speaker-img">
<img src="/wp-content/uploads/2022/08/speaker-1.png"> </div>
<div class="speaker-name">
<p>
<?php the_title(); ?>
</p>
</div>
<div class="speaker-city">
<p>
Fribourg
</p>
</div>
</a></li>
<?php } ?>

ACF repeater field not rendering the rows

I have this website that I'm working on for a client. I jumped into the project and I have to create the accordion on the template file for specific pages. I just started getting deeper into PHP so I found a solution online in this article https://wpbeaches.com/create-an-acf-repeater-accordion-in-wordpress/
Now, my problem is this:
I created the repeater field with two subfields https://prnt.sc/xfg3lv and I choose that it only shows on this page template https://prnt.sc/xfg6lw
Then I created the fields on the actual page with that template https://prnt.sc/xfgdhp and inserted that piece of code from the article into the template file. Of course, I modified the names with my field names. The problem is that on the front it is not showing the rows even if they exist and just skips to the else statement. I will paste the complete code of that template page so if anyone can help it would be awesome. I'm busting my head for two days now on this issue.
The code (I marked where my code starts and ends):
<?php
/**
* Template Name: Services
*
**/
global $post;
$post_slug = $post->post_name;
$grandParent_title=get_page(get_ancestors($post->ID,'page')[0] )->post_title;
$grandParent_Image=wp_get_attachment_url( get_post_thumbnail_id(get_ancestors($post->ID,'page')[0] )) ;
get_header();
global $wp;
$current_slug = add_query_arg( array(), $wp->request );
$url_slug=explode('/', $current_slug);
$uri_string=$url_slug[1];
$_SESSION['current_url']=$uri_string;
function find_string_in_array ($arr, $string) {
return array_filter($arr, function($value) use ($string) {
return strpos($value, $string) !== false;
});
}
if(find_string_in_array ($url_slug, 'poly')==true)
{
$nav_theme_location='polystyrene';
$BannerClass='';
$post_type='polystyrene';
$galleryClass='';
}elseif(find_string_in_array ($url_slug, 'fiber')==true){
$nav_theme_location='fiberglass';
$BannerClass='fiberbanner';
$post_type='fiberglass_type';
$galleryClass='fiber';
}elseif (find_string_in_array ($url_slug, 'wood')==true) {
$nav_theme_location='woodwork';
$BannerClass='woodworkbanner';
$post_type='woodworks';
$galleryClass='wood';
}
elseif (find_string_in_array ($url_slug, 'creative')==true) {
$nav_theme_location='creative_production';
$BannerClass='creativebanner';
$post_type='creative_services';
$galleryClass='creative';
}elseif (find_string_in_array ($url_slug, 'f-and-b')==true) {
$nav_theme_location='fb';
$BannerClass='fandbbanner';
$post_type='creative_services';
$galleryClass='';
}else{
$nav_theme_location='';
$BannerClass='';
$post_type='';
$galleryClass='';
}
?>
<section class="banner inner-banner <?= $BannerClass;?>">
<!-- <div class="bannerBox" style="background-image: url(<?php // echo get_the_post_thumbnail_url())?get_the_post_thumbnail_url():;?>)"> -->
<div class="bannerBox" style="background-image: url(<?= (get_the_post_thumbnail_url()!='')?get_the_post_thumbnail_url():$grandParent_Image;?>)">
<div class="bannerContent">
<div class="overlay"></div>
<h1><?= $grandParent_title;?></h1>
</div>
</div>
</section>
<section class="secondrynavigation">
<div class="container">
<div class="innerMenu">
<?php
$args=array(
'theme_location'=>$nav_theme_location,
'container' =>false,
'menu_class' =>'',
);
wp_nav_menu($args);
?>
</div>
</div>
</section>
<section class="pad">
<div class="container-fluid">
<div class="innerHeading">
<h3><?php the_title();?></h3>
<p><?php
$content_post = get_post($post->ID);
echo $content = $content_post->post_content;?></p>
</div>
</div>
<div class="gallery <?= $galleryClass?>">
<div class="container">
<div class="row">
<?php
// echo $post_type;exit;
$loops = new WP_Query(array('posts_per_page'=> -1,
'post_type' => $post_type,
'orderby' => 'id',
'order' => 'asc',
'category_name'=>$uri_string));
// echo "<pre>";
// print_r($loops);exit;
if($loops->have_posts()):
while ($loops->have_posts()) :
$loops->the_post();
global $post;
$post_slug = $post->post_name;
?>
<div class="col-lg-4">
<a href="<?= get_permalink();?>">
<div class="galleryBox">
<div class="overlay">
<p><i>Read More</i></p>
</div>
<div class="image" style="background-image: url(<?= get_the_post_thumbnail_url();?>)"></div>
<div class="galleryText">
<h5><?php the_title();?></h5>
</div>
</div>
</a>
</div>
<?php endwhile;
endif;?>
</div>
</div>
</div>
</section>
<!--MY CODE-->
<?php
if( have_rows('services_accordion') ):
// loop through the rows of data for the tab header
while ( have_rows('services_accordion') ) : the_row();
// $title = get_sub_field('services_title');
// $description = get_sub_field('servises_description');
?>
<button class="accordion"><?php echo get_field('services_title'); ?></button>
<div class="panel">
<p><?php echo get_field('services_description'); ?></p>
</div>
<?php
endwhile;
else :
echo 'In progress';
endif; ?>
<!--MY CODE END-->
<?php get_footer();?>
In the end this is the result on the page https://prnt.sc/xfh0z7.
Thanks in advance!
Try wrapping your content into:
<?php while (have_posts()) : the_post(); ?>
// all your content related to the post including repeater code.
<?php endwhile; // End of the loop.?>
This way repeater will be in scope of your current post/page. Alternatively you can indicate post id as an argument in the repeater function.

php template code - all properties page display issue

My page: http://kevinkang.ca/en_US/all-properties/ was broken yesterday.
But I can't find any problem on these. Can anyone help me to find out the problem?
<div class="grid-x all-properties-page">
<?php
$args = array( 'category__not_in' => array( 4, 8, 9, 10 ) , 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $i=0; ?>
<div class="medium-12 cell padding-top-page">
<div class="home-data description-title-price">
<div class="grid-container">
<div class="grid-x">
<div class="medium-6 cell">
<?php the_title(); ?>
</div>
<div class="medium-6 cell text-right">
<?php the_field('price'); ?>
</div>
</div>
</div>
</div>
<a href="<?php the_permalink(); ?>"><div class="grid-x">
<ul class="images3">
<?php
//Get the images ids from the post_metadata
$images = acf_photo_gallery('3-images', $post->ID);
$img3=0;
if( count($images) ):
//Cool, we got some data so now let's loop over it
foreach($images as $image):
$id = $image['id']; // The attachment id of the media
$title = $image['title']; //The title
$caption= $image['caption']; //The caption
$full_image_url= $image['full_image_url']; //Full size image url
$full_image_url = acf_photo_gallery_resize_image($full_image_url, 1562, 760); //Resized size to 262px width by 160px height image url
$thumbnail_image_url= $image['thumbnail_image_url']; //Get the thumbnail size image url 150px by 150px
$url= $image['url']; //Goto any link when clicked
$target= $image['target']; //Open normal or new tab
$alt = get_field('photo_gallery_alt', $id); //Get the alt which is a extra field (See below how to add extra fields)
$class = get_field('photo_gallery_class', $id);
$img3++;
?>
<?php if( !empty($url) ){ ?><a href="<?php echo $url; ?>" <?php echo ($target == 'true' )? 'target="_blank"': ''; ?>><?php } ?>
<li style="background-image: url(<?php echo $full_image_url; ?>);" class="img-list<?php echo $img3; ?>">
<?php if( !empty($url) ){ ?></a><?php } ?></li>
<?php endforeach; endif; ?>
</ul>
<?php foreach ( get_the_category() as $value) {
$i++; ?> <h3 class="cat<?php echo $i; ?> <?php echo $value->name; ?>"><?php echo $value->name;} ?></h3>
</div></a>
<div class="grid-container pr-address-bg">
<div class="address-cont price-address">
<?php the_field('price'); ?>
<?php the_field('address_first_line'); ?>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
I see you have a lot of problems in your HTML ( inside , multiple tags. First check your HTML with https://validator.w3.org/. It looks the main parts of the problems come from header.php. I would have a look.

CPT and ACF Repeater not working together

Here's the page template code I'm working with, defined into sections...
<?php
//* Template Name: Partnerships Archive
?>
<!-- Header =========================================== -->
<?php get_header(); ?>
<!-- Homepage Header Video =========================================== -->
<div class="subpage-video-sca">
<!-- VIDEO -->
<div class="subpage-desktop-vid-sca">
<video title="<?php the_field('seo_video_title'); ?>" autoplay loop muted playsinline>
<source src="<?php the_field('vimeo_link'); ?>" type="video/mp4">
</video>
</div>
<!-- GRAD-OVERLAY -->
<div class="subpage-overlay-image-sca">
</div>
<!-- OVERLAID TEXT -->
<div class="subpage-text-over-video-sca">
<div>
<h1><?php the_field('sub_page_title'); ?></h1>
<p><?php the_field('paragraph'); ?></p>
</div>
</div>
</div>
<!-- Breadcrumbs =========================================== -->
<?php echo do_shortcode("[breadcrumbs]"); ?>
<!-- Main Content =========================================== -->
<div class="wrap">
<?php
$args = array(
'post_type' => 'partnerships',
'orderby' => 'title',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a class="one-third partnership-block" href="<?php the_permalink(); ?>">
<img src="<?php the_field('logo'); ?>" alt="<?php the_title();?> graphic">
</a>
<?php $products_count = $the_query->current_post + 1; ?>
<?php if ( $products_count % 4 == 0): ?>
</div><div class="row">
<?php endif; ?>
<?php endwhile; endif; ?>
</div>
<!-- Testimonials =========================================== -->
<div class="wrap">
<div class="page-section-headers pt-testimonials-container txt-right">
<h1>WHAT <span class="red">OUR PARTNERS</span><br>HAVE TO SAY</h1>
</div>
<?php if( have_rows('testimonial') ): ?>
<div class="testimonial-slider-container slick-slider">
<?php while( have_rows('testimonial') ): the_row();
// vars
$text = get_sub_field('testimonial_text');
$client = get_sub_field('client_name');
$company = get_sub_field('client_company');
?>
<div class="testimonial-slider-single">
<p><?php echo $text; ?></p>
<h2><?php echo $client; ?></h2>
<h3><?php echo $company; ?><h3>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
</div>
The code in the main content area is pulling through custom post types I have.
The code in the Testimonials area is pulling through data from an ACF Repeater field for this page specifically.
They both work independently but when I have them both on the page at the same time, the testimonials doesn't pull through.
That said, Ive just noticed that when I put the testimonials first and the main content after, they both work!!
Can anyone help? Is there something I haven't closed properly in the Main content or something? I don't get it...
It's wordpress, using Genesis Framework, latest versions of both. The page is here: http://staging.seedcreativeacademy.co.uk/partnerships/
add wp_reset_postdata(),after while :
<div class="wrap">
<?php
$args = array(
'post_type' => 'partnerships',
'orderby' => 'title',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a class="one-third partnership-block" href="<?php the_permalink(); ?>">
<img src="<?php the_field('logo'); ?>" alt="<?php the_title();?> graphic">
</a>
<?php $products_count = $the_query->current_post + 1; ?>
<?php if ( $products_count % 4 == 0): ?>
</div><div class="row">
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>

How can we use slider inside a loop

I need a slider working inside a loop for that I am using the below code
while ( have_posts() ) : the_post(); ?>
<div id="content">
<?php
$attachments = get_children( array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'order' => '',
'post_mime_type' => 'image')
);
echo "aaaaa".count($attachments);
?>
<?php if(count($attachments)!=0){ ?>
<div id="portfolio-gallery">
<ul id="slideshow_detail">
<?php
foreach ( $attachments as $att_id => $attachment ) {
$getimage = wp_get_attachment_image_src($att_id, 'room-gallery', true);
$roomimage = $getimage[0];
echo '<li>
<h3></h3>
<span>'.$roomimage.'</span>
<p></p>
<img src="'.$roomimage.'" alt="" class="thumb-slide" />
</li>';
}
?>
</ul>
<div id="wrapper1">
<div id="fullsize">
<div id="imgprev" class="imgnav" title="Previous Image"></div>
<div id="imglink"></div>
<div id="imgnext" class="imgnav" title="Next Image"></div>
<div id="image"></div>
<div id="information">
<h3></h3>
<p></p>
</div>
</div>
<div id="thumbnails">
<div id="slideleft" title="Slide Left"></div>
<div id="slidearea">
<div id="slider-room"></div>
</div>
<div id="slideright" title="Slide Right"></div>
</div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
<!--
$('slideshow_detail').style.display='none';
$('wrapper1').style.display='block';
var slideshow_detail=new TINY.slideshow_detail("slideshow_detail");
window.onload=function(){
slideshow_detail.auto=true;
slideshow_detail.speed=5;
slideshow_detail.link="linkhover";
slideshow_detail.info="information";
slideshow_detail.thumbs="slider-room";
slideshow_detail.left="slideleft";
slideshow_detail.right="slideright";
slideshow_detail.scrollSpeed=4;
slideshow_detail.spacing=25;
slideshow_detail.active="#fff";
slideshow_detail.init("slideshow_detail","image","imgprev","imgnext","imglink");
}
//-->
</script>
</div>
<?php endwhile;?>
By using this code slider is working only once;I need a slider that working under a loop .
ie I have to add multiple post,each post having multiple images .I have to show details of each post and showing images of each post using a slider
I feel maybe the while loop, beginning at the top could be the problem. Why not use an if statement. I'm not familiar with your "while" syntax. I've only ever seen the following:
<?php while( someCondition() ): ?>
//statements in here
<?php endwhile; ?>
Or maybe try an if statement instead:
<?php if ( have_posts() ): ?>
//all slider code here
<?php } else { ?>
//give a 'there are no posts' message
<?php endif; ?>
also what wordpress plugin are you running? Without knowing that, it's a bit difficult to trouble shoot this problem.

Categories