WP_Query + Infinite Scroll - php

I've stucked while working on my brand new blog template (on wordpress). I've got following query/php code:
echo '<div id="posts-container" class="fusion-blog-layout-medium fusion-blog-infinite fusion-posts-container-infinite fusion-blog-archive fusion-clearfix">';
$args = array( 'post_type' => 'era', 'post_status' => array('publish', 'future'), 'paged' => $paged );
$custom_query = new WP_Query($args);
while($custom_query->have_posts()) : $custom_query->the_post();
$post_classes = $post_class . ' ' . $alignment_class . ' ' . $thumb_class . ' post fusion-clearfix';
ob_start();
post_class( $post_classes );
$post_classes = ob_get_clean();
echo '<article id="post-' . get_the_ID() . '" ' . $post_classes . '>';
get_template_part( 'new-slideshow' );
echo '<div class="fusion-post-content koncert post-content">';
echo ( '<h2 class="entry-title fusion-post-title" data-fontsize="18" data-lineheight="27">' .get_the_title() . '</h2>' );
if ( get_field( "data_i_miejsce_koncertu", get_the_ID() ) ) {
echo ( '<div class="lista-koncert-podtytul">' . get_field( "data_i_miejsce_koncertu", get_the_ID() ) . '</div>' );
}
echo '<div class="fusion-post-content-container">';
do_action( 'avada_blog_post_content' );
if ( get_field( "opis", get_the_ID() ) ) {
echo '<div class="lista-koncert-opis">' . wp_trim_words(get_field( "opis", get_the_ID() ), 60, ' [...]') . '<br><br>Zobacz więcej ></div>';
}
echo '</div>';
echo '</div>'; // End post-content.
echo '</article>';
endwhile;
wp_reset_postdata(); // reset the query
echo '</div>';
What I would like to achieve is to not have regular pagination (I've already removed the controls from my template), but I would like to use jquery infinite scroll script. But being honest - i have no clue how to even start ;/ mainly because there are no that many live examples/tutorials in the internet.. thanks for any tips

You need to invovle javascript to make infinte scrolling work.
Basically what you need to have:
Page that displays first few posts and loads the infinite scrolling javascript function
Hook on wp_ajax to provide next posts data as you call it
After scroll/"click on load more" you call this with javascript and append the loaded posts at the bottom
Repeat this until all posts are loaded
This should give you a good starting point: https://www.billerickson.net/infinite-scroll-in-wordpress/
Also please don't write your HTML in Wordpress themes/plugins with echo. This is far more readable and helps you to keep your indentation right:
?>
<div id="posts-container" class="fusion-blog-layout-medium fusion-blog-infinite fusion-posts-container-infinite fusion-blog-archive fusion-clearfix">
<?php
$args = array( 'post_type' => 'era', 'post_status' => array('publish', 'future'), 'paged' => $paged );
$custom_query = new WP_Query($args);
while($custom_query->have_posts()) : $custom_query->the_post();
$post_classes = $post_class . ' ' . $alignment_class . ' ' . $thumb_class . ' post fusion-clearfix';
ob_start();
post_class( $post_classes );
$post_classes = ob_get_clean();
?>
<article id="post-<?php echo get_the_ID() ?>" <?php echo $post_classes ?>>
...

Related

Why wp_video_shortcode inside shortcode echoes when calling the wrapping shortcode?

I have a shortcode, which returns html created like this:
public function get_video_grid($the_query) {
$html = '';
if ( $the_query->have_posts() ) {
$count = 0;
while ( $the_query->have_posts()) : $the_query->the_post();
$attachment = get_attached_media( 'video' );
$post_ID = get_the_ID();
global $wpdb;
$video_url = wp_get_attachment_url($post_ID);
$video_attr = array(
'src' => $video_url
);
$item = $wpdb->get_row(
"
SELECT video_start_time
FROM " . $wpdb->prefix . "lc
WHERE attachment_id = $post_ID
",
ARRAY_A
);
$time = $item['video_start_time'];
$html .= '<div class="video-thumb-wrapper">';
$html .= '<h4 class="grid-title">' . get_the_title() . '</h4>';
$html .= '<span class="grid-timestamp">' . gmdate('d.m.Y H:i', strtotime($time)) . '</span>';
$html .= '<div class="aspect-ratio">';
$html .= wp_video_shortcode( $video_attr );
$html .= '</div>';
$html .= '</div>';
$count++;
endwhile;
}
return $html;
} // get_video_grid
However, if I am using the shortcode either in article or in do_shortcode(), the video elements are rendered outside of the containers on some pages. On some WP installations this works just fine. The version of WP does not seem to affect.
If I try to store the html string to variable like this:
<?php
/**
* Template Name: Modular Page
*/
get_header(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post();
if ( post_password_required() ) {
echo get_the_password_form();
} else {
lobo_modular_content( $post );
}
endwhile;
$content = do_shortcode("[lilicast_list]");
get_footer(); ?>
the videos are still rendered. Notable here is that nothing should be echoed at this point, yet still I have the elements in my html. As far as I know, all I am asking is the wp to return me a string.
Why is the wp_video_shortcode() echoed before anything else? Why the behaviour is not consistent with just adding the shortcode inside of the post?

wordpress Post a different post assigned inside the category of html

Hello there good programmers, I have 3 different sections in my webpage. I am using a wordpress loop to post all the post in wordpress. my problem is, i want to assign every post in the other sections or something conditions ?, for example.
For example i have 3 different sections
i have a section id named offer, i want to display the post that is limited only for my section offer page.
<section id = "offer">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category( '3' ) ) : ?>
<div class="post-cat-three">
<?php else : ?>
<div class="post">
<?php endif; ?>
</section>
next section is the section id named projects, i want to display a post that is intended for my projects sections only. and put in inside my section named
<section id="projects">
</section>
The last section is the contact section where i put my contact details and other informations, and i want it to be assigned in contact section.
<section id = "contact">
</section>
You can do it as #Omar Faruque says or do one single query, then store every section content on a variable and then do the rest of the markup, like this:
<?php
$args = array(
//Category Parameters
'category__in' => array(1, 2, 3), //Query only the posts on the categories with IDs 1, 2, 3
//Type & Status Parameters
'post_type' => 'post',
);
$query = new WP_Query( $args );
$category1_posts = "";
$category2_posts = "";
$category3_posts = "";
if($query->have_post()) {
while($query->have_posts()) {
$query->the_post();
$thumbnail_url = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ) );
if(in_category(1)) {
$category1_posts .= '<div class="category1_post">';
$category1_posts .= '<h2>' . get_the_title() . '</h2>':
$category1_posts .= '<img src="' . $thumbnail_url . '">';
//Same for other elements you want here, the_excerpt for example
$category1_posts .= '</div>';
} else if (in_category(2)) {
$category2_posts .= '<div class="category2_post">';
$category2_posts .= '<h2>' . get_the_title() . '</h2>':
$category2_posts .= '<img src="' . $thumbnail_url . '">';
//Same for other elements you want here, the_excerpt for example
$category2_posts .= '</div>';
} else {
$category3_posts .= '<div class="category3_post">';
$category3_posts .= '<h2>' . get_the_title() . '</h2>':
$category3_posts .= '<img src="' . $thumbnail_url . '">';
//Same for other elements you want here, the_excerpt for example
$category3_posts .= '</div>';
}
}
}
wp_reset_query();
//Then just place the sections and echo the posts you have in your variables
?>
<section id="category1">
<?php echo $category1_posts; ?>
</section>
<section id="category2">
<?php echo $category2_posts; ?>
</section>
<section id="category3">
<?php echo $category3_posts; ?>
</section>
Best is make 3 different query for 3 section like below
<?php
/**
* The WordPress Query class.
* #link http://codex.wordpress.org/Function_Reference/WP_Query
*
*/
$args = array(
//Category Parameters
'category__in' => array(1, 2),
//Type & Status Parameters
'post_type' => 'post',
//Order & Orderby Parameters
'order' => 'DESC',
'orderby' => 'date',
//Pagination Parameters
'posts_per_page' => 10
);
$query = new WP_Query( $args );
if($query->have_post()):
while($query->have_posts()): $query->the_post();
// Wirte your html here
endwhile;
endif;
?>

