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;
Related
I have created static page templates in Wordpress which named page-slider. PHP, page-images. PHP, etc. kindly help me how to call these pages collectively on index.php by using a loop through get the template part?
<?php
$arg = array(
'post_type' => 'pages',
);
query_posts($arg);
while (have_posts()) : the_post();
get_template_part( 'page-', 'the_slug()' . '.php' );
endwhile;``
?>
I have tried like above code
You just need to add the conditions for each page while template call.
<?php
$arg = array(
'post_type' => 'pages',
);
query_posts($arg);
while (have_posts()) : the_post();
if(is_page('slider')){
get_template_part( 'page', 'slider' );
}
if(is_page('images')){
get_template_part( 'page', 'images' );
}
endwhile;
?>
You can use the same format in index.php file
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 ...
I have a template that displays the titles of a custom post type. I have approximately 60 posts in the group. A page is set to display 10 posts per page. This yields six pages to display all the titles but I'm having trouble getting the navigation (page to page) to function.
In the code below I get the first set of titles returned but clicking on the 'previous' 'next' links only displays the same set of titles.
Searching around I can't find any definitive solutions on how to format a template to navigate between groups of titles for a custom post type.
I did find that CPTs need to have the max_num_pages defined. So if I insert a value in place of $max_num_pages I do get a result where you can click from page to page but once you reach the end of the list you click onto an empty page.
<h2>Glossary</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'glossary', 'posts_per_page' => 10, 'paged='.$paged, 'orderby' => 'title', 'order' => 'ASC' ) ); ?>
<?php $max_num_pages=$loop->max_num_pages ?>
<p>Maximum Number of Pages Value: <?php echo $max_num_pages;// This line for debug purposes only?></p>
<?php if ( have_posts() ) : ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('Previous entries',$max_num_pages) ?></div>
<div class="alignright"><?php previous_posts_link('Next entries',$max_num_pages) ?></div>
etc..
What code do you use to allow users to click from one group of title to another?
Thanks
Try adding the following before your WP_Query:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
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 this code below that querys posts and a post type:
<?php
$args = array('post_type' => 'apartmentlisting', 'parent' => 0, 'showposts'=>'-1');
query_posts($args);
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
I am trying to query and return only the parent pages I have 10 parent pages and each have about 4-5 child pages. Any ways to return just the parents?
I have been digging on the codex on WP and on google and nothing. I only found articles on returning posts that have a parent of page ID XX.
Any ideas?
If there is no parent, the parent is zero. So your query should work. But the parameter is 'post_parent' not 'parent'. And 'showposts' is deprecated, use 'posts_per_page' instead. So try this:
$args = array('post_type' => 'apartmentlisting', 'post_parent' => 0, 'posts_per_page'=>'-1');
Have you looked into using the following:
<?php get_post_ancestors( $post ) ?>
where $post is the post id? You can get the post's ID with the following if you are within an action hook for the_content:
global $wp_query;
$page = $wp_query->page->ID;