Separate Post and Pages from WordPress Search Results - php

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; ?>

Related

WordPress: Styling taxonomy slug page

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();
}

Different html/css for first 4 posts in the same query in Wordpress

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

wordpress php page content restrictions

i have 2 member ids, 1 and 2, i wish to only allow id 1 to view certain content on a page, the page id is 63, so i have copied and duplicated page.php and renamed it page-63.php
then, i have wrapped php get_template_part with the code below, but it does not appear to work, have i put anything in the wrong section or have i missed a step? any help would be gratefully received
<?php if(is_user_logged_in() && tdp_hasMembershipLevel(1) : ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php else : ?>
this is displayed to logged out users or logged in users with any membership other than 1
<?php endif; ?>
Just copy the complete file page.php and make it page-63.php, without changing anything to the code apply your if(){}else{} conditions within the while loop. You will get the desired results. Or you can add the while loop from page.php to your current code. As per me your current code should be in loop mentioned in page.php.

Wordpress -- how to make a random post appear on home page?

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

showing fifth post in wordpress

in my wordpress website how can start showing posts from fifth post?
Because in top of my website there is a slide show that shows title of first five posts
and again under slide show shows first five posts and it is not beautiful.
You can use either get_posts or query_posts as a means to show the most recent posts excluding the first five. Please keep in mind query_posts directly alters the main loop by changing the variables of the global variable $wp_query.
This kind of seems like what you want though so an example would be to modify the index file of your theme to include:
query_posts('posts_per_page=5&offset=5');
Note: You can change the posts_per_page to what ever you desire. You also have many more options available to you.
For more detailed information please see the following documentation :
http://codex.wordpress.org/Template_Tags/get_posts
http://codex.wordpress.org/Function_Reference/query_posts
TLDR: Use the offset parameter in WP_Query.
If you are familiar with PHP this code will solve your problem.
Setup a logic to not show data for first 5 post as follows:
$i = 0;
while (have_posts()) {
++$i;
if ($i > 5) {
the_title();
// and more content
}
}
This will show data inside loop only if $i > 5.

Categories