Jquery UI accordion? - php

I've managed to make the Jquery UI accordion work on the front page of my Wordpress website.
Problem is, instead of opening and closing the divs when I click on the headings, the effect happens when I click on random sections of the text, like clicking in the middle of a paragraph.
I'd like to make it so that if I click on a header (h2 or h3, etc.) it opens the section beneath the header. Right now, it seems to work when I click in the middle of paragraph, or sometimes it works when I click on a header but then it doesn't work when I click on the header the next time... I'm totally confused.
I always thought that it automatically worked with headers (similar to the accordion plug-in I have on another site). I can't find any info online that clears this up for me.
Here's my accordion script:
$(function() {
$( "#accordion" ).accordion({
});
});
The PHP code below shows the div I want the accordion to act on. You see, I'm pulling content into my front page from another page in Wordpress. Note the "accordion" div in there...
<section id="faq">
<div class="indent">
<?php
$query = new WP_Query( 'pagename=faq' );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<div class="entry-content">';
echo '<div id="accordion">'; //This the div that should be affected...
the_content();
echo '</div>';
echo '</div>';
}
}
/* Restore original Post Data */
wp_reset_postdata();
?>
</div>
</section>
Could the problem have something to do with fact that I'm pulling in content from another page?
BTW, I also have a couple of other scripts running in the same JS file as the accordion script, but I commented those out and the problem remained.
Here's how the whole JS file looks, just in case you'd like to see:
jQuery(document).ready(function($) {
/* Stick navigation to the top of the page */
var stickyNavTop = $('.main-navigation').offset().top;
$(window).scroll(function(){
var scrollTop = $(window).scrollTop();
if (scrollTop > stickyNavTop) {
$('.main-navigation').addClass('sticky-header');
$('.site-header').addClass('shifted');
} else {
$('.main-navigation').removeClass('sticky-header');
$('.site-header').removeClass('shifted');
}
});
/* Scroll to specific section on front page */
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: (target.offset().top - 50)
}, 1000);
return false;
}
}
});
});
/* Accordion effect */
$(function() {
$( "#accordion" ).accordion({
});
});
}); /* Ends the jquery declaration */

Your code is printing several #accordion divs, when it should be only one wrapping all the accordion items. It should be like:
if ( $query->have_posts() ) {
echo '<div id="accordion">';
while ( $query->have_posts() ) {
$query->the_post();
echo '<h3>'.get_the_title().'</h3>';
echo '<div class="entry-content">';
the_content();
echo '</div>';
}
echo '</div>';
}
That way you will have a code similar to the presented in the docs: one #accordion wrapping the items, the <h3> as titles and the <div> as the contents.

I solved the problem by wrapping the entire body of the page in the "accordion" div (leaving out just the header and the footer), and then making the script work on just H6 headers.
Thanks!

Related

How to reload a parent page on closing a overlay lightbox (wordpress)?

I need to refresh a parent page when closing a overlay lightbox (not popup window).
The lightbox has no close button, it's closed by clicking anywhere outside the lightbox.
I tried the following code, but it doesn't refresh the parent page.
Would you plase let me know how to refresh the parent page?
HTML:
<div class="dwqa-ask-question">
<a href="https://www.myweb.com/ask-questions/" rel="lightbox" data-lightbox-type="iframe">Ask Question
</a>
</div>
Lightbox:
<div class="nivo-lightbox-overlay nivo-lightbox-theme-default nivo-lightbox-effect-fade nivo-lightbox-open">
<div class="nivo-lightbox-wrap">
<div class="nivo-lightbox-content">
<iframe src="https://www.mycom.com/ask-questions/" class="nivo-lightbox-item" frameborder="0" vspace="0" hspace="0" scrolling="auto"></iframe>
</div>
<div class="nivo-lightbox-title-wrap"></div>
</div>
Previous
Next
Close
</div>
Added the following code in a lightbox page (=ask-questions page):
$(document).ready(function () {
if (window.opener) {
window.close();
}
if (window != top) {
top.location.replace(document.location);
}
});
jQuery(document).ready(function refreshParentWindow(){
window.opener.location.href="/{!$CurrentPage.parameters.id}";
window.top.close();
});
jQuery(document).ready(function (){
var rel = window.opener.location;
window.close();
rel.reload();
});
Thank you.
"Responsive Lightbox" is the plugin i'm using for the lightbox.
There are multiple ways to set this up. I personally always use additional css classes to show and/or hide my html elements on the page, but since you're currently using a plugin, then you could add an event listener to your page.
So use this javascript on your page:
jQuery(document).ready($ => {
console.log("You just loaded your js file onto the page"); // Testing whether you've loaded your js file correctly!
$(document).on("click", e => {
if (
document.querySelector('.nivo-lightbox-overlay').contains(e.target)
&&
!document.querySelector(".nivo-lightbox-wrap").contains(e.target)
) {
console.log("You just clicked outside of the lightbox wrap"); // Testing event listener
location.reload(); // a simple refresh
// OR
// location.reload(true); // a hard refresh
};
});
});
You see the orange area around your light-box wrap with nivo-lightbox-overlay class? Click anywhere on it an it'll reload your page!
Let me know if you were able to get it to work!
Another solution
jQuery(document).ready( $ => {
$(document).on("click", "div.nivo-lightbox-overlay", () => {
if ($("div.nivo-lightbox-overlay").length) {
location.reload(); // a simple refresh
// OR
// location.reload(true); // a hard refresh
}
})
});