Trying to call post title, excerpt, and permalink from id for shorcode

I was able to get the title and excerpt to display correctly, but I can't figure out which permalink call to use.
function display_excerpt_shortcode( $atts ) {
extract(shortcode_atts(array(
'excerptid' => ''
), $atts));
if ($excerptid) {
$args=array(
'p' => $excerptid,
'post_type' => 'post',
'post_status' => 'publish'
);
$my_query = new WP_Query($args);
if ($my_query) {
$title = apply_filters( 'the_title', $my_query->posts[0]->post_title );
$excerpt = apply_filters( 'the_excerpt', $my_query->posts[0]->post_excerpt );
$link = apply_filters( 'the_permalink', $my_query->posts[0]->post_permalink );
return '<h3>' . $title . '</h3> <p>' . $excerpt . '</p> <a class="button small primary" href="' . $link . '" title="' . $title . '" >Read More </a>';
}
}
return;
}
add_shortcode('display_excerpt', 'display_excerpt_shortcode');
I've tried all kinds of combinations. the_permalink, get_permalink, post_permalink... I just can't figure out if it's the wrong combination or if I'm just completely off the mark. Thanks in advance.
Have you tried:
$link = get_permalink( $my_query->posts[0]->post_ID )
I think your problem is that the query object has no 'permalink' property.
Following the guide in the codex's class reference page, you'll find the pattern of setting a new post object during each iteration of the loop with $the_query->the_post();:
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//Now reference WP post functions:
the_title();
the_permalink();
}
}
Sorry it's a totally different design, but this has never failed me.

