I added pagination with paginate_links( $args_pagi), but the result pages do not have the paginated content, instead they have the same content.
global $wp_query;
$args_pagi = array(
'base' => add_query_arg( 'paged', '%#%' ),
'total' => $posts->max_num_pages,
'current' => $paged
);
$html .= '<div class="post-nav">';
$html .= paginate_links( $args_pagi);
// $output .= '<div class="next-page">' . get_next_posts_link( "Older Entries »", 3 ) . '</div>';
$html .= '</div>';
else:
$html .= '<p>Sorry, there are no posts to display</p>';
endif;
This is the code I added to get pagination. This is done on front page, Do I need to add anything extra to my theme page, etc? to functions.php??
Related
so I have this code below that is showing on my career page.
I have a title content on the left and on the right, a button, and when clicking on it, it shows the full content of the article.
function myWebsite_careers_function() {
$markup = '';
$args = array(
'post_type' => 'career',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'post_date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ($loop->found_posts > 0) {
$markup .= '<div class="container">';
while ( $loop->have_posts() ) : $loop->the_post();
$link = get_the_permalink();
$markup .= '<div class="row pt-3 pb-3">';
$markup .= '<div class="col-8">';
$markup .= '<h4>' . get_the_title() . ',<br>' . get_field( "location" ) . '</h4>';
$markup .= '</div>';
$markup .= '<div class="col-4">';
$markup .= '<h4 style="text-align: right;">' . __("learn more", "myWebsite") . '</h4>';
$markup .= '</div>';
$markup .= '</div>';
endwhile;
wp_reset_postdata();
$markup .= '</div>';
}
return $markup;
}
add_shortcode( 'myWebsite_careers', 'myWebsite_careers_function' );
I can translate all my content except the button "learn-more" (code upper).
I would like to know how to get "aprende más" instead of "learn more" when it is in the Spanish version.
I hope you can help me with that, thank you in advance. Cheers!
I found the solution, it was in my WPML plugin, so I clicked on "theme and plugins localization" and then scanned my theme => string translation => and search the string in the list. Translate and its work. thank you for your help cheers! –
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 ?>>
...
I have an issue with a shortcode that I'm writing for wordpress. I'm trying to use the get_the_content() function but instead of pulling the contents of the custom post type I've created it's pulling the contents of the page the shortcode is sitting on. Other functions are all working fine e.g. get_the_title() and get_the_post_thumbnail(). I pass the ID in the functions and it works great for everything else, just not get_the_content.
get_the_content($testimonial_item->ID);
The shortcode contains pagination and other elements which all work correctly, It's just this one function that causing me grief. The full code is below, any help would be greatly appreciated.
function ncweb_display_testimonial_items($atts, $content = null) {
extract( shortcode_atts( array(
'per_page' => 6
), $atts ) );
/* pagination parameters */
// check what page we are on
if ( isset ($_GET['testimonial-page'] ) ) $page = $_GET['testimonial-page']; else $page = 1;
// default number of pages
$total_pages = 1;
// portfolio offset. Used in the get_posts() query to show only portfolio for the current page
$offset = $per_page * ($page-1);
// setup the portfolio args for retrieving the total number of portfolio items
$testimonial_count_args = array(
'post_type' => 'ncweb-testimonials',
'posts_per_page' => -1
);
$testimonial_count = count(get_posts($testimonial_count_args));
// calculate the total number of pages
$total_pages = ceil($testimonial_count/$per_page);
/* end pagination parameters */
// main image query
$testimonial_args = array(
'post_type' => 'ncweb-testimonials',
'numberposts' => $per_page,
'offset' => $offset
);
$testimonial_items = get_posts($testimonial_args);
// start our output buffer
ob_start();
if($testimonial_items) :
/*** main portfolio loop ***/
$counter = 1;
echo '<div class="testimonial-items" id="testimonial-items">';
foreach($testimonial_items as $testimonial_item) :
$testimonial_company = get_post_meta($testimonial_item->ID, 'ncweb_testimonial_company', true);
$testimonial_client = get_post_meta($testimonial_item->ID, 'ncweb_testimonial_client_name', true);
echo '<aside class="testimonial-list-item row">';
echo '<div class="col-xs-12 testimonial-list-item-info">';
echo '<div class="testimonial-image">'. get_the_post_thumbnail($testimonial_item->ID) .'</div>';
echo '<div class="testimonial-client"><span class="testimonial-client-name">'. $testimonial_client .'</span><br/><span class="testimonial-company">'. $testimonial_company .'</span></div>';
echo '</div>'; //end of testimonial-list-item-info
echo '<div class="col-xs-12 testimonial-item-content">'. get_the_content($testimonial_item->ID); .'</div>';
echo '</aside>';
$counter++;
endforeach;
echo '</div>';
/*** display pagination ***/
// pagination base
echo '<div id="testimonial-pagination">';
$base = get_permalink(get_the_ID()) . '%_%';
echo paginate_links( array(
'base' => $base,
'format' => '?testimonial-page=%#%',
'prev_text' => __('Previous', 'ncweb'),
'next_text' => __('Next', 'ncweb'),
'total' => $total_pages,
'current' => $page,
'end_size' => 1,
'mid_size' => 5
));
echo '</div>';
/*** end pagination display ***/
else :
echo '<p>' . __('No testimonial items found.', 'ncweb') . '</p>';
endif; // end if($images)
return ob_get_clean();
}
add_shortcode( 'testimonial-items', 'ncweb_display_testimonial_items' );
This is because the get_the_content() function is a wrapper and it's only arguments are the Read More text and the stripteaser argument.
What you actually want is.
$post= get_post($testimonial_item->ID);
$testimonial_items = $post->content ;
$testimonial_items = apply_filters('the_content', $testimonial_items);
Then you will have what you are looking for.
You can try this code to get the perfect output when using shortcode in content.
<?php
ob_start();
the_content();
$content_output = ob_get_clean();
echo $content_output;
?>
Cleanest way to output the_content using a shortcode is this:
function my_sc_content() {
ob_start();
$my_content = the_content();
echo $my_content;
$output_string = ob_get_contents();
ob_end_clean();
return $output_string;
}
add_shortcode('my-content','my_sc_content');
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
I am trying to create my blog feed on a home page(static page) using a shortcode.
So far I have managed to make it display titles, but I would also like it to display like 250 characters of the entry content under each displayed post title.
In other words, I just need it to display title and first few sentences.
Is it even possible?
This is the code used to create the feed.(it displays predefined number of latest posts titles inside tag)
function getblogposts($atts, $content = null) {
extract(shortcode_atts(array(
'posts' => 1,
), $atts));
$return_string = '<h3>'.$content.'</h3>';
$return_string .= '<ul>';
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
if (have_posts()) :
while (have_posts()) : the_post();
$return_string .= '<li>'.get_the_title().'</li>';
endwhile;
endif;
$return_string .= '</ul>';
wp_reset_query();
return $return_string;
SOLVED
Special thanks to pmandell and gtr1971.
Complete feed.php code which creates feeds limited to 100 characters.
<?php
function getblogposts($atts, $content = null) {
extract(shortcode_atts(array(
'posts' => 1,
), $atts));
$return_string = '<h3>'.$content.'</h3>';
$return_string .= '<ul>';
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
if (have_posts()) :
while (have_posts()) : the_post();
$return_string .= '<li>'.get_the_title().'';
$return_string .= '<div class="excerpt">' . get_the_excerpt() . '</div></li>';
endwhile;
endif;
$return_string .= '</ul>';
wp_reset_query();
return $return_string;
}
function new_excerpt_more( $more ) {
return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">[...]</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 100 );
?>
Yes, of course this is possible. You are using a WordPress loop, so just use get_the_excerpt().
while (have_posts()) : the_post();
$return_string .= '<li>'.get_the_title().'';
$return_string .= '<div class="excerpt">' . get_the_excerpt() . '</div></li>';
endwhile;
You can use the new_excerpt_more filter to control what displays at the end of the excerpt. Here is an example where "[...]" displays at the end of the excerpt, as a link to the post.
function new_excerpt_more( $more ) {
return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">[...]</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );
You can use the the_excerpt() function and limit its length. Look here in the Wordpress Codex for more info Function Reference: the_excerpt
Control Excerpt Length using Filters: By default, excerpt length is
set to 55 words. To change excerpt length to 20 words using
excerpt_length filter, add the following code to functions.php file in
your theme.
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
use substr($text, 0, 250). It means You want to cut the string from position 0 to 250.