ACF Repeater Field | Load More

I am following a Github code to add load more to ACF repeater field. I have 101 fields but load more not working. I tried to bebug too. Ajax response is 0. May be via ajax its not working. Do I have to add anything on functions.php file. Ajax response is 0.
<?php
/*
The code in this file is an example off the code that you would use in your template to
show the first X number of rows of a repeater
I'm sure there are more elegant ways to do the JavaScript, but what's here will work
*/
if (have_rows('gallery_work', 'option')) {
// set the id of the element to something unique
// this id will be needed by JS to append more content
$total = count(get_field('gallery_work', 'option'));
?>
<ul id="my-repeater-list-id">
<?php
$number = 2; // the number of rows to show
$count = 0; // a counter
while( have_rows('gallery_work', 'option') ):the_row();
//the_row();
$image_se_work = get_sub_field('image_se_work', 'option');
?>
<li><img src="<?php echo $image_se_work;?>" alt=""></li>
<?php
$count++;
if ($count == $number) {
// we've shown the number, break out of loop
break;
}
endwhile; // end while have rows
?>
</ul>
<!--
add a link to call the JS function to show more
you will need to format this link using
CSS if you want it to look like a button
this button needs to be outside the container holding the
items in the repeater field
-->
<a id="my-repeater-show-more-link" href="javascript:void(0);" onclick="my_repeater_show_more();"<?php
if ($total < $count) {
?> style="display: none;"<?php
}
?>>Show More</a>
<!--
The JS that will do the AJAX request
-->
<script type="text/javascript">
var my_repeater_field_post_id = <?php echo $post->ID; ?>;
var my_repeater_field_offset = <?php echo $number; ?>;
var my_repeater_field_nonce = '<?php echo wp_create_nonce('my_repeater_field_nonce'); ?>';
var my_repeater_ajax_url = '<?php echo admin_url('admin-ajax.php'); ?>';
var my_repeater_more = true;
function my_repeater_show_more() {
// make ajax request
$.post(
my_repeater_ajax_url, {
// this is the AJAX action we set up in PHP
'action': 'my_repeater_show_more',
'post_id': my_repeater_field_post_id,
'offset': my_repeater_field_offset,
'nonce': my_repeater_field_nonce
},
function (json) {
// add content to container
// this ID must match the containter
// you want to append content to
$('#my-repeater-list-id').append(json['content']);
// update offset
my_repeater_field_offset = json['offset'];
// see if there is more, if not then hide the more link
if (!json['more']) {
// this ID must match the id of the show more link
$('#my-repeater-show-more-link').css('display', 'none');
}
console.log(json);
},
'json'
);
}
console.log(<?php echo $total;?>);
</script>
<?php
} // end if have_rows
?>
You only have one file of a 2 file example here. This is the code that goes in the template. The PHP side of the this example is in the other PHP file https://github.com/Hube2/acf-dynamic-ajax-select-example/tree/master/repeater-ajax-load-more and includes the WP AJAX action that you need to add to your functions.php file.

How to load dynamic content (jquery) inside ajax call?

