Good afternoon all,
I have a custom taxonomy called 'Gallery' and a have created a new page template to pull in and paginate all posts in the 'gallery' taxonomy.
This works fine (as a page), however I would like to set this page as the WordPress static 'Front page'.
When I set this page template as the 'Front page' the pagination no longer works. I have tried many solutions today and would really appreciate some help on this one!
Any help/tips massively appreciated!
Thanks.
My Code:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
'post_type' => 'gallery',
'paged' => $paged,
'orderby' => 'menu_order',
'order' => 'DESC'
);
query_posts( $args );
if ( have_posts()) : while( have_posts() ) : the_post(); ?>
<!-- List Posts -->
<?php endwhile; ?>
<nav>
<?php previous_posts_link( __( 'Previous', 'framework' ) ); ?>
<?php next_posts_link( __( 'Next', 'framework' ) ); ?>
</nav>
<?php else : ?>
<!-- No Posts -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>
EDIT:
If I add global $paged; before the if statements it does works perfectly. Could anyone educate me on what I was missing?
Also what are the performance implications (if any) of calling global $paged; on the homepage?
Add global $paged; before any code runs.
global $paged works throughout the WordPress, to access this we need to put it before usage like your case is. Yes, there is no issue at all to use this variable and you are in totally safe hands. Actually, $paged variable is getting posts for each page and once you are using this variable its showing up data appropriately.
There are other global variables as well to be used in WordPress, for whole list you can visit this official codex page https://codex.wordpress.org/Global_Variables
I hope after reading this comment and going through this URL your all points will be solved and you can utilize all this info in future as well with best approach ...
Related
If anyone out there is familiar with Event Organiser templates. I could use some help. I am trying to limit it to 5 per page and add custom pagination. I can't seem to edit the loop in eo-loop-events.php file to add a custom query to limit it to 5 per page.
I've tried the shortcode and looked at their documentation all afternoon and am getting nowhere. I figure there is probably an easy way to do this and I am missing it or I can't do this with the free version.
Any help would be appreciated.
I just had to implement pagination for my wp events organiser template. This is inside widget-event-list.php but will probably work in other templates. This is copy pasted from a working example within the template.
This utilizes the WP_Query loop instead of the $eo_event_loop() that the template uses by default. The WP_Query loop has arguments to get "event" data only.
Fortunately, the event organiser functions (like eo_get_event_datetime_format()) still work.
In this example, I am only outputting the title in the <h4> tag.
This solution was inspired by this answer to another question.
<h1>Paginated Query Area</h1>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type'=>'event', // Your post type name
'posts_per_page' => 3,
'paged' => $paged,
);
$loop = new WP_Query( $args );
?>
<?php if ( $loop->have_posts() ): ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
//Generate HTML classes for this event
$eo_event_classes = eo_get_event_classes();
//For non-all-day events, include time format
$format = eo_get_event_datetime_format();
?>
<h4 class="event-title"><?php the_title(); ?></h4>
<?php endwhile; ?>
<div class="upcoming-pagination">
<?php // Pagination display
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
</div>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
I've visited a number of previous entries and didn't find the solution still. Others have the same problem but their findings weren't applicable to this case. So I'm hoping someone can give me a hint with this issue. :(
So I have a custom query and already added in the paged argument:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$snapshotargs=array(
'posts_per_page' => 10,
'post_type' => 'qd_galleries',
'qd_galleries_cat' => 'snapshot',
'paged' => $paged
);
$snapshotposts = new WP_Query( $snapshotargs);
Then, between closing the loop's while and if (as stated in the codex), I placed the navigation links and reset the query:
<?php } ?><!-- end while -->
<nav id="page_nav">
<?php get_previous_posts_link('More »') ?><br/>
<?php get_next_posts_link('More »') ?>
</nav>
<?php wp_reset_postdata(); ?>
<?php }; ?><!-- end if -->
I even tried with next_posts_link() instead of get_next_posts_link().
Both of them return blank, empty page_nav div, even though there are almost 150 posts to show and pagination is set to 10.
What could the problem be? Am I missing something?
Thank you in advanced for your support!
Here's the full functional code you can modify to fit your needs:
// Note that on your site's blog page the query var is page, not paged
// everywhere else it is paged
$curpage = (get_query_var('page')) ? get_query_var('page') : 1;
$snapshotargs=array(
'posts_per_page' => 10,
'post_type' => 'qd_galleries',
'qd_galleries_cat' => 'snapshot',
'paged' => $curpage
);
query_posts($snapshotargs);
$pagecount = $wp_query->max_num_pages;
while(have_posts()):the_post();?>
<h1><a href='<?php the_permalink();?>'><?php the_title();?></a></h1>
<p> the_excerpt();?></p>
<?php endwhile; ?>
// Note that the pagination is outside of the loop
<p class='pagination-links'>
Showing page <?=$curpage;?> of <?=$pagecount;?>
<?php previous_posts_link();?> <?= ($curpage > 1 && $curpage != $pagecount)?'|':'';?> <?php next_posts_link(); ?>
</p>
Instead of using index.php as my front page, I've created a custom page and changed the "Front page displays" setting in the "Reading Settings" tab accordingly.
Loops don't seem to be working with my custom front page, although they still work on index.php.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<--- POST CONTENT -->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
What might I be doing wrong? Is there some other bit of code I need to include?
UPDATE:
To clarify, I need for the home page to be a custom page because I'm using custom fields so the client can edit bits of content easily enough. This is why I'm not using index.php or home.php
If you are trying to make a custom homepage you can create a home.php and this will be used instead.
If you are using more than 1 loop make sure your reset the loop.
As I understand you need to tell the loop what it has to show in page templates.
So simply use a custom query in that custom page like this:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'post_status'=>'publish',
'posts_per_page'=> get_option('posts_per_page'),
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC'
);
$wp_query=new WP_Query($args);
while ( have_posts() ) : the_post();
...
endwhile;
How do I get a random post in Wordpress?
I would like to display a button on a page that, when pressed, goes to a random post from the blog. I don't want a random post to be displayed on the page, I just want a link that leads to that post.
I tried searching for a code on Google and here at stackoverflow but no success.
Thanks...
UPDATE:
Here is my template code:
<?php /*Template Name: Random*/ ?>
<?php get_header(); ?>
<nav><?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?></nav>
<div id="main-content-archive">
<div class="grey-text">Random post</div>
<?php $query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );?>
<?php if (have_posts()) : while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
?>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
create a page template, and use the following code to get a random post:
//Create WordPress Query with 'orderby' set to 'rand' (Random)
$the_query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );
// output the random post
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
then in a page, just use:
see a random post
I found this post which gave me desired results...
Here's a solution copy/pasted from the wpbeginner blog post. No copyright infringement intended.
Just add the following code to the functions.php file:
add_action('init','random_add_rewrite');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}
Use mydomain.com/random/ as your href for your button that leads to the random post.
Thanks everyone who contributed for your help...
Cheers!
I find it is more useful to have a URL that will redirect to a random post that you can use as link in sidebar or in menus. If it is a single WP site and even on wp.com it's really easy, for a blog at
http://mygroovywpsite.me/
All you need to do is append it with ?random
http://mygroovywpsite.me/?random
I found this did not work (nor the wp_beginner code above) on subsites in my multisite installation, either approach just loaded the home page. Maybe I had some funky cache issues. The way I do this on many sites is a few more steps w/o plugins.
First make a Page in your site called "Random" / with the slug "random" -- it does not need any content in it
Then create a page-random.php template
<?php
/*
Random Post Picker
Use on page to send viewer to random post optionally mod query
*/
// set arguments for WP_Query on published posts to get 1 at random
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'rand'
);
// It's time! Go someplace random
$my_random_post = new WP_Query ( $args );
while ( $my_random_post->have_posts () ) {
$my_random_post->the_post ();
// redirect to the random post
wp_redirect ( get_permalink () );
exit;
}
?>
Then you get the re-direct for any link on your blog ...../random w/o any wrestling with .htaccess
I've done it this way because I've had to modify the query, sometimes for custom post type, sometimes to restrict to category, etc.
I only had one site that was a problem because the hosting suppressed the use of mySQL queries with ORDER BY RAND()
Another Simple solution to display Random Post
1.First a create a custom page template. Name it as random post or a name of your choice!
2.Open the page and remove the default wp loop and Paste the code below
3.To change the no of post change the number ‘1’ to your choice!
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
source: http://www.yengkokpam.com/displays-random-posts-in-a-page/
Check This
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
I have a template named "Foo" in "foo.php", I would like to be able to select all pages that are using that template. I have searched for awhile now but have not been able to find a successful way to do this... Can somebody enlighten me on the proper/only way to do this?
You can get this by using following code
$query = new WP_Query( array( 'meta_key' => '_wp_page_template', 'meta_value' => 'foo.php' ) );
if ( have_posts() ) while ( have_posts() ) : the_post();
<?php the_title(); ?>
<?php endwhile; // end of the loop. ?>
Robot's answer is good, but I thought I'd clarify a few things.
First, You should use the variable for the query you created, so it would be $query->have_posts() etc.
Second, you should specify post_type. I used any, so that it will pull any post types except for revisions.
Last, if this is in a page with any other WP loops, you may want to use wp_reset_query. I added one below and one above just in case, but you only really need this if you have another loop above or below. Remove it if you don't.
Here is the code:
wp_reset_query();
$query = new WP_Query( array(
'post_type' => 'any',
'meta_key' => '_wp_page_template',
'meta_value' => 'foo.php'
) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post(); // WP loop
the_title();
endwhile; // end of the loop.
} else { // in case there are no pages with this template
echo 'No Pages with this template';
}
wp_reset_query();
Hope that helps someone!! Happy coding!
This also works
$pages = get_pages(
array(
'meta_key' => '_wp_page_template',
'meta_value' => 'template.php'
)
);
foreach($pages as $page){
echo $page->post_title.'<br />';
}
http://jorgepedret.com/old/web-development/get-pages-by-template-name-in-wordpress/