Wordpress ninjas... I require some help regarding pagination.
I have the following code :
global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array_merge( $wp_query->query_vars,
array(
'post_type' => 'attachment',
'posts_per_page' => 10,
'paged' => $paged
)
);
How come $query->max_num_pages returns 0 ?
And is there a way to alter the original archive query with the $query->max_num_pages ?
[EDIT]
In Addition to the above code I have the following
$output = '<div class="download-attachments left_content_container"><ul>';
//this does not retrieve anything
$query = new WP_Query( $args );
// this does retrieve posts
$attachments = get_posts( $args );
// this block is only used for testing purposes..
// ie. it is not being executed because the query does not return anything, Whilst get_posts() works
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
echo the_ID();
}
}
foreach ($attachments as $attachment) {
setup_postdata($attachment);
//get attachments metadata etc.
$url = wp_get_attachment_url( $attachment->ID );
$title = get_the_title( $attachment );
$caption = $attachment->post_excerpt;
$size = get_readable_size( filesize(get_attached_file( $attachment->ID )) );
$icon = get_icon_for_attachment( $attachment->ID );
$mime = get_post_mime_type( $attachment->ID );
$date_added = $attachment->post_date;
$filename = ( !$caption == '' ) ? $caption : $title ;
$description = ($attachment->post_content != '') ? '<span class="attachment-author"><span class="attachemnt-label">Added by: ' . $attachment->post_content . '</span></span>' : '';
$output .= '<li class="'.$mime.'">
<img class="attachment-icon" src="'.$icon.'" alt="pdf">
'. $filename .' '.$description.'
<span class="attachment-date"><span class="attachment-label">Date added: </span>'.$date_added.'</span>
<span class="attachment-size"><span class="attachment-label">Attachment size: </span>'.$size.'</span></li>' ;
}
$output .= '</ul></div>';
echo $output;
echo '<div class="paging pull-left">';
$paged = $wp_query->get( 'paged' );
if ( ! $paged) {/* do nothing */ ;}
else { bootstrap_pagination(); var_dump($wp_query->max_num_pages); // returns 0 }
echo '</div>';
You have a couple of problems here. You don't need to call the global $wp_query because you are not going to use it. Secondly, array_merge is totally out of place. This should not be used at all in this situation. Your code should look something like this
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'attachment',
'posts_per_page' => 10,
'paged' => $paged
)
);
$query = new WP_Query( $args );
For more info on custom queries with WP_Query have a look here
Related
how to get the current category name of the page inside functions.php?
this is for adding a load more functionality based on a category per page.
this is what my current code looks like
add_filter('query_vars', 'registering_custom_query_var');
function registering_custom_query_var($query_vars)
{
$query_vars[] = 'id'; // Change it to your desired name
return $query_vars;
}
function more_post_ajax() {
global $wp_query;
$ppp = (isset($_POST["ppp"])) ? $_POST["ppp"] : 1;
$page = (isset($_POST['pageNumber'])) ? $_POST['pageNumber'] : 0;
$category = get_category(get_query_var('cat'));
header("Content-Type: text/html");
$args = array(
'post_type' => 'post',
'posts_per_page' => $ppp,
'category_name' => $category->name,
'paged' => $page,
);
$loop = new WP_Query($args);
$out = '';
if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
$out .= '<div class="article_card"><div class="article_card__thumbnail">' . get_the_post_thumbnail() . '</div><div class="article_card__content"><h3>' . get_the_title() . '</h3><p class="body-small">' . wp_trim_words( get_the_excerpt(), 60 ) . '</p><a a href="' . get_the_permalink() . '"> Learn more →</a></div></div>';
endwhile;
endif;
wp_reset_postdata();
die($out);
}
add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax');
add_action('wp_ajax_more_post_ajax', 'more_post_ajax');
Are you looking for something like this?
<?php
$post = get_post();
if ( $post ) {
$categories = get_the_category( $post->ID );
var_dump( $categories );
}
How can i get the messages from a certain form?
With this i can get all the inbound messages:
<?php
$args = array(
'post_type' => 'flamingo_inbound',
'post_status' => 'publish'
);
$query = new WP_Query($args);
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
echo '<article>';
echo get_the_title();
echo '</article>';
}
}
wp_reset_postdata();
?>
But i wanna show only the messages from a specific form for the current month. I search on Google and here but i can not find any example or anything for it so i hope somebody knows.
I have made a solution, maybe not the nicest php writing but it works:
<?php
$currentmonth = date('m');
$args = array(
'post_type' => 'flamingo_inbound',
'post_status' => 'publish',
'monthnum' => $currentmonth
);
$query = new WP_Query($args);
if( $query->have_posts() ){
echo '<ul>';
while( $query->have_posts() ){
$query->the_post();
$results = get_post_meta( get_the_ID() );
foreach($results['_meta'] as $result) {
$normaldata = unserialize($result);
if ($normaldata['post_id'] == '1234') { // The id of the post where the form is send from
$title = get_the_title();
echo '<li>' . $title . '</li>';
} else {
}
}
}
echo '</ul>';
}
wp_reset_postdata();
?>
I'm trying to get the featured image from the postid passed through the url.
http://www.example.com/schedule-appointment/?postid=589
I've managed to get the postid from the url, but everything goes down hill from there. I must be missing something simple. I'm not a programmer...would love some help.
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');
function cf7_add_post_id(){
$Path=$_SERVER['REQUEST_URI'];
$control = array();
$control = explode('?', $Path);
$get = $control[1];
$get = explode('=', $get);
$get2 = $get[1];
$args = array(
'post_type' => 'page',
'post__in' => $get2,
);
// Fire up the Query
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ): $the_query->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->$get2) );
echo '$feat_image';
};
Try this
<?php
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');
function cf7_add_post_id(){
$ID = isset( $_GET["postid"] ) ? $_GET["postid"] : false;
if( $ID ){
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $ID ), 'full' );
$url = $thumb['0'];
echo "<img src ='".$url."' alt = 'Image'>";
}
}
?>
There is no need for the WP_Query , You have one id and you can easily get this done by using following code,
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');
function cf7_add_post_id(){
$postid = $_GET['postid'];
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($postid) );
echo '$feat_image';
};
Pagination Doesn't work with word press custom post.
i have tried these code below,but pagination doesn't appear.
tried different query but no result.
i am new in php and word press.i just copy and past code.
can anyone please help me?
what i have done so far are below.
in function.php
/*pagination*/
function wpbeginner_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
in custom post query
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'post_type'=> 'latestnews');
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
?>
<div class="page_news">
<div class="single_page_news">
<h2><?php the_title(); ?><h2>
<p><?php the_content(); ?></p>
</div>
</div>
<?php endforeach; ?>
<?php wpbeginner_numeric_posts_nav(); ?>
please help me
Use These code
$paged = ( get_query_var('page') ) ? get_query_var('page') :1;
$query = new WP_Query( array( 'posts_per_page' => 1,'paged' => $paged,'post_type' => 'achievements','orderby' => 'date', 'order' => 'ASC' ) );
while ( $query->have_posts() ) : $query->the_post();
endwhile;
Instead of
$args = array( 'posts_per_page' => 5, 'post_type'=> 'latestnews');
$myposts = get_posts( $args );
Follow this link
http://thenetapp.com/2014/01/how-to-list-wordpress-posts-with-pagination/
Your pagination function is only set up for the default main query, not for a custom query.
Also, don't use get_posts for paginated queries. It it is nice function to use for a custom query, but it becomes a bummer to work with once you require pagination.
Rather use WP_Query for paginated queries, it is much easier to use.
Example:
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$args = array(
'posts_per_page' => 1,
'paged' => $paged,
'post_type' => 'YOUR POST TYPE'
);
$q = new WP_Query($args);
if($q->have_post()) {
while($q->have_posts()) {
$q->the_post();
//YOUR LOOP
}
//YOUR PAGINATION
}
wp_reset_postdata();
You can have a look at the codex for extra parameters.
You now need to change every instance of $wp_query in your pagination function to $q for it to work.
Just a point of note, you don't have to call the $post global
EDIT
From your comments, there is a much easier way to accomplish your goal without any custom query
This page is an archive page that is meant to display your custom post type latestnews. You can simply just rename your archive-custom.php to archive-latestnews.php. See the Template Hierarchy. Just make sure has_archive is set to true when you register your post type
You should also never shop the main query for a custom query on any type of archive page. It is always troublesome, as you can see. So, delete your custom query and replace it with the default query
This is all you should have in your archive page
if(have_post()) {
while(have_posts()) {
the_post();
//YOUR LOOP
}
//YOUR PAGINATION
}
Just change all the instances of $q back to $wp_query again. Everything should work then
For extra info, you have to check out this post I've done on WPSE
I' am learning how to make a WordPress Plugin. I did make few simple plugins but not as complicated as this one. It's a Events Calendar. The var_dump from the function "nc_get_start_date()" on the page it outputs wrong dates.
The output from the var_dump(nc_get_start_date());
string(32) "1970-01-01,1970-01-01,1970-01-01"
This is what the function should return in real
23-12-2013, 25-12-2013, 26-12-2013
In the function.php on the plugin folder. This is the codes
/* Query to get the events post from the database */
function get_nc_events(){
global $post;
$query = new WP_Query(
array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC'
)
);
return $query;
}
/* Get the start date from the above function */
function nc_get_start_date(){
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
$wnc_start_date = $wnc_start_date[0];
$wnc_start_date = date("Y-m-d", strtotime($wnc_start_date));
$wnc_start_date_array .= "$wnc_start_date,";
endwhile;
return rtrim($wnc_start_date_array, ",");
}
When I write the code in page-caledar.php without the function it renders everything prefectly.
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
echo $wnc_start_date = $wnc_start_date[0] . "<br/>";
endwhile;
Problem Solved. Thanks everyone. The problem was in this function
/* Get the start date from the above function */
function nc_get_start_date(){
global $post;
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
$wnc_start_date = $wnc_start_date[0];
$wnc_start_date = date("Y-m-d", strtotime($wnc_start_date));
$wnc_start_date_array .= "$wnc_start_date,";
endwhile;
return rtrim($wnc_start_date_array, ",");
}
I didn't make global $post;