My homepage loads pages with jquery ajax call. In the Pictures subpage there is 3 div, and each loads a php with ajax, too. I would like to include a gallery js to every sub-subpage. However, the js does not loads. Here is my code in the index.php for the subpages:
<script type="text/javascript">
$(function()
{
var actualmenu = 'fooldal.html';
$('#tartalom').load('fooldal.html');
$('.fooldal').click(function()
{
$('#tartalom').load('fooldal.html');
actualmenu = 'fooldal.html';
}
);
$('.kepek').click(function(){
$('#tartalom').load('kepek.php', function(){
$(document.body).on('click', 'a[rel=gallery_view]' ,function(){
$(this).KeViewer('gallery', {'percent' : 70});
return false;
});
});
actualmenu = 'kepek.php';
});
});
</script>
And there is my kepek.php page:
<script type="text/javascript">
$(function()
{
$('#galeria').load('kepek_csenge.php');
$('#csenge').click(function()
{
$('#galeria').load('kepek_csenge.php');
}
);
);
</script>
<div id="albums">
<div class="album" id="csenge">
Csenge
<br/>
<img src="albumok/csenge/01.jpg" alt="csenge"/>
</div>
</div>
<div style="clear:both;"></div>
<div id="galeria" width="500"></div>
Inside kepek_csenge.php there are rows like this, which should trigger the gallery:
<a class="thumb_wrapper" href="albumok/csenge/01.jpg" title="Első" rel="gallery_view"><img alt="" class="thumb" src="albumok/csenge/thumbs/01.jpg" /></a>
Unfortunately it just loads a new page with the selected picture. How can i use the galleryviewer js in this page?
It is important to understand that the document you are loading into was ready long before you load anything with ajax
This means that the code wrapped in $(function() in your ajax pages will fire as soon as it is encountered. If that code precedes the html it references then it won't find those elements since they don't exist yet.
Try moving all the script tags in the ajax loaded content to after the html.
It is generally easier to put all the scripts into one and wrap different parts in functions and then just call those functions in the success callback of load();
function loadPage(page){
var url = '...' +page;
$('#selector').load(url, function(){
initViewer();
});
}

PHP trouble with current page name

I am creating menu, and if the specific page is current , menu item assign different color.
Simple situation.
So i do :
<?php $page = $_SERVER['SCRIPT_NAME'];?>
<?php if ($page == "index.php"){ echo "class='active'";} ?> >
Home
That works for every page, except one.
I tried some other options suggested on stackoverflow
How to get current PHP page name , like :
echo basename($_SERVER['PHP_SELF']); returns file_name.php
and
if(basename(__FILE__) == 'file_name.php') {
//Hide
} else {
//show
}
Still does not work.
$pagename = basename($_SERVER['PHP_SELF']);
if($pagename=='index.php'){
echo "class='active'";
}
According to the data you supplied I suggest to use client-side solution instead of server-side solution. I mean that you use javascript.
This is may be done as follows:
<ul id="menu">
<li>Home</li>
<li>About</li>
<li>Any Other</li>
</ul>
<script>
function setCurrentNavLink(id){
nav = document.getElementById(id);
links = nav.getElementsByTagName('a');
for (i = 0; i < links.length; i++){
if (links[i].href == location.href){
links[i].className = 'current';
break;
}
}
}
setCurrentNavLink('menu');
</script>
Check this demo: http://jsbin.com/tomid/1
Sorted. The problem was in JQuery , thats operates Tabs on the current page.
$(document).ready(function(){
// When a link is clicked
$("a.tab").click(function () {
// switch all tabs off
$("ul.tabs li a.active ").removeClass("active");
//$(".active ").removeClass("active");
// switch this tab on
$(this).addClass("active");
//$(".collapse navbar-collapse").addClass("active");
// slide all content up
$(".content").slideUp();
// slide this content up
var content_show = $(this).attr("title");
$("#"+content_show).slideDown();
});
});
The mistake was in 3rd section, where Class .active was removed from Every object. Including header. Therefore header did not appear as active.
Instead of that, class .active had to be removed only from Tabs tab.

Wordpress hide content on page

Im creating a wordpress site with WP-Ecommerce. My client asked for a functionality that i cant figure out how to make. He has a product with a long description and he wants multiple "read on" links to hide parts of it. It should be done in javascript so that client wont be redirected anywhere when he clicks on read more (extended content should just scroll open.)
it would be nice to find a plugin but i cant seem to find one.
A plugin for this would be easy to write. You just need to write a shortcode that adds the javascript to do what you want. The jQuery accordion for instance.
<?php
function add_accordion_js() {
?>
<script>
$(function() {
$( ".accordion" ).accordion();
});
</script>
<?php
}
function jquery_accordion($atts, $content = null) {
return '<div class="accordion">' . do_shortcode($content) . '</div>';
}
add_action('wp_head', 'add_accordion_js');
add_shortcode('accordion', 'jquery_accordion');
?>
Something like that can be added to your functions.php and you would wrap everything you need to collapse in [accordion][/accordion] tags... and it will break the sections down at the tags...
somhow i couldn't get accordion() function to work but Steve pointed me in right direction and i got this going:
function add_accordion_js() {
?>
<script>
jQuery(function() {
jQuery(".accordion").hide();
jQuery(".opener").click(function (e) {
jQuery(this).parent().next('.accordion').slideDown('fast');
jQuery(this).slideUp();
});
});
</script>
<?php
}
function jquery_accordion($atts, $content = null) {
return '<span class="opener">(read more)</span><div class="accordion">' . do_shortcode($content) . '</div>';
}
add_action('wp_head', 'add_accordion_js');
add_shortcode('accordion', 'jquery_accordion');
and things i needed to hide under read more link i wrapped in [accordion][/accordion]tags

Categories