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();
}
Related
I am just learning custom WordPress theme creation and I can't find answers to some questions.
-- I want to create a blog theme. There will be a carousel. How can I choose (for example) the first 4 posts for this carousel? And after them, the "latest posts" loop will start after 4th post.
I've found only the loop which is shown next;
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display post content
endwhile; endif;
?>
Hello fellow community,
I'm pulling my hair off for the past 3 days.
I tried many post / gallery / sliders plugins on Wordpress, and I cannot get the result I was looking for.
Basicaly what I would like to do is to generate a dynamic slider based on the current posts from the active category page. The posts are all listed under the same category. The slider must display ONLY the posts belonging to the current category.
I tried to customize the category.php page with Slider Revolution but it only retrieve "the most recent" post and not the rest.
I tried with many others plugins but they all display posts from other categories.
Thank you very much for your help.
So a bxslider could look something like this
<?php
if ( have_posts() ) : ?>
<div class="bxslider">
<?php while ( have_posts() ) : ?>
<?php the_post();?>
<div class="whateverClassHere">whatever content here <?php echo get_the_title(); ?></div>
<?php endwhile; ?>
</div>
<?php endif; ?>
And then you would initiate it with Jquery
$('.bxslider').bxslider(); //read the docs for settings here.
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 a search results page under construction, and it splits the posts out in to categories (products, recipes, articles), in 3 separate sections down the page. The categories (recipes and articles) were fine and easy - creating a query to use in 2 separate loops, but I'm having trouble with the custom post type.
I want to pull these in with the loop as well if possible, but not sure whether to do it on the post type or taxonomy. The products are obviously split in to sub categories under the taxonomy, so when I tried it, it was pulling the same product multiple times.
Right now, having given up on trying to pull them through using the taxonomy, I am trying this:
<?php
$args = array(
'post_type'=> 'products'
);
$products = new WP_Query($args);
if ( have_posts() ) :
?>
<div class="product suggestions cfx sub-range">
<h2>We found xx products...</h2>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
This code isn't pulling anything through...will a different query work, or should I still be trying to use the custom taxonomy?
When I search for a solution, pretty much everything I can find is creating a search.php page for displaying only custom post types, or just adding the custom post type to search results, not having a separate loop for custom post types along side the loops for categories...
Thanks
I think I got it. I changed the slug from products and it started working for some reason. So now i have this:
<?php
$args = array ('post_type'=>'product-ranges');
$products = new WP_Query($args);
if ( have_posts() ) :
?>
<div class="product suggestions cfx sub-range">
<h2>We found xx products...</h2>
<?php while ( $products->have_posts() ) : $products->the_post();
?>
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