enter image description hereI am currently working on a project where my script adds the schema codex into the navigation. The jQuery code does work.
However, when I add the link for ajax.googleapis.com it break all other Divi style scripts. I would be grateful for any help! Please view my coding below. I have this in a child theme and the code is stored in a functions.php file.
The theme builder I am working with is Div.
<?php
function load_js()
{
if(!is_admin())
{
echo '<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>';
echo '<script type="text/javascript">
jQuery( document ).ready(function(){
// Put itemscope and itemtype into navigation ul
$("nav ul").attr("itemscope", "").attr("itemtype", "http://www.schema.org/SiteNavigationElement");
// put itemprop in lis
$("nav ul li").attr("itemprop", "name");
//put itemprop url in anchors
$("nav ul li a").attr("itemprop", "url");
});
</script>';
}
}
add_action( 'wp_footer', 'load_js');
After think on it and working on some other projects, I have accomplished and fixed the problem.
The issue was that the script to pull in the ajax jquery library was being loaded into the and not the . So I made some simple changes you can view below to see the fix.
<?php
function load_js()
{
if(!is_admin())
{
echo '<script type="text/javascript">
$( document ).ready(function(){
// Put itemscope and itemtype into navigation ul
$("nav ul").attr("itemscope", "").attr("itemtype", "http://www.schema.org/SiteNavigationElement");
// put itemprop in lis
$("nav ul li").attr("itemprop", "name");
//put itemprop url in anchors
$("nav ul li a").attr("itemprop", "url");
});
</script>';
}
}
add_action( 'wp_footer', 'load_js');
// load ajax jquery for schema nav
function ajax_jquery()
{
if(!is_admin())
{
echo '<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>';
}
}
add_action( 'et_head_meta', 'ajax_jquery');
Thanks everyone for your help!
Related
Somehow this doesn't work. The #autoverkocht should be hidden by default, unless the .b-car-info__price is hidden, #autoverkocht should be shown.
jQuery(document).ready(function($){
$("#autoverkocht").css({
display: "none",
visibility: "hidden"
});
})
if($('.b-car-info__price').is(":hidden")){
$('#autoverkocht').css('visibility', 'visible');
}
It's either the jQuery above or the functions.php code below that doesn't work.
add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
wp_enqueue_script(
'your-script', // name your script so that you can attach other scripts and de-register, etc.
get_template_directory_uri() . '/js/autoverkocht.js', // this is the location of your script file
array('jquery') // this array lists the scripts upon which your script depends
);
}
You can use inline styling like below:
<div id='autoverkocht' style='display: none;'>
</div>
or use css styling like this:
#autoverkocht{
display: none;
}
And in the jquery do this:
<script>
jQuery(document).ready(function($){
if($('.b-car-info__price).is(":hidden")){
$('#autoverkocht').show();
}
});
</script>
Think jQuery toggle and hide is what you need .
$('#autoverkocht').hide()
$('#autoverkocht, .b-car-info__price').on(
'click',
function()
{
$('#autoverkocht, .b-car-info__price').toggle()
}
);
Hope this is what you require ......
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!
I have this site here http://jamessuske.com/freelance/seasons/index.php and in the navigation on the left, I am using jQuery to remove a class and add it the current pages its on, also if you click on the Menu link it should show a submenu.
<script type="text/javascript">
$( '.navigation li a' ).each(function() {
$(this).removeClass('active');
});
$('.navigation li ul.menu-submenu').hide();
$('.navigation li a').eq(6).addClass("active");
</script>
Above is the code I have in every separate page to update the navigation.
In my header.php file, I have the following:
<script type="text/javascript" src="js/jquery-2.0.3.min.js"></script>
<script type="text/javascript">
$(window).load(function () {
var theWindow = $(window),
$bg = $("#bg"),
aspectRatio = $bg.width() / $bg.height();
function resizeBg() {
if ((theWindow.width() / theWindow.height()) < aspectRatio) {
$bg.removeClass()
.addClass('bgheight');
} else {
$bg.removeClass()
.addClass('bgwidth');
}
}
theWindow.resize(resizeBg).trigger("resize");
});
</script>
Would this mess up my navigation?
The header.php file is included on all pages with php include.
jQuery 2.x has dropped support for old IE versions. Use jQuery 1.10.x instead. Credit to Rob W.
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