I'm trying to load a php file into a div#main when a link is clicked. The html content loads but the actual php code included in the file does not. Here's what I've got:
Link to trigger everything:
<a id="nav" href="http://domain.com/inc/file.php">
jQuery:
jQuery(document).ready(function($) {
$("#nav").on('click', function (e) {
e.preventDefault();
$("#main").empty();
$.ajax({
url: $("#nav").attr('href')
}).done(function(data) {
$('#main').html(data); // display data
});
});
});
Code from file.php (mostly WordPress functions):
<div id="hostels" class="section cf">
<?php get_a_post(31); ?>
<article id="swiss" <?php post_class('swiss bg col span-1-3'); ?> role="article">
<h3>
<a href="<?php the_permalink(); ?>">
<span class="logo-pl-small"><?php include(TEMPLATEPATH . '/library/svg/logo.svg'); ?></span>
<span class="">Swiss Cottage – London UK</span>
</a>
</h3>
<?php if ( has_post_thumbnail()) { the_post_thumbnail(); } ?>
<div class="entry-content">
<?php the_excerpt(); ?>
<?php edit_post_link( __( 'Edit', 'bonestheme' ) ); ?>
<?php if ( get_post_meta($post->ID, 'vidlink', true) ) {
echo apply_filters('the_content', get_post_meta($post->ID, 'vidlink', true));
} ?>
</div>
</article>
</div>
OUTPUT:
<div id="main">
<div id="hostels" class="section cf">
</div>
</div>
The file begins to load but as soon as <?php get_a_post(31); ?> is reached, the file does not continue to load.
I could be wrong, but I thought that by using $.ajax the php code would execute on the server and code it outputs would load into my #main div.
So I've got this somewhat working.
I found this tutorial for Ajax Powered Loops with jQuery and WordPress from which I discovered that (as I had guessed) the files I created need to understand WordPress functions. For this I need to include:
require_once('../../../../wp-load.php');
In my case that's the relevant path from file.php.
There's still some issues such as shortcodes generated from plugins not working fully, but that's beyond the scope of my original question.
There's a lot of useful info relating to ajax and Wordpress in the tutorial above though, if anyone is having similar issues I'd recommend having a read.
Instead of
$.ajax({
url: $("#nav").attr('href')
}).done(function(data) {
$('#main').html(data); // display data
});
why not the more simple
$("#main").load('http://domain.com/inc/file.php');
??
Related
I'm trying to loop a piece of jQuery code inside a foreach loop. Each article in the loop have a phone number custom post type related (ACF). At this point the loop works well.
As you see, the jQuery code only replace an image to another while user clicks in order to show the number (Ex : "Display Phone Number" image, becomes "555-555-1234").
The problem is that when I click in any image to display number...all articles show their phone number at the same time. I think is an ID problem in my jQuery code. After two days of searching and testing different codes this problem still not resolved yet.
Any suggestions/tracks will be very welcome !
Thanks
====
Things I have tried :
I have tried to put the jQuery code outside the foreach (same result)
I have tried to change the id image into a class (works better)
I have tried different jQuery functions (replaceWith(), show(), etc)
foreach ($related_posts_articles as $related_post ){
<div class="col-sm-6 col-md-6 col-lg-3 col-xs-12">
<div>
<a href="<?php echo get_permalink($related_post->ID); ?> ">
<?php echo get_the_post_thumbnail($related_post->ID,"square-300",array('class' => 'img-responsive')); ?>
</a>
<div>
<a href="<?php echo get_permalink($related_post->ID); ?>">
<h2>
<?php echo wp_html_excerpt(strip_tags(get_field('acf_titre_mini', $related_post->ID)), 30, '...' ); ?>
</h2>
</a>
<div>
<?php echo wp_html_excerpt( strip_tags(get_field('acf_description_mini',$related_post->ID)), 129, '...' ); ?>
</div>
<!-- START bloc number -->
<?php if( get_field('acf_numero_image', $related_post->ID) ): ?>
<div>
<img class="input_img" src="<?php the_field('acf_btnVoirNum_image', $related_post->ID); ?>" >
<script>
jQuery(document).ready(function($) {
$( ".input_img" ).click(function() {
$( ".input_img" ).attr( "src", "<?php the_field('acf_numero_image', $related_post->ID); ?>" );
});
});
</script>
</div>
<?php endif; ?>
<!-- END bloc number -->
</div>
</div>
</div>
}
Take note of the 'this' Context
When an event fires in jquery the callback function this context is set to the element which the event was fired from.
jQuery(document).ready(function($) {
$(".input_img").click(function() {
// Use `this` to target the event element
$(this).attr("src", "<?php the_field('acf_numero_image', $related_post->ID); ?>" );
});
});
Advice:
You shouldn't generate same jquery code inside each iteration of the for each. Since you're repeating unnecessary code. You can harness HTML data-* attributes to achieve the outcome you seek.
You are giving the class name the same for all images. Also by looping you are adding lot's of scripts. You can add Onclick events to image and create a function and grab the data and do you things. Also, you can add extra attributes to img tag to get your data. Also try to put different ID like below,
foreach ($related_posts_articles as $key=>$related_post ){
<img id="img-<?php echo $key; ?>" onclick="myFunction(this)" class="input_img" src="<?php the_field('acf_btnVoirNum_image', $related_post->ID); ?>" >
}
I made a custom template for the team I'm working on. I used this code for the template (along with the parts that get the header and footer ofc)
<div class="main-container">
<?php
the_post();
$thumbnail = has_post_thumbnail();
?>
<section class="section-header overlay preserve3d">
<div class="background-image-holder parallax-background">
<?php the_post_thumbnail('full', array('class' => 'background-image')); ?>
</div>
</section>
<section>
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="article-body">
<?php
if(!( $thumbnail ))
the_title('<h1>','</h1>');
the_content();
wp_link_pages();
?>
</div><!--end of article snippet-->
</div>
</div>
</div>
</section>
But if there's no featured image, a gray box will appear. I'd rather just have the whole section be hidden if there is no featured image, because it will look much better.
I'm a php rookie so I have no idea how to do this. What I think I need to do: Have an if/else statement check for the featured image. If there is no image, add a class to said section and define 'display none' in my css file for that class.
So this is the code I tried to pair with that, but it did not work:
<?php
// Must be inside a loop.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
$("section:first").addClass("noshow");
}
?>
Is this code incorrect? Where do I need to place this in my document for it to work?
While PHP is executed on the server, jquery is executed on the user end. You could always echo that jquery statement and that way it would be sent to the user.
You could follow the path Martin E. said on his comment (which in my opinion is a much cleaner version) or you could pursue the way you were working on by adding jquery on your header if you haven't and trying something like this:
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
echo '<script>$("section:first").addClass("noshow");</script>';
}
?>
I'm building a work portfolio using a WordPress category feed and jQuery to show some effects when you hover over a portfolio example. I would like to iterate using jQuery and a WordPress feed so the effect works for each of my portfolio examples... I have successfully done this for the first example in my portfolio, however getting it to work on each portfolio example is proving challenging. I understand each portfolio example needs a unique identifier but I don't understand how to apply it to each one. Please see the code below thank you for helping me
JS:
$(document).ready(function() {
$('div.recentWork').hover(function(){
$(".links").stop(1).slideDown();
$(".imagio").fadeOut();
},function(){
$(".links").stop(1).slideUp();
$(".imagio").fadeIn();
});
});
PHP:
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php
if(has_post_thumbnail($post->ID)){
$thumbsrc = get_the_post_thumbnail($post->ID,'medium');
}else{
$thumbsrc = "<img src=\"images/no_featured_image.jpg\" style=\"width:100%;height:180px;\">";
}
$url = get_post_meta($post->ID, "URL", true);
$website = get_post_meta($post->ID, "Website", true);
?>
<div class="recentWork">
<div class="links">
<img src="<?php bloginfo('url'); ?>/images/icons/icon_zoom.png" /><img src="<?php bloginfo('url'); ?>/images/icons/icon_more.png" /><br />
<?php the_title(); ?>
</div>
<div class="imagio">
<?php echo $thumbsrc; ?>
</div>
</div>
<?php endwhile; ?>
Uses class selectors
$('.className')
or node selectors
$('div')
or both
$('div.className')
Then inside of your hover handler, use
$(this)
e.g.
$(document).ready(function() {
$('div.recentWork').hover(
function(){
$(this).find('.imagio').fadeOut();
$(this).find('.showLinks').fadeIn();
},
function(){
$(this).find('.imagio').fadeIn();
$(this).find('.showLinks').fadeOut();
}
);
});
my name is Chris and this is my first help question.
I am having some issues trying to figure out how to embed a Minecraft Server Dynamic Map into my PHP.
The map is 209.105.236.244:8123
The code in which I am trying to embed it is
<div class="span6">
<?php if( protectThis("1, 2") ) : ?>
<h1 class="page-header"><?php _e('You have the ability to view this map'); ?></h1>
<p><?php _e('You will only be able to see this content if you have a <span class="label label-info">special</span> user level. ')?></p>
<?php else : ?>
<div class="alert alert-warning"><?php _e(' ***Dynamic Map Here***'); ?></div>
<?php endif; ?>
</div>
I am just not able to figure out how to make the map display here instead of them just going to the url. :3
If anyone can provide me with a solution or even a jumping off point on how to go about this, that would be simply amazing! Please and thank you :)
I'm assuming you intend to call javascript based on the page source at 209.105.236.244:8123
You could forgo the echo <?php _e(' ***Dynamic Map Here***'); ?> and include your map related scripts here just as you did with your other html tags. It should still fall within your "else" condition so that these scripts are only called if your requirements are met.
In this case, any time protectThis("1, 2") is not true, the map should be displayed.
<?php if( protectThis("1, 2") ) : ?>
<h1 class="page-header"><?php _e('You have the ability to view this map'); ?></h1>
<p><?php _e('You will only be able to see this content if you have a <span class="label label-info">special</span> user level. ')?></p>
<?php else : ?>
<div class="alert alert-warning">
<script type="text/javascript" src="js/script.js"></script>
<script type="text/javascript" src="js/script.json.js"></script>
</div>
<?php endif; ?>
pictures of interface
I've created a jquery script to AJAX load content. This content consists of post made from a custom-post-type. For reference I've used Loading WordPress posts with Ajax and jQuery.
Problem What would I replace the .load URL with, to allow that everytime I click #ajaxNext it goes to previous post submitted custom-post-type URL?
example: Current displayed custom-post was posted in Aug 30 2011. *Upon clicking #ajaxNext post updates to Aug 24 2011's post *
Reference to Wordpress doc.
jQuery This code is held in the header of single-custom_type.php
<script type="text/javascript">
$(document).ready(function(){
$.ajaxSetup({cache:false});
$("#ajaxNext").click(function(){
var post_id = $(this).attr("rel")
$("#container").html("loading...");
$("#container").load("<?php get_adjacent_post_rel_link( $title, $in_same_cat, $excluded_categories, $previous ); ?>");
return false;
});
});
</script>
This $("#container").load('<?php get_adjacent_post_rel_link( $title, $in_same_cat, $excluded_categories, $previous ); ?>'); is what creates the request's URL (I put in a direct URL for testing purposes and it works. Unfortunately, this php doesn't work). I would like that .load's URL to be the next post in wordpress. I've followed the tutorial as far as it would take me. Had to make a few adjustments due to me using a custom-post-type. Still no luck in it working :(
PHP Logically home.php contains PHP reference below
<div id="wrapper">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
<div id="inner">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail(full);
}
?>
</div>
</div>
HTML this particular chunk of html contains the anchor which executes the AJAX call
<section class="summary">
<div>
<header> <img src="<?php echo get_template_directory_uri(); ?>/library/images/works.png" />
<h1>Process</h1>
<h2><a id="ajaxNext" href="<?php the_permalink() ?>" rel="<?php the_ID(); ?>" title="<?php the_title_attribute(); ?>">Next</a></h2>
</header>
<p>
<p><?php getCustomField('Process'); ?></p>
</p>
</div>
</section>
If i'm missing anything please don't hesitate to ask! I'll respond immediately.
Lastly, If there was a previous button and if the code is drastically different how would i create that .load(); URL request i.e go to the newest post.
You are using the wrong function. get_adjacent_post_rel_link() returns a complete link-- <link rel="prev"...> You need to raw URL, not the whole link. Unfortunately, there isn't a 'right' function that I can find, much to my surprise. There are a number of solutions for this out there, but I'd tend to just suck the URL out of what I had.
$next = get_adjacent_post_rel_link();
preg_match('/href=\'([^\']*)\'/',$next,$matches);
var_dump($matches[1]);