Wordpress Get Posts AJAX infinate scroll - php

I'm working on a website http://www.matchlessphotography.com which has got a great display of photos - it tiles for ages...
Essentially the client would like this to continue on infinately.
I have no idea how to do this and have had a look at some tutorials without any bearing...
This is how I am currently getting the posts:
<?php
global $post;
$args = array('numberposts' => 104, 'meta_key=visible&meta_value=yes');
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<a class="photohovera" href="<?php the_permalink(); ?>">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<? echo $url ?>&h=138&w=197" width="197" height="138" title="<?php the_title(); ?>" />
<span class="spanno">
<strong class="title_blog_mini_post">
<?php the_title(); ?>
</strong>
</span>
</a>
<?php endforeach; ?>
I guess I just need to do this again but with an increased offset each time...?

Paul Irishs jQuery Plugin Infinite Scroll is the way to go here.
The plugin looks for a pagination container e.g. div.navigation and an item container for the items you are going to retrieve like .photohovera in your case.
WordPress provides a function for displaying pagination links. It's called paginate_links and its default output should match the requirements of the plugin.
However you will need to change they way you are getting your posts right now from get_posts() to a WP_Query Object:
<?php
$args = array('numberposts' => 104, 'meta_key=visible&meta_value=yes');
$myposts = new WP_Query( $args );
$results = $myposts->get_results();
foreach( $results as $post ) : setup_postdata($post); ?>

Related

WordPress Related Posts thumbnails work but permalink error

I am attempting to show 4 related posts beneath each WP post. The thumbnails work correctly, pulling in the feature image and the title, but the permalink does not. It comes in as "http://example.com/original-post-name/< ? the_permalink(); ? >/" (spaces added) and is clickable but of course no content found. This exact code works fine on a different site of mine, but not this new one. I'm sure it could be improved - I'm fairly new to wordpress theming.
<?php
// Default arguments
$args = array(
'posts_per_page' => 4,
'post__not_in' => array( get_the_ID() ),
'no_found_rows' => true,
);
$cats = wp_get_post_terms( get_the_ID(), 'category' );
$cats_ids = array();
foreach( $cats as $wpex_related_cat ) {
$cats_ids[] = $wpex_related_cat->term_id;
}
if ( ! empty( $cats_ids ) ) {
$args['category__in'] = $cats_ids;
}
// Query posts
$wpex_query = new wp_query( $args );
// Loop through posts
foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<!--<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute(
'echo=0' ) ); ?>"><?php the_title(); ?></a>-->
<div class="relatedthumb">
<a rel="external" href="<?the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br
/>
<?php the_title(); ?>
</a>
</div>
<?php
// End loop
endforeach;
// Reset post data
wp_reset_postdata(); ?>
Apologies if this has been asked before - I've tried searching and hoping this is a simple solution. Thank you!
You are using the wrong opening tag for php
<a rel="external" href="<?php the_permalink(); ?>">
So it's a typo

Query Page with Posts In WordPress

I am pulling posts from a specific category. This feature works. However, I also need it to pull pages every now and then and I can't figure out a way to get it to pull this in addition to the posts.
HTML/PHP
<?php query_posts('cat=8&posts_per_page=3'); while (have_posts()) : the_post(); ?>
<div>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" class="card-img img-responsive">
<p class="feature-card-head"><?php the_title(); ?></p>
<p class="feature-card-txt"><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I'd like to keep this structure and find a way to include pages, any feedback is helpful!
You cannot fetch posts and use this query as it is. WordPress posts have no categories by default so the cat=x part will always exclude automatically all posts.
I think there is no better solution than using a second query. Depending on what you are using this for, it may be better to separate these loops.
If you want to use a single loop for multiple queries consider merging the query like mentioned here:
<?php
$query1 = new WP_Query(array(
'cat' => 8,
'post_type' => 'post',
'posts_per_page' => 3
));
$query2 = new WP_Query(array(
'post_type' => 'page',
));
$wp_query = new WP_Query();
$wp_query->posts = array_merge( $query1->posts, $query2->posts );
$wp_query->post_count = $query1->post_count + $query2->post_count;
?>
<?php while( $wp_query->have_posts() ): $wp_query->the_post(); ?>
<div>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" class="card-img img-responsive">
<p class="feature-card-head"><?php the_title(); ?></p>
<p class="feature-card-txt"><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; wp_reset_query(); ?>

