Is there any way to have a different post appear each time someone refreshes the home page of my self-hosted Wordpress blog?
Currently I have the home page set to show only 1 post, but it's only the latest one. I would like it to be different each time someone visits my site, to pull a post randomly from the archive of all posts.
Here's what the loop currently looks like in the theme I'm using:
<?php
}
// Load main loop
if ( have_posts() ) {
// Start of the Loop
while ( have_posts() ) {
the_post();
?>
Is there any way to achieve this?
I also don't want to keep the "Blog pages show at most 1 post" setting since that messes up my search results (every result is on a separate page) but I only want 1 post to show up on the homepage loop.
The best way is to modify the global query by hooking on pre_get_posts.
Insert this code into your theme's functions.php:
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts($query) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set('orderby', 'rand');
}
}
This will check if you're on home page, and if it is the main query (so only the main posts query is targeted), and if that is true, will set the orderby to rand, so each time a random post will appear on the home page.
Please, note that if you have pagination on your home page, this will always order your posts in random order, so in this case you might want to build a custom query over your loop, by using the WP_Query class or query_posts().
<li><h2>Random Post</h2>
<ul>
<?php $posts = get_posts('orderby=rand&numberposts=5'); foreach($posts as $post) { ?>
<li><?php the_title(); ?>
</li>
<?php } ?>
</ul>
</li>
Refer http://codex.wordpress.org/Template_Tags/get_posts
Related
I have a custom taxonomy called Topics.
Topics currently has three categories:
Note the count of posts for each category above.
When a user goes to a topic page, i.e. /topics/news, I want to show all posts related to news neatly, so looking to write custom markup.
To do this, I have come across taxonomy templates, but getting weird results.
For starters, I'm on /topics/news. From the above image, you can see News has 2 posts.
Here is my taxonomy-topics.php file:
<?php get_header();
if ( have_posts() ){
while ( have_posts() ) {
the_title();
}
}
get_footer(); ?>
Just trying to show the title of the news posts at the moment. However, it is looping through and printing the title for one post several times. Seems like there's an infinite loop happening.
You must call the_post() so that the post index is moved to the next one in the posts array in the main query (i.e. the $wp_query global):
while ( have_posts() ) {
the_post(); // call this or you'll be stuck in an infinite loop! :)
the_title();
}
I am trying to change the HTML structure and CSS styles of the first 4 posts in the main WP_Query in archive.php
I am doing this simple thing where I checked the global $wp_query variable.
if ( have_posts() ) :
if( 4 > $wp_query->current_post ) :
the_title();
endif;
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
else :
get_template_part( 'no-results', 'archive' );
endif;
This works fine, the first 4 posts in the query get displayed in whatever HTML/CSS I apply to them before the get_template_part() gets called in.
The problem is when I go to the next page in the pagination, a different set of 4 posts get displayed. The 4 new posts of the second page in the pagination.
I don't want that. I want the same 4 posts that appear on the first page to keep appearing as I go to the next or previous pages. I need to give the first 4 posts a different HTML structure, not just CSS styling and I need them to persist throughout the pagination.
I tried changing the main query with pre_get_posts and using offset but that gave me a set of problems in the theme and the admin panel that I decided against it.
How may I achieve that?
EDIT My first attempt at this problem was to do a second query and leave the main query intact but then I wouldn't be able to check the post_count in the first query to see if it's bigger than 4 because I'm always showing only 4 posts_per_page that's why I need them to be in the same query because I'm going to hide the first 4 posts on the category page that doesn't have more than 4 posts and only show them on the category page that has more than 4 posts.
EDIT 2 To make this simpler to understand, if it's getting too messy.
IF CATEGORY (QUERY) HAS MORE THAN 4 POSTS
DISPLAY 4 POSTS WITH CUSTOM HTML/CSS
THEN GET TEMPLATE PART AND DISPLAY THE REST OF THE POSTS WHILE EXCLUDING THE FIRST 4 POSTS BECAUSE DUPLICATES
ELSE
DISPLAY DEFAULT TEMPLATE PART
Here's a loop I use to show the first four posts, it has a wp_reset_postdata that might be required so your pagination loop is unaffected.
<?php $rp_query = new WP_Query( 'showposts=4' );
if ( have_posts() ) : while ( $rp_query->have_posts() ) : $rp_query->the_post(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<? endif; ?>
Solution is already built-in and available as plugin.
Please try Blog Designer PRO plugin - https://codecanyon.net/item/blog-designer-pro-for-wordpress/17069678?ref=miyanialkesh7
Best regards,
Alkesh
I have created a news dispatches post category. I also created a news releases page, this is the page where I want to show my news dispatches post category posts. How can I show my news dispatches posts in news releases page using post ID or using page permalink in a query? Here is my example code.
add_action('pre_get_posts', 'ad_filter_categories');
function ad_filter_categories($query) {
if ($query->is_main_query() && is_home()) {
$query->set('category_name','news dispatches');
}
}
How can I replace the is_home code line with the page/post ID or the page permalink? Thanks for the help.
Can't use is_page in pre_get_posts (see http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts).
So in the news releases page, add this in:
// Modify the page query
query_posts( 'category_name=news-dispatches');
// The Loop
if ( have_posts() ) {
echo '<ul>';
while ( $have_posts() ) {
the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
UPDATE:
To answer this question: "What php file I am gonna look into to insert this code? ":
It's hard to give exact instructions because it really depends on how your theme is setup.
You need to create a page called page-news-releases.php. See http://codex.wordpress.org/images/1/18/Template_Hierarchy.png for an explanation of page templates. So when wordpress shows the page with the slug news-releases, it will use this file instead of the default.
You'll put my code in this new page for the main loop. You'll also need to look at your other theme files and copy code that needs to go before and after it (for your header and footer and that sort of stuff). It's a little out of the scope of stackoverflow to teach wordpress template design, but hopefully this will get you started. Good luck!
I did find one topic that sort of touched base on this but if there is a better link please advise.
I have a self hosted Wordpress website set up typically for a static home page and a separate page that shows recent posts called "Reviews". I need to write posts under a category "Recent News" that would be excluded from the recent post and shown on their own page, a second blog page.
The Codex tells me to add this to the index.php to exclude the category "recent-news":
<?php
if (is_home()) {
query_posts("cat=-3");
}
?>
with 3 being "recent-news". This does nothing no matter where I place it on the page. It still shows up with the recent posts. I'm guessing I'm in the wrong section for excluding categories in the Codex.
How do I exclude a category from recent posts, add a new page called "Recent News" and have it only show posts from the 'recent-news" category.
or have I climbed down a rabbit hole...
you can do it like this: edit your index.php page or create a custom template and then create a page (say "Front Page" ) and assign this template to it. In this template, copy your index.php code or if you feel confident simply create your template and include this code:
<?php if (have_posts()) : ?>
<?php $my_query=new WP_Query( 'cat=-3'); while
($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>
<div class="post">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile;?>
<?php endif;?>
About your second question, simply go to Appearance --> Menu and there you'll see you can add pages but also categories. Simply select the category "Recent News" as a menu element and voilá, now you'll have a page displaying only posts from that category
I do not recommand messing with index.php
I think you should create a page template that will query posts from "Recent News" category.
Then in wordpress create a page and choose the page template you created.
In order to set it as the home page you will have to set it via wordpress configurations.
Wordpress page template
query posts by category
Set static homepage
I am looking for a way to present search results in my custom wordpress theme.
I was hoping being able to present the results like this:
Displaying 4 search results for "test"
Pages
testpage 1
testpage 2
Posts
testpost 1
testpost 2
I wrote a function is_type_page that I can use inside the loop (2 loops), but this breaks the pagination functionality.
Any suggestions how to achieve this?
I would run 2 separate loops on the page, after the first loop for pages run rewind_posts() and then run the loop again. Also the key to pagination is making sure the global $paged variable is being picked up on by both loops. $paged is how wordpress separates posts into pages. i.e. if you go to page 2 of something then the global $paged = 2.
Hope that helps
Multiple loops using rewind_posts here
Running two loops is the way to go if you want them displayed with separate headers. Here is code to get them to show intermingles as they come up by date created...
<?php while (have_posts()) : the_post(); ?>
<?php if ( $post->post_type == 'page' ) { ?>
**DISPLAY PAGE**
<?php } else { ?>
**DISPLAY POST**
<?php else : endif; ?>