WP Query pagination on shortcode

I am creating a shortcode to display the most recent posts in the homepage, however I cannot get the pagination working.
When you click on 'older posts' the page refreshes but the same 2 original posts are displayed.
if ( ! function_exists('vpsa_posts_shortcode') ) {
function vpsa_posts_shortcode( $atts ){
$atts = shortcode_atts( array(
'per_page' => 2,
'order' => 'DESC',
'orderby' => 'date'
), $atts );
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => $atts["per_page"],
'order' => $atts["order"],
'orderby' => $atts["orderby"],
'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : $output;
while ($query->have_posts()) : $query->the_post();
$output .= '<article id="post-' . get_the_ID() . '" class="' . implode(' ', get_post_class()) . '">';
$output .= '<h4 class="post-title"><span>' . the_title('','',false) . '</span></h4>';
$output .= '<div class="row">';
$output .= '<div class="col-md-3">';
$output .= '<a href="' . get_permalink() . '" title="' . the_title('','',false) . '">';
if ( has_post_thumbnail() ) {
$output .= get_the_post_thumbnail( get_the_id(), 'featured', array('class' => 'img-responsive aligncenter'));
} else {
$output .= '<img class="img-responsive aligncenter" src="' . get_template_directory_uri() . '/images/not-available.png" alt="Not Available" height="150" width="200" />';
}
$output .= '</a>';
$output .= '</div>';
$output .= '<div class="col-md-9">';
$output .= get_the_excerpt();
$output .= '<span class="post-permalink">Read More</span>';
$output .= '</div>';
$output .= '</div>';
$output .= '<div class="post-info">';
$output .= '<ul>';
$output .= '<li>Posted: ' . get_the_time("F j, Y") . '</li>';
$output .= '<li>By: ' . get_the_author() . '</li>';
$output .= '<li>Categories: ' . get_the_category_list(", ") . '</li>';
$output .= '</ul>';
$output .= '</div>';
$output .= '</article>';
endwhile;
$output .= '<div class="post-nav">';
$output .= '<div class="prev-page">' . get_previous_posts_link( "« Newer Entries" ) . '</div>';
$output .= '<div class="next-page">' . get_next_posts_link( "Older Entries »", 3 ) . '</div>';
$output .= '</div>';
else:
$output .= '<p>Sorry, there are no posts to display</p>';
endif;
wp_reset_postdata();
return $output;
}
}
add_shortcode('vpsa_posts', 'vpsa_posts_shortcode');
You need to call paginate function.Try following code:
if ( ! function_exists('vpsa_posts_shortcode') ) {
function vpsa_posts_shortcode( $atts ){
$atts = shortcode_atts( array(
'per_page' => 2,
'order' => 'DESC',
'orderby' => 'date'
), $atts );
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => $atts["per_page"],
'order' => $atts["order"],
'orderby' => $atts["orderby"],
'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : $output;
while ($query->have_posts()) : $query->the_post();
$output .= '<article id="post-' . get_the_ID() . '" class="' . implode(' ', get_post_class()) . '">';
$output .= '<h4 class="post-title"><span>' . the_title('','',false) . '</span></h4>';
$output .= '<div class="row">';
$output .= '<div class="col-md-3">';
$output .= '<a href="' . get_permalink() . '" title="' . the_title('','',false) . '">';
if ( has_post_thumbnail() ) {
$output .= get_the_post_thumbnail( get_the_id(), 'featured', array('class' => 'img-responsive aligncenter'));
} else {
$output .= '<img class="img-responsive aligncenter" src="' . get_template_directory_uri() . '/images/not-available.png" alt="Not Available" height="150" width="200" />';
}
$output .= '</a>';
$output .= '</div>';
$output .= '<div class="col-md-9">';
$output .= get_the_excerpt();
$output .= '<span class="post-permalink">Read More</span>';
$output .= '</div>';
$output .= '</div>';
$output .= '<div class="post-info">';
$output .= '<ul>';
$output .= '<li>Posted: ' . get_the_time("F j, Y") . '</li>';
$output .= '<li>By: ' . get_the_author() . '</li>';
$output .= '<li>Categories: ' . get_the_category_list(", ") . '</li>';
$output .= '</ul>';
$output .= '</div>';
$output .= '</article>';
endwhile;global $wp_query;
$args_pagi = array(
'base' => add_query_arg( 'paged', '%#%' ),
'total' => $query->max_num_pages,
'current' => $paged
);
$output .= '<div class="post-nav">';
$output .= paginate_links( $args_pagi);
// $output .= '<div class="next-page">' . get_next_posts_link( "Older Entries »", 3 ) . '</div>';
$output .= '</div>';
else:
$output .= '<p>Sorry, there are no posts to display</p>';
endif;
wp_reset_postdata();
return $output;
}
}
add_shortcode('vpsa_posts', 'vpsa_posts_shortcode');
WP_Query with pagination inside a shortcode
Here I will show you how to create a [shortcode] containing a custom new WP_Query with pagination links.
To create a [shortcode] with a new WP_Query post loop with pagination links built in to turn any is_singular() page or single post into a custom archive based on the WP_Query arguments you set. Lets call it [wp_query_pagination_inside_shortcode], so we add with the following add_shortcode() in functions.php.
Or in my case I create separate /functions/ folder for files to stay organized with the appropriates names and have a require_once include file structure.
root/functions.php ->
require_once 'functions/shortcode-wp-query-pagination-inside-shortcode.php';
root/functions/shortcode-wp-query-pagination-inside-shortcode.php->
add_shortcode('wp_query_pagination_inside_shortcode', 'my_shortcode_function_tag');
if (!function_exists('my_shortcode_function_tag')) {
function my_shortcode_function_tag($atts)
{
ob_start(); // Turn output buffering on, helps with complicated template and theme structures
// Note: https://developer.wordpress.org/reference/functions/get_query_var/#used-by
// Custom query_vars translates to Getting an "archive" on any current page that contains a query with corresponding pagination number in the url
// example.com/page-shortcodes-on/page/2/ ...etc see below how to manipulate this
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$atts = shortcode_atts(
array(
'post_type' => 'post',
// Set attributes here you want in your shortcode
// like [shortcode post_types="post"]
),
$atts
);
// set up the default args you wont change and the ones you will by accessing the value of $atts passed into the shortcode function from your WYSIWYG content editor
$args = array(
'posts_per_page' => 3,
'paged' => $paged, // Important to receive page data
'post_type' => $atts['post_type'], // This is how you get the values of your shortcode attributes passed in
'orderby' => 'date',
'order' => 'DESC',
);
$the_query = new WP_Query($args);
// Da loop
// match your themes loop structure this below is just boiler plate stuff ignore or use your choice its not important how you create the post loop, just do it
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
get_template_part('template-parts/loop');
} //end while
} // endif
$big = 999999999; // need an unlikely integer for how many pages are possible
// I've tested example.com/page-shortcodes-on/page/999999999999999999/ and the custom query_var still works
echo paginate_links(
array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), // referrence the url
'format' => '?paged=%#%', // used for replacing the page number
'current' => max(1, get_query_var('paged')), // grabs the page data
'total' => $the_query->max_num_pages //$the_query is your custom query
)
);
return ob_get_clean(); // Silently discard the buffer contents
wp_reset_query(); // Lets go back to the main_query() after returning our buffered content
}
}
In a WYSIWYG Content editor in pages or posts content editor
[wp_query_pagination_inside_shortcode post_type="posts"]
URL structure for this query_var 'paged'
example.com/page-shortcode-is-on/page/2/
You will need to stylize the pagination links a bit to be acceptable but this should give you a highly useful tool to build custom WP_Query loops with multiple different post_types from anywhere in your site through your WYSIWYG editor. Add more attributes to really make this thing valuable.
// Ignore below, these are just possible search queries for this issue
WP_Query pagination inside a shortcode / add_shortcode with pagination / shortcode with custom attributes / Paginated custom post type wp_query shortcode / custom post type wp_query archive posts per page / page / pagination wont work

