I'm using the jquery cycle to create a slideshow in my Wordpress template. The "pager" is creating buttons that overlay the slideshow and rollover advances the slides. The trouble I'm running into is that I need the rollover to stay the same, but I also need to set an href so that clicking on the buttons takes the user to a specific page.
I need to retrieve the variable $target from my slideshow for the href.
I need the onclick to make the button clickable.
As I stated above, I'm new at this and it's quite possible that this is terrible code/can't be done/I'm a moron.
My jquery:
<script src="<?php bloginfo('stylesheet_directory'); ?>/scripts/jquery.cycle.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
if ( $('.slides > .slide').size() > 1 ) {
$('.slides')
.cycle({
timeout: 6000,
speed: 1000,
pager: '#slides #mainNav',
pauseOnPagerHover: 200,
pagerEvent: 'mouseover',
pause: true,
pagerAnchorBuilder: function(idx, slide) {
var slideImage = $(slide).find('img');
var slideTitle = slideImage.attr('title');
var slideURL = "<?php echo $target; ?>";
return '<li>' + slideTitle + /* '<br /><span class="description">' + slideDescr + '</span>*/'</li>';
}
});
}
});
</script>
and my Wordpress slideshow:
<?php if ( is_front_page() && $slides = get_posts(array('numberposts' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'post_type' => 'slide')) ) : ?>
<div id="mainImg">
<div id="slides">
<div class="slides">
<?php foreach ($slides as $slide) : ?>
<?php $title = $slide->post_title; ?>
<?php $content = wpautop($slide->post_content); ?>
<?php $description = get_post_meta($slide->ID, 'description', true); ?>
<?php $thumb = get_the_post_thumbnail($slide->ID, 'slide', array('title' => $title, 'alt' => $description)); ?>
<?php $url = get_post_meta($slide->ID, '_slide_url', true); ?>
<?php $target = (get_post_meta($slide->ID, '_slide_url_blank', true)) ? 'target="_blank"' : ''; ?>
<div class="slide">
<?php if ($url) : ?>
<a href="<?php echo $url; ?>" <?php echo $target; ?>><?php echo $thumb; ?></a>
<?php else: ?>
<?php echo $thumb; ?>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<div id="mainNav"></div>
</div>
</div><!--end mainImg-->
<?php endif; ?>
Thank you in advance for your help.
Where you have:
var slideURL = "<?php echo $target; ?>"
Replace it with:
var slideURL = $(slide).find('a').attr('target');
What you are doing above is getting the slide object, finding the 'a' tag in the slide, and then getting the "target" attribute of the 'a' tag. This value is then assigned to the slideURL variable (which will either be '_blank' or undefined). For more information on how to use jQuery to get HTML elements and/or their attributes, you should familiarise yourself more with jQuery's Traversing and Attributes methods.
You should never mix Javascript code with PHP or even HTML code anyway. Read up more on "Unobtrusive JavaScript".
Related
So I have been trying to add the carousel as a shortcode but whenever I try it will end up crashing or not displaying this is being done in functions php and it appends to the body as of now please see the code below
function flexslider_js() {
$images = get_field('gallery');
if( $images ): ?>
<div id="carousel" class="flexslider">
<ul class="slides">
<?php foreach( $images as $image ): ?>
<li>
<img src="<?php echo esc_url($image['sizes']['thumbnail']); ?>" alt="Thumbnail of <?php echo esc_url($image['alt']); ?>" />
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif;
?>
<script>
// Can also be used with $(document).ready()
$(window).load(function () {
$(".flexslider").flexslider({
animation: "slide",
animationLoop: false,
itemWidth: 210,
itemMargin: 5
});
});
</script>
<?php
}
add_action('wp_head', 'flexslider_js');
thank you for the help I have found the answer this may not be the most efficient way of doing so if there any suggestions of a better way please be sure to say
function flexslider_js() {
?>
<script>
// Can also be used with $(document).ready()
$(window).load(function () {
$(".flexslider").flexslider({
animation: "slide",
animationLoop: false,
itemWidth: 210,
itemMargin: 20
});
});
</script>
<?php
$images = get_field('gallery');
$html = '';
foreach( $images as $image ) {
$slides .= '<li><div class="slide-contents"><img src="' . $image['sizes']['thumbnail'] . '" class="slide-image"/></div></li>';
}
$html .= '<div id="carousel" class="flexslider"><ul class="slides">' . $slides . '</ul></div>';
console.log($html);
return $html;
}
add_shortcode( 'carousel', 'flexslider_js' );
I'm loading wordpress posts via an AJAX script, everything works great until I try to prevent the latest post from being included in the query. When I try to pass offset=1 to the query I get the same posts repeating everytime the 'More posts' link is clicked.
Here's my custom page template, the first loop creates a single hero post that sits above the AJAX loop:
<?php
/*
Template Name: Home
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<div class="latest-popular">
<a class="active-link">Most recent</a> Most popular
</div>
<section class="hero-image">
<?php $my_query = new WP_Query('orderby=menu_order&posts_per_page=1');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<figure><?php the_post_thumbnail(); ?>
<a href="<?php the_permalink(); ?>" rel="bookmark"><figcaption <?php post_class(); ?>>
<h1><?php the_title(); ?></h1>
<h2><?php the_field('description'); ?></h2>
<p class="credit"><?php the_field('credit'); ?></p>
<span class="length"><?php the_field('running_time'); ?> minutes</span>
</figcaption></a><?php if( $field = get_field('exclusive') ): ?><section id="exc" <?php post_class(); ?>></section><?php endif; ?><!-- <section class="award"></section> -->
</figure>
<?php endwhile; ?>
</section>
</main><!-- #main -->
</div><!-- #primary -->
<!-- Ajax Load More script block -->
<section id="ajax-load-more">
<ul class="listing" data-path="<?php echo get_template_directory_uri(); ?>" data-post-type="post" data-display-posts="10" data-post-not-in="<?php echo $postsNotIn; ?>" data-button-text="More Posts" >
<!-- Load Ajax Posts Here -->
</ul>
</section>
<!-- /Ajax Load More -->
<!-- Ajax -->
<script>
$(function() {
if($("#ajax-load-more").length){
var page = 1,
$loading = true,
$finished = false,
$window = $(window),
$el = $('#ajax-load-more'),
$content = $('#ajax-load-more ul'),
$path = $content.attr('data-path');
if($path === undefined){
$path = '/wp-content/themes/my-theme/ajax-load-more.php';
}
//Define button text
if($content.attr('data-button-text') === undefined){
$button = 'More posts';
}else{
$button = $content.attr('data-button-text');
}
$el.append('<p id="load-more" class="more"><span class="loader"></span><span class="load-more-link">'+$button+'</span></p>');
//Load posts function
var load_posts = function(){
$('#load-more').addClass('loading');
$('#load-more span.text').text("Loading...");
$.ajax({
type : "GET",
data : {
postType : $content.attr('data-post-type'),
category : $content.attr('data-category'),
author : $content.attr('data-author'),
taxonomy : $content.attr('data-taxonomy'),
tag : $content.attr('data-tag'),
postNotIn : $content.attr('data-post-not-in'),
numPosts : $content.attr('data-display-posts'),
pageNumber : page,
},
dataType : "html",
url : $path+"/ajax-load-more.php",
beforeSend : function(){
if(page != 1){
$('#load-more').addClass('loading');
$('#load-more span.text').text("Loading...");
}
},
success : function(data){
$data = $('<span>'+data+'</span>');// Convert data to an object
//alert(data);
if(data.length > 1){
$data.hide();
$content.append($data);
$data.fadeIn(500, function(){
$('#load-more').removeClass('loading');
$('#load-more span.text').text($button);
$loading = false;
});
} else {
$('#load-more').addClass('done');
$('#load-more span.text').text($button);
$loading = false;
$finished = true;
}
},
error : function(jqXHR, textStatus, errorThrown) {
$('#load-more').removeClass('loading');
$('#load-more span.text').text($button);
//alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);
}
});
}
$('#load-more').click(function(){
if(!$loading && !$finished && !$(this).hasClass('done')) {
$loading = true;
page++;
load_posts();
}
});
load_posts();
}
});
</script>
<?php get_footer(); ?>
And the PHP that includes the AJAX loop:
<?php
// Our include
define('WP_USE_THEMES', false);
require_once('../../../wp-load.php');
// Our variables
$postType = (isset($_GET['postType'])) ? $_GET['postType'] : 'post';
$category = (isset($_GET['category'])) ? $_GET['category'] : '';
$author_id = (isset($_GET['author'])) ? $_GET['taxonomy'] : '';
$taxonomy = (isset($_GET['taxonomy'])) ? $_GET['taxonomy'] : '';
$tag = (isset($_GET['tag'])) ? $_GET['tag'] : '';
$exclude = (isset($_GET['postNotIn'])) ? $_GET['postNotIn'] : '';
$numPosts = (isset($_GET['numPosts'])) ? $_GET['numPosts'] : 6;
$page = (isset($_GET['pageNumber'])) ? $_GET['pageNumber'] : 0;
$args = array(
'post_type' => $postType,
'category_name' => $category,
'author' => $author_id,
'posts_per_page' => $numPosts,
'paged' => $page,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_status' => 'publish',
);
// EXCLUDE POSTS
// Create new array of excluded posts
/* Example array from parent page:
$features = array();
foreach( $posts as $post):
setup_postdata($post);
$features[] = $post->ID;
endforeach;
if($features){
$postsNotIn = implode(",", $features);
}
*/
// QUERY BY TAXONOMY
if(empty($taxonomy)){
$args['tag'] = $tag;
}else{
$args[$taxonomy] = $tag;
}
query_posts($args);
?>
<?php
// our loop
if (have_posts()) :
$i =0;
while (have_posts()):
$i++;
the_post();?>
<!-- Do stuff -->
<?php endwhile; endif; wp_reset_query(); ?>
As I mentioned above, adding offset=1 causes the same posts to be called every time the 'more posts' link is clicked, I need the latest post in the loop to be excluded, can anyone shed some light on what I'm doing wrong please?
You must use the data-post-not-in attribute and pass an array of post IDs as Offset does not work with pagination.
Check out the readme on the github repo # https://github.com/dcooney/wordpress-ajax-load-more
An example would be:
$features = array('7238', '6649', '6951'); // Array of posts to exclude
if($features){
$postsNotIn = implode(",", $features); //Implode the posts and set a variable to pass to our data-post-not-in param.
}
And then when you would call the Ajax like this:
<ul class="listing" data-path="<?php echo get_template_directory_uri(); ?>" data-post-type="post" data-display-posts="10" data-post-not-in="<?php echo $postsNotIn; ?>" data-button-text="More posts">
Offset has a tendency to break pagination. There's a great article about this on the codex which should solve your problem. Please see: http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
I am trying to achieve result with below structure in foreach loop where after every two images it will repeat entire structure.
I have some basic knowledge for something I can use eg. counter++; and than %2 but don't know syntax and how to use it for my code.
<?php
function dt_attached($postid=0, $size='thumbnail', $attributes='', $linksize='full', $count=-1) {
if ($postid<1) $postid = get_the_ID();
if ($images = get_children(array(
'post_parent' => $postid,
'post_type' => 'attachment',
'numberposts' => $count,
'post_mime_type' => 'image',)))
foreach($images as $image) {
$attachment=wp_get_attachment_image_src($image->ID, 'thumbnail');
$small_image = wp_get_attachment_image_src($image->ID, 'midium');
$big_image = wp_get_attachment_image_src($image->ID, 'full');
?>
<div class="mainrow">
<div class="block">
<a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
<img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
</a>
</div>
<!--[I want to get two images in mainrow]-->
<div class="block">
<a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
<img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
</a>
</div>
</div>
<?php //the_attachment_link($image->ID, false, true, false); ?>
<?php }
}
?>
So what I want is if there is more than two images it will repeat entire html structure. Thanks a lot for your help
What I gathered from your comments is that you want to display two images per row, and if there's one extra image, to display a placeholder next to it in the final row.
All you need is a count of how many images there are, and whether it's an even or odd number. Then once you know you're at the last image (using an incrementing counter), you add the placeholder:
What your code doesn't do is place two images in one row. For that we need to take the modulo (%) of the counter as well.
<?php
$counter = 0;
$imgCount = count($images);
foreach ($images as $image) {
$attachment = wp_get_attachment_image_src($image->ID, 'thumbnail');
$small_image = wp_get_attachment_image_src($image->ID, 'midium');
$big_image = wp_get_attachment_image_src($image->ID, 'full');
if ($counter % 2 == 0): ?>
<div class="mainrow">
<?php endif; ?>
<div class="block">
<a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
<img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
</a>
</div>
<?php if ($counter++ % 2 == 1): ?>
</div>
<?php endif; ?>
<?php //the_attachment_link($image->ID, false, true, false); ?>
<?php
}
// Since (if there are an odd number of images) the loop may not close the <div>,
// we have to make sure it does.
if ($counter % 2 == 0) {
?>
<!-- placeholder goes here -->
</div>
<?php
}
I am trying to modify this Wordpress theme: http://www.elegantthemes.com/preview/DailyNotes/
For a regular Text Post, I want it to display a thumbnail picture if one exists. Otherwise, just display the Title and Excerpt from the post. Here is the IF and ELSE statement that I am stuck on. I can't get it to work and it's one or the other (always IF or always ELSE) when I change the conditional statement.
<?php if ($arr[$j]['posttype'] == 'text') { ?>
<div class="inside" onclick="window.location='<?php echo $arr[$j]["permalink"];?>'">
<?php if (file_exists($arr[$j]["thumb"])) { ?>
<span class="photospan">
<img src="<?php bloginfo('template_directory'); ?>/images/shadow-overlay.png" alt="thumbnail" class="thumb_overlay" />
<?php print_thumbnail($arr[$j]["thumb"], $arr[$j]["use_timthumb"], $arr[$j]['title'] , 149, 149, '', $post = $arr[$j]["post"]); ?>
</span>
<?php } else { ?>
<div class="overflow">
<h2><?php echo($arr[$j]['title']); ?></h2>
<?php echo $arr[$j]['excerpt']); ?>
</div>
<?php } ?>
<img class="icon" src="<?php bloginfo('template_directory'); ?>/images/icon-photo.gif" alt="article post" />
</div>
<?php }; ?>
I tried putting in different arguments of the print_thumbnail function into the IF statement and it doesn't work. Here are the arguments for the print_thumbnail function in case it helps:
function print_thumbnail($thumbnail = '', $use_timthumb = true, $alttext = '', $width = 100, $height = 100, $class = '', $echoout = true, $forstyle = false, $resize = true, $post='') {
Can anyone tell me which argument I should use in my IF statement? Thanks in advance and let me know if you need more information.
You need to do <?php if ($arr[$j]["thumb"]) { ?>
and then
else if (!$arr[$j]["thumb"]) {
I'm trying to make a pagination system, and to make it work on every page I'm using the $query_string variable, which supposedly contains all the information about the category, etc.
So I'm doing something like this:
add_action('wp_ajax_and_action', 'get_posts_page');
add_action('wp_ajax_nopriv_and_action', 'get_posts_page');
function get_posts_page() {
$query_string = $_POST['query_string'];
global $wpdb;
query_posts($query_string . '&posts_per_page=10&post_status=publish&offset='.$_POST['off']);
It's in my functions.php file. I've globalized the $query_string variable in my header.php file. The $_POST['query_string'] is coming from a javascript function (also in my functions.php file) which I've set to be in wp_head (so the head of the document I assume). It's posting a bunch of data to the PHP function:
$.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
action: 'and_action',
off: offset+number,
pagenumber: page_number,
query_string: '<?php echo $query_string; ?>'
}, function(data) {
However, upon further inspection it shows the query_string variable as null.
So when I do: <?php echo $query_string ?> nothing returns. Any ideas why that might be? Thanks :)
Updated
Heres an Update
functions.php
The Javascript:
add_filter('wp_head', 'javascript_page');
function javascript_page() {
?>
<script type="text/javascript">
$(document).ready(function() {
var number = 10;
var offset = 0;
var page_number = 2;
var busy = false;
/* Bind the scroll function to an event */
$(window).bind('scroll', function(e) {
/* If the scroll height plus the window height is more than the document height minus 10, continue */
if($(window).scrollTop() + $(window).height() > $(document).height() - 10 && !busy) {
busy = true;
/* Quick message so you know more stuff is loading */
$('.loading-more').html('Click to load more posts..');
$.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
action: 'and_action',
off: offset+number,
pagenumber: page_number,
query_string: '<?php echo $query_string; ?>'
}, function(data) {
offset = offset+number;
$('.empty-div').append('<div class="pages"><p>Welcome to <strong>Page '+page_number+'</strong></p></div><hr />'+data);
busy = false;
page_number += 1;
});
}
});
$('.loading-more').bind('click', function(e) {
busy = true;
$('.loading-more').html('<em>Loading more posts..</em>')
/* Quick message so you know more stuff is loading */
$.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
action: 'and_action',
off: offset+number,
pagenumber: page_number,
query_string: '<?php echo $query_string; ?>'
}, function(data) {
offset = offset+number;
$('.empty-div').append('<div class="pages"><p>Welcome to <strong>Page '+page_number+'</strong></p></div><hr />'+data);
busy = false;
page_number += 1;
$('.loading-more').html('Click to load more posts..');
});
});
});
</script>
The PHP function:
add_action('wp_ajax_and_action', 'get_posts_page');
add_action('wp_ajax_nopriv_and_action', 'get_posts_page');
function get_posts_page() {
$query_string = $_POST['query_string'];
global $wpdb;
query_posts($query_string . '&posts_per_page=10&post_status=publish&offset='.$_POST['off']);
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-meta">
<span class="%1$s">Posted on</span> <?php the_date('F jS'); ?>
- <a class="comment-link" href="<?php the_permalink(); ?>#comment"><?php comments_number('Leave a Response!', '1 Response', '% Responses'); ?></a>
</div><!-- .entry-meta -->
<br />
<a class="post-thumbnail-thing" href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail(); ?></a>
<div class="entry-content">
<?php the_content( __( '<span class="alignright">
<span class="button-css">Continue Reading →</span>
</span>', 'twentyten' ) ); ?><br /><hr />
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
</div><!-- #post-## -->
<?php comments_template( '', true ); ?>
<?php
endwhile; endif;
die();
}
header.php
<?php global $query_string; ?>
As I've said, the main problem is $query_string is null :(
Your globalizing $query_string inside of your header.php file, but you need to globalize it inside of your function javascript_page() inside of functions.php
Here's a video explaining what I'm doing
function javascript_page() {
global $query_string;
?>
<script type="text/javascript">