Wordpress a tag show thumbnail image in lightbox with plugin

So I tried to make a custom post type called 'showcase' and created a few posts in this custom post type. The problem is that when I click on a 'product' it should showup in a lightbox instead of going to the thumbnail url.
This is the code I am using:
<section id="showcase">
<?php
$args = array( 'post_type' => 'showcase', 'posts_per_page' => -1 ); $the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
$it = 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
$terms = get_the_terms( $post->ID , 'showcase_category');
$it ++;
if ($it == 6) {
echo '</section><div class="cta-block"><h2>Live model drawing</h2><p>Drawing from a live model, gives you the opportunity to draw what you see instead of drawing what you think about.</p>View courses</div><section id="showcase">';
}
?>
<a class="entry-showcase" href="<?php the_post_thumbnail_url(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail( 'medium' ); ?>
</a>
<?php wp_reset_postdata();
endwhile; else :
endif; ?>
</section>
Also, I tried using a few plugins which automaticly detect jpg, gif etc but for some reason they are not working for thumbnail images.
Some plugins I tested:
Easy FancyBox,
Responsive Lightbox,
Simple Lightbox,
Also, I am not getting any errors it just goes straight to the media file in url instead of staying on the same page and showing the image in a lightbox.
Thanks in advance (:
You can maybe use the Fresco-Lightbox. After implementing you can add the "fresco"-class to any imagelink and open it in a lightbox:
<a class="entry-showcase fresco" href="<?php the_post_thumbnail_url(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail( 'medium' ); ?>
</a>
Also see the Fresco documentation.

Creating a loop in functions.php in wordpress

I am trying to create a loop that will work as a custom navigation menu in the header using custom post type manager. I am creating a function in the functions.php and then calling the function in the header.php. I can't use the regular " while : have posts etc.." because it actually changes the page content. I just want to create a function that will bring up the image, custom fields, etc..
Here is my code that doesn't work:
<?php
// Our Team Navigation Menu
function our_team_arg( $arg2 ) {
$arg2 = array('posts_per_page' => 60, 'post_type' => 'our_team');
query_posts($arg2);
$myposts = get_posts( $arg2 );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="founderblk">
<a href="<?php the_permalink(); ?>">
<img alt="<?php the_title(); ?>" src="<?php print_custom_field('team_member_image:to_image_src'); ?>">
</a><br />
<span class="foundertitle"><?php print_custom_field('team_member_title'); ?></span>
</div>
<?php
endforeach;
wp_reset_postdata();
}
// END Our Team Navigation Menu
?>
you can use wp_query and use while normally it will not change the page content when you are using wp_reset_postdata();
and query_posts actually call wp_query internally
here is an example
$args = array('posts_per_page' => 60, 'post_type' => 'site-product');
// The Query
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) { $custom_query->the_post(); ?>
<div class="founderblk">
<a href="<?php the_permalink(); ?>">
<img alt="<?php the_title(); ?>" src="<?php print_custom_field('team_member_image:to_image_src'); ?>">
</a><br />
<span class="foundertitle"><?php print_custom_field('team_member_title'); ?></span>
</div>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
for the second line in your code
$arg2 = array('posts_per_page' => 60, 'post_type' => 'our_team');
I assume that you write it for testing because it is actually overwrite the function arguments

Wordpress doesn't display the permalinks of two of my post

Here is my webpage: http://dastousgroupeconseil.com/faq-2/
Here is the complete code of my page: http://pastebin.com/PQUsYdha
I used this php code to display the excerpt and the hyperlink of my posts, but some of them don't want to display. Is it because I reset the $post variable after each endforeach?
<?php global $post;
$args = array( 'numberposts' => 4, 'offset'=> 0, 'category' => 140 );
$myposts = get_posts( $args );
foreach( $myposts as $post) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>" target='_blank' ><?php the_excerpt(); ?></a>
<?php endforeach; wp_reset_postdata(); ?>
I've had issues using setup_postdata a few times myself, I tend to skip it altogether and just use the retrieved $post objects.
So for the permalink:
<?php echo get_permalink($post->ID); ?>

Categories