I got lots of articles I show on my category page.
Here is the code.
<?php if(is_category(4)) {
while ( have_posts() ) : the_post(); ?>
<div class="work">
<div class="work-thumb">
<a href="<?php echo get_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<div class="work-title">
<?php the_title(); ?>
</div>
</div>
<?php endwhile; // end of the loop.
} ?>
It is just loop on all articles, but it doesn't show all of them, just like 50%
What could be the problem>?
Is the page that's using that template/code the page that you have set to be the Posts Page, in admin settings?
If so, then the posts per page setting could be less than the total number of posts (and you would need pagination, or to increase this number).
If it's a custom query with the code in your question, then you need to add this to the query arguments:
'posts_per_page' => -1
Note: Even if your case is the former, you could alter the query by using the pre_get_posts filter. E.g. put this in your theme's functions.php:
add_action('pre_get_posts', 'my_filter');
function my_filter( $query ){
$query->set('posts_per_page', -1);
return $query;
}
Inside that function, you want to wrap the code inside an if statement, to do it specifically for the, for example, post type or taxonomy in question.
Related
So, I am looking for a way to get the latest post in order to display it in a different way than the rest of the posts. In the settings I have my "Blog" page to display the posts, as generally everybody do.
The first thing I tried (an answer from another question), was to use the normal loop, I mean, the if (have_posts())...while(have_posts())..etc. And above that IF, place another IF just to get the latest post and through that way I could style my last post. But as I have pagination, on each page, the latest post, is actually the latest post from that page and not the real latest post. Hope this was understandable.
My second try, was to exclude from the normal loop the latest post, and for that I used a snippet from an article which explains how to exclude the latest post and keeping the pagination working using pre_get_posts and found_posts so my code was as follow:
add_action('pre_get_posts', 'myprefix_query_offset', 1 );
function myprefix_query_offset(&$query) {
//Before anything else, make sure this is the right query...
if ( ! $query->is_home() ) {
return;
}
//First, define your desired offset...
$offset = 1;
//Next, determine how many posts per page you want (we'll use WordPress's settings)
$ppp = get_option('posts_per_page');
//Next, detect and handle pagination...
if ( $query->is_paged ) {
//Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
//Apply adjust page offset
$query->set('offset', $page_offset );
}
else {
//This is the first page. Just use the offset...
$query->set('offset',$offset);
}
}
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
//Define our offset again...
$offset = 1;
//Ensure we're modifying the right query object...
if ( $query->is_home() ) {
//Reduce WordPress's found_posts count by the offset...
return $found_posts - $offset;
}
return $found_posts;
}
So far so good, this code is working and it's excluding the latest post and the pagination is working, but now my problem is, how do I get the latest post? I tried to use wp_query above the loop in my home.php to get that single latest post but realized that pre_get_posts overrides the wp_query?
How can I solve this and get the latest post? Do I have to do the opposite? I mean, first get the latest post and then create a custom loop for the rest of the posts, but how to manage the pagination?
This is my html so far in home.php
<div class="blog-list">
<div class="blog-item featured-item">
// display latest post here
</div>
<?php if ( have_posts() ): ?>
<div class="teaser-inner mb-4">
<div class="row">
<?php while ( have_posts() ): ?>
<?php the_post(); ?>
<div class="blog-item col-md-6 mb-3 mb-sm-5">
<div class="row">
<div class="col-6 col-md-4">
<?php $img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
<a href="<?php echo esc_url( get_permalink() ); ?>" class="blog-link">
<div class="blog-item-img bg-cover" style="background-image:url(<?php echo esc_url($img_url); ?>)"></div>
</a>
</div>
<div class="col-6 col-md-8">
<?php the_title(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<div class="blog-pagination">
<?php echo paginate_links(); ?>
</div>
<?php endif; ?>
</div>
Any clue is appreciated.
Your pagination system is working fine. What you could do, it retrieve the latest post with wp_get_recent_posts:
$latestPosts = wp_get_recent_posts(['numberposts' => 1]);
$latestPost = $latestPosts ? current($latestPosts) : null;
Then in your div, you can display the information of your latest post with $latestPost.
<?php if ($latestPost): ?>
<div class="blog-item featured-item">
// display latest post here
</div>
<?php endif; ?>
For the above to work, you need to alter the pre_get_posts a little bit to make sure to only alter the query if it's the main query:
if (! $query->is_home() || ! $query->is_main_query()) {
return;
}
Im trying to display data via a loop using ACF[advance custom fields, Option Page] in my Footer.
This works on my homepage without any problems. But if i navigate to sub-pages it breaks , meaning it would show maybe only the page_links row or on another page only the images_footer row.
It confuses me because home page works perfectly normal and shows all footer data from below.
Also i looked if there is any conflict in the custom-fields names towards the sub-pages custom-fields names and the footer but i dont find any.
Another thing if i would remove the first loop the second one works this is only on some pages, so weird.
If i keep the first while loop ,delete the second while loop, and navigate to other pages,the first while loop show on some pages and some not, how is this even possible.
*I have done some var_dumps(); after both if statements, but obviously the dump does not. So it doesnt even pick up the row -> ('page_links', 'option') or in some cases ('images_footer', 'option').
Any helpful tips would be much appreciated.
<div class="col-lg-8">
<div class="links">
<?php
if( have_rows('page_links', 'option') ):
while( have_rows('page_links', 'option') ): the_row();
?>
<p><?php the_sub_field('footer_link_name'); ?></p>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="foot-images">
<?php
if( have_rows('images_footer', 'option') ):
while( have_rows('images_footer', 'option') ): the_row();
?>
<div><img class="img-fluid" src="<?php the_sub_field('logo_image'); ?>" alt=""></div>
<?php endwhile;endif; ?>
</div>
</div>
Following this documentation: ACF - Get values from an options page
I realized my website (www.inunfuturoaprile.it) with a minimal Wordpress theme that does not include the category.php template. I have created the template by me: the goal was to get pages that display a list of the posts belonging to a specific category. Unfortunately, due to my scarce knowledge of php, the code does not seem to work properly: I only see one post per category (e.g. https://www.inunfuturoaprile.it/politica/).
Here's the category.php file code:
<?php
/**
* Template di Categoria
*/
get_header(); ?>
<div id="content">
<?php
// Check if there are any posts to display
if ( have_posts() ) : ?>
<?php
// Since this template will only be used for Design category
// we can add category title and description manually.
// or even add images or change the layout
?>
<h1><strong><?php single_cat_title(); ?></strong>: Elenco Articoli</h1>
<div class="entry">
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?> // <small><?php the_time('j F Y') ?></small>
<?php endwhile; // End Loop
else: ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
Someone can help me? Thank you :)
I think your posts_per_page parameter is set to 1.
You can see other posts using pagination:
https://www.inunfuturoaprile.it/politica/page/2/
https://www.inunfuturoaprile.it/politica/page/3/
Check it on Settings->Reading page ( /wp-admin/options-reading.php), parameter "Blog pages show at most"
But it also can be set up somewhere in the theme code.
Changing it may affect your main page - since the main page shows only one post too.
So you can change it only on category.php page. Add this after get_header():
get_header();
global $wp_query;
$wp_query->set('posts_per_page', 10);
// or this - if you need no pagination at all:
// $wp_query->set('nopaging', true);
$wp_query->query($wp_query->query_vars); ?>
Hi I am trying to making my static wordpress page seen here
Link to the posts that are shown. However they are just linking to the file directory in the browser and not the post.
It was working before where it would just link to the post in a wordpress theme but now that doesn't even work.
I haven't touch the code in a week I left it in the state as mentioned above, where it would link but just to the wordpress theme.
Here is the code I use to get the information to the static page from wordpress -
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'post',
'paged' => $paged
);
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<article>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content(); ?>
</div>
<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
<hr>
</article>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
<?php else: ?>
<article>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</article>
<?php endif; ?>
Basically what I am trying to do is get it so when I click on the post it takes me to that post. Not to the file directory.
Any help is appreciated. Thank you.
I'm using xubuntu 16.04_LTS incase that has anything to do with compatibility in wordpress.
I have permalinks setup as shown
If all you want is the links to work . . .
http://auroraservers.org/MainSite/?p=38 ---fails
http://auroraservers.org/MainSite/wordpress/?p=38 ---works
That works and could be adjusted in the permalinks custom area. If you want that URL to be different, you're dealing with htaccess changes.
While not beautiful, you could replace the_permalink(); with
$id = get_the_ID();
$url = 'http://auroraservers.org/MainSite/wordpress/?p='.$id;
try to rename Index.php as index.php. It seems like Wordpress can't find index.php as a template for the post and therefore the system redirects you to the file directory since it can't find any of the possible template files.
Addition: Since your "Index.php" seems not to include code for the display of single posts, you might want to set up a file "single.php" as a template for single posts. But nevertheless, Wordpress will try to use index.php in many situations - always when the actual template file for a particular post type can't be found, like archive.php, page.php and many others...
So it might be a good idea to create a "regular", all-purpose index.php, rename your static page to something else, for example to front-page.php and define t as your starting page.
I seem to have fixed the issue I have been having. Thanks for all the replies.
Incase someone needs to know what I have done.
I went into mysql and dropped the database and recreated it. Must of gotten corrupted somehow.
I'm trying to show on my wordpress theme a specific post on a page. In this case, I'm trying to show the post on the home page and I've tried a lot to at least show the title, but all that I get is the title of the page, not of the post itself.
I've tried the_title and get_the_title() but the magic didn't happen.
Here comes the code relevant to my problem.
home.php
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) : the_post();?>
<?php
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
// if ( has_post_format( 'video' )) {
// echo 'this is the video format';
// }
get_template_part( 'template-parts/content-chat', get_post_format('chat') );
endwhile;
else :
// get_template_part( 'template-parts/content', 'none' );
endif; ?>
And it calls the file (it is calling the right file, double checked)
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
<h2><?php the_title(); ?></h2>
<?php the_content();
echo get_post_format();
?>
</article><!-- #post-## -->
In summary, the questions are:
1) Why is it showing the title of the page?
2) How can I show properly this kind of post format in my page?
For your first question wp grabs the title of the home page based on query_posts() this is what makes wordpress so dynamic, you can actually check a page or category id and then select with query posts what to show on it. Since you do not have a query_posts in your code wp defaults to the current post id which is your home page.
For your second, I had a code snippet, but it is at home, you can look it up here
https://developer.wordpress.org/reference/functions/query_posts/
Ok, so here is the snippet. Sorry for the delay, I have been on vacation.
First you have to determine the ID of your home page, or you can use the is home page function. If you go the former route you simply go to the back end to your home page and hover over it with your mouse. the home page should show up in a link tooltip at the bottom of your page. It will be something like post=5 the number is what you need.
Next I would create a category for the post you want to display on the front page and get that ID from the category page in the back end. Again it will be something like tag_id=12
<?php
$currpage = $post -> ID;
if($currpage == 5) {
query_posts('showposts=1&cat=12');
}
while (have_posts()): the_post();
You can have multiple posts on one page this way, infact you can have your whole loop on that page then under it dynamically populate other posts under it like in this example:
http://testex-ndt.com/products/ect-products/ The top part is the page that is working a loop and the bottom (testimonials) are posts that are assigned to a specific category, the showposts here are set to 3 and will display excerpts of the latest three posts. This is one website I did in the past.
One other thing you may want to keep in mind is page templates, if you want one page to work one way and you want another type of page to work another you will have to look into templating.
here is the entire code for that page.
<?php
/*
Template Name:Main Product
*/
?>
<?php get_header(); ?><!-- BANNER is part of main homepage only -->
<?php if (have_posts()) : ?>
<?php while (have_posts()): the_post(); ?>
<?php $postName = get_the_title($post); ?>
<!-- title link dynamic -->
<?php edit_post_link('Edit this item','',' | '); ?>
<?php the_content('Continue Reading'); ?>
<?php posts_nav_link(); ?>
<?php endwhile; ?>
<?php else : ?>
<div class="w3-col text">
<h2>Not Found</h2>
<p><?php _e("Sorry, no posts or pages could be found. Why not search for what you were trying to find?"); ?></p>
<?php get_search_form(); ?>
</div>
<?php endif; ?>
<!-- insert testimonial here -->
<section class="w3-row cl testimonials">
<!-- make code for query based on the page, then get the top three testimonials in excerpt form This is commented out because I did it with a function I attached to the header I will show below
$currpage = $post -> ID;
if($currpage == xx){
$cat = x;
}
-->
<h2>Testimonials for <?php echo $postName; ?></h2>
<?php
$currpage = $post -> ID;
$cat = testimonial($currpage); //here is the function
query_posts('showposts=3&cat=' .$cat ); ?>
<?php while (have_posts()): the_post(); ?>
<div class="w3-col excerpt" style="width:30%;">
<?php the_excerpt(''); ?>
</div>
<?php endwhile;
wp_reset_query(); // DO THIS EVERYTIME YOU ALTER QUERY_POSTS
?>
</section>
<!-- /content float -->
</div>
<!-- /content -->
</div>
So the Function was set up like as a seperate php file so I could alter it modularly. I called the file category_help.php
<?php
/*This particular block of code is only for determining what category of testimonial is allowed in a post. */
function testimonial($myPageID){
//services (all cats)
$id = "-15,-14,-13";
//Products
//lfet
if($myPageID == 18) $id = 2;
//bfet
if($myPageID == 22) $id = 4;
//rfet
if($myPageID == 20) $id = 3;
//ect
if($myPageID == 24) $id = 5;
//ultrasound
if($myPageID == 26) $id = 8;
return $id;
}
?>
Then I called this in the top of header.php
<?php require 'category_help.php'; ?>