WordPress Custom Theme nth Post - php

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

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

Display Gutenberg content in page template PHP file

Firts time I trying to create page using WordPress with new editor called Gutenberg. In Wordpress I Always using:
<?php the_content(); ?>
to display content but it is not working with Gutenberg. Is there some new way to display content when I using Gutenberg in PHP page templates files?
You have to call the_content(); inside the WordPress while loop like below.
if ( have_posts() ) {
while ( have_posts() ) : the_post();
the_content();
endwhile;
}

How to display the content of a blog post using a custom post template?

I am using my own custom WordPress theme and I am running into trouble displaying the content of blog posts. I can display the title and the date it was published using php but I can't get any of the paragraphs, images, headings, etc. to display on the page. I am using Gutenberg blocks (default) for the content of the blog posts.
I have tried using php functions to grab the content but they don't seem to be working.
<div class="col-md-6 col-md-offset-3">
<p class="date"><span class="glyphicon glyphicon-time">
</span> <?php echo get_the_date();?></p><br />
<p><?php $content = apply_filters('the_content', $post-
>post_content);?></p>
</div>
I am expecting the content of the post to display within the div container but the function is not grabbing the content. Any help would be appreciated!
It sounds like you may be trying to retrieve the post content from outside the loop.
If you look at the post template for a theme e.g. 2017, it is this bit that does the magic. It’s not even necessary to pass a post ID:
<?php
while ( have_posts() ) : the_post();
get_template_part( 'components/page/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
?>
E.g. you should just be able to do:
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile; // End of the loop.
?>
Might be a good idea to start with the code on the link above, or copy the single.php file for the theme you’re using and use that as the basis for your custom post page?

PHP slider based on current posts from active category in Wordpress

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.

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

Categories