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
Related
I write the below code to a footer.php file. This works but is applied for all the pages. I want to apply page scroll in one page only.
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('body').animate({scrollTop: +400}, 1000);
});
</script>
Can any one help me on how to do it.
Thanks
Add a contidional statemnent on the footer.php file to only execute the jquery code if it is in only in the current page you wish for.
something like this example below
<?php
global $post;
$target_pageid = 7654;
$current_pageid = $post->ID;
if (current_pageid == $target_pageid){
?>
jQuery(document).ready(function() {
jQuery('body').animate({scrollTop: +400}, 1000);
});
<?php
}
?>
I'm now stuck with this problem below, and I hope you can help me out. I would really appreciate it.
1st -- I created the Wordpress Menu (got it all worked).
2nd -- I implemented the Bootstrap classes (e.g nav navbar-nav, dropdown, dropdown-menu etc..) on the html tags of the Wordpress Menu. (got it and made it work somehow)
3rd -- To make the dropdowns work I used (preg_place) to replace the default (submenu) to (dropdown-menu) then to make the function work I used the (preg-replace) again to INSERT a class and data-toggle to the Anchor Links.
Now the problem is, the anchor links doesn't seem to function right at all.
check it out here.
(use the navs on top)
HEres the code on the function.php
function new_submenu_class($menu) {
$menu = preg_replace('/ class="sub-menu"/','/ class="dropdown-menu" /',$menu);
return $menu;
}
add_filter('wp_nav_menu','new_submenu_class');
function add_menuclass($ulclass) {
$ulclass = preg_replace('/<a/', '<a class="dropdown-toggle" data-toggle="dropdown" role="button"', $ulclass);
return $ulclass;
}
add_filter('wp_nav_menu','add_menuclass');
Thanks much guys!
And btw.. i used a jQuery for the 3rd level submenu. which is:
<script type="text/javascript">
$(document).ready(function() {
$('.navbar a.dropdown-toggle').on('click', function(e) {
var $el = $(this);
var $parent = $(this).offsetParent(".dropdown-menu");
$(this).parent("li").toggleClass('open');
if(!$parent.parent().hasClass('nav')) {
$el.next().css({"top": $el[0].offsetTop, "left": $parent.outerWidth() - 4});
}
$('.nav li.open').not($(this).parents("li")).removeClass("open");
return false;
});
});
</script>
In the function, I would try to change this line:
$('.navbar a.dropdown-toggle').on('click', function(e) {
For this one:
$('.navbar').on('click', 'a.dropdown-toggle', function(e) {
Since a.dropdown-toggle has been happended...
Maybe it was not present on "document ready", so the "delagation" syntax could help.
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!
Iv been able to add a quick-tag button that adds [spoiler][/spoiler] to my wp comment section but now I need to hide / show the text inside.
I was provided with the following code to add into my themes function.php but it doesn't look like the code is being executed because the page output is still test [spoiler]hint[/spoiler] and not test View Spoiler>>
`// jQuery Spoiler Function`
function add_spoilers() {
echo '
<script type="text/javascript">
$(document).ready(function() {
$("span.spoiler").hide();
$(\'<a class="reveal">View Spoiler ยป</a> \').insertBefore(\'.spoiler\');
$("a.reveal").click(function(){
$(this).parents("p").children("span.spoiler").fadeIn(100);
$(this).parents("p").children("a.reveal").fadeOut(100);
});
});
</script>
<style>
/* If you want you can add custom CSS here for the spoilers! Below is what I use on my blog. */
a.reveal:hover { cursor:pointer; }
span.spoiler { background-color:#FCE586; }
</style>
';
}
add_action('wp_head', 'add_spoilers');
// Spoiler Shortcode
function spoiler_shortcode( $atts, $content = null ) {
$output = '<span class="spoiler">';
$output .= do_shortcode($content);
$output .= '</span>';
return $output;
}
add_shortcode('spoiler', 'spoiler_shortcode');`
I am a newbie in Javascript and I a trying to accomplish something certainly obvious, but I do not seem able to do it right.
I have a menu on the left, and there's one particular link which should load an external .php page in a certain destination div.
The .php page includes PHP statements like
echo "<script> blah blah blah </script>;"
When I load the page these scripts are not executed.
I try to avoid using jQuery, but even with jQuery I do not seem able to make it work.
Could someone tell me how I should do it?
Calling page:
<script>
//jQuery method, jQuery loaded
function loadPage(destId, pagetoload){
$('<div id="info" />').load(pagetoload, function() {
$(this).appendTo(destId)
.slideDown(3000);
});
return false;
}
</script>
<a id='assigncusts' href="javascript:loadPage('others', 'players.php')">Assign</a>
The called .php page:
<?PHP
function createDropdown($arr, $code_cust, $log) {
echo "".$arr[$log]."";
foreach ($arr as $key => $value) {
if ($key != $log){
echo '<option value="'.$value.'">'.$value.'</option>';
}
}
echo '</select>';
}
?>
//In a while loop:
echo "<tr><td>".$row['player_name']."</td><td>";
createDropdown($users_array, $row['player_name'], $row['Login']);
echo "</td></tr>\n";
Thank you in advance
Create your div in html, then:
$(document).ready(function() {
$("#info").load("yourscript.php");
});
Your passing the string 'others' to destId and doing $(this).appendTo(destId) -- it's basically do [info div].appendTo('other') searching for the TAG "other"
Instead try doing:
$('#info').hide().load(pagetoload, function() {
$(this).show().slideDown(3000);
});
Or you might want $('#' + divId) it depends on where you want the page loaded to. The matched element before .load() is where the content will be automatically placed into.