Direct a portfolio item to the single project and disable the ajax script wordpress theme

At the moment on my portfolio page you can click on a portfolio item and a ajax container pops up. And in this container you can click on a button to go to the project and read more details.
BUT
I want to disable this ajax container and when you click on a single portfolio item it needs to go straight to the project item page with all the details on it etc.
Now have I been searching in all of the .php files and in the scripts but I just can't find the action when you click on a portfolio item and I'm not really sure with what I need to replace it when found. I hope someone could help me with this one.
Here is the code of the portfolio page:
<div id="portfolio-grid" class="clearfix">
<?php
while ( $portfolio_query->have_posts() ) : $portfolio_query->the_post();
get_template_part( 'includes/entry', 'portfolio' );
endwhile;
wp_reset_postdata();
?>
</div> <!-- end #portfolio-grid -->
And here is the code of the Ajax container -I think-
function et_show_ajax_project(){
global $wp_embed;
$project_id = (int) $_POST['et_project_id'];
$portfolio_args = array(
'post_type' => 'project',
'p' => $project_id
);
$portfolio_query = new WP_Query( apply_filters( 'et_ajax_portfolio_args', $portfolio_args ) );
while ( $portfolio_query->have_posts() ) : $portfolio_query->the_post();
global $post;
$width = apply_filters( 'et_ajax_media_width', 600 );
$height = apply_filters( 'et_ajax_media_height', 480 );
$media = get_post_meta( $post->ID, '_et_used_images', true );
echo '<div class="et_media">';
if ( $media ){
echo '<div class="flexslider"><ul class="slides">';
foreach( (array) $media as $et_media ){
echo '<li class="slide">';
if ( is_numeric( $et_media ) ) {
$et_fullimage_array = wp_get_attachment_image_src( $et_media, 'full' );
if ( $et_fullimage_array ){
$et_fullimage = $et_fullimage_array[0];
echo '<img src="' . esc_url( et_new_thumb_resize( et_multisite_thumbnail($et_fullimage ), $width, $height, '', true ) ) . '" width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" />';
}
} else {
$video_embed = $wp_embed->shortcode( '', $et_media );
$video_embed = preg_replace('/<embed /','<embed wmode="transparent" ',$video_embed);
$video_embed = preg_replace('/<\/object>/','<param name="wmode" value="transparent" /></object>',$video_embed);
$video_embed = preg_replace("/height=\"[0-9]*\"/", "height={$height}", $video_embed);
$video_embed = preg_replace("/width=\"[0-9]*\"/", "width={$width}", $video_embed);
echo $video_embed;
}
echo '</li>';
}
echo '</ul></div>';
} else {
$thumb = '';
$classtext = '';
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Ajaximage');
$thumb = $thumbnail["thumb"];
echo '<a href="'. esc_url( get_permalink() ) . '">';
print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext);
echo '</a>';
}
echo '</div> <!-- end .et_media -->';
echo '<div class="et_media_description">' .
'<h2 class="title">' . '' . get_the_title() . '' . '</h2>' .
truncate_post( 560, false );
echo '</div> <!-- end .et_media_description -->';
echo '<a class="more" href="' . get_permalink() . '">' . __( 'Meer info »', 'Flexible' ) . '</a>';
endwhile;
wp_reset_postdata();
die();
}
You can see the page here: http://bit.ly/10BDVVf
Again thank you!
This is being controlled via javascript in /wp-content/themes/Flexible/js/custom.js. Comment out lines 29-77 and it will link as expected (the return false at the end cancels that standard navigation), but keep in mind that if you edit the theme directly and upgrade it, your changes will be overwritten.

Categories