How to add posts with navigation in home.php? - php

I have a theme I'm building and I'm trying to work on the homepage from home.php. I already have my listing of latest articles generating, but I want the page navigation to show up at the bottom for the next and/or previous set of articles. Ideally, some sort of lazy load too, but at the moment, I just want to understand how to do the page navigation part.
I'm having trouble finding articles on how to do this without "add THIS plugin" or "use THIS theme".
Here's what I already have to generate my articles section:
<section id="articles" class="container">
<?php
$grid_posts = get_posts( array(
'posts_per_page' => 12
) );
if ( $grid_posts ) {
foreach ( $grid_posts as $post ) :
setup_postdata( $post ); ?>
<?php
if ( has_post_thumbnail() ) : get_template_part('template-parts/posts-grid', 'image');
else : get_template_part('template-parts/posts-grid', 'default');
endif;
?>
<?php
endforeach;
wp_reset_postdata();
}
?>
</section>
I just want to find a resource that shows me how to either "load more articles" on that page or show the "next/previous page" navigation.
Thanks

You're looking for wp_link_pages
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'test' ),
'after' => '</div>',
) );
For those kind of questions and to get a basic understanding, it's also always worth at least looking into a "template theme" like _s
In your case, the file to look at would be this one

Related

Woocommerce review section outside of tab (on single-product page) wont' show comments?

I am working on a custom theme, so what I have done so far is:
I removed the review tab in the Woocommerce single product page because I replaced it with 5 custom tabs and I want to write my custom section just below ('woocommerce_after_single_product_summary').
I copied the 'single-product-reviews.php' template for editing and called it in my theme's functions file. That all seems to work fine.
This is how it looks.
As you can see everything reads fine except for the comments themselves. There are reviews/comments posted, I tested it multiple times manually and from Dashboard, I just can't get them to show up. The star rating shows up on the products themselves, even on the shop page after you write the review, so it obviously works. It might be a simple mistake, I might just be too tired to notice it. I will post the code, if anyone has any solutions, thanks in advance.
Btw the reviews and comments are enabled everywhere in the Dashboard, I checked it many times. This is the div/code that is causing the problem, I have not edited the file at all besides changing the title.
<?php if ( have_comments() ) : ?>
<ol class="commentlist">
<?php wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ) ); ?>
</ol>
<?php
if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
echo '<nav class="woocommerce-pagination">';
paginate_comments_links(
apply_filters(
'woocommerce_comment_pagination_args',
array(
'prev_text' => is_rtl() ? '→' : '←',
'next_text' => is_rtl() ? '←' : '→',
'type' => 'list',
)
)
);
echo '</nav>';
endif;
?>
<?php else : ?>
<p class="woocommerce-noreviews"><?php esc_html_e( 'There are no reviews yet.', 'woocommerce' ); ?></p>
<?php endif; ?>
</div>

wp_query pagination on archive.php

I'm using the following wp_query on a few pages in Wordpress, and as you can see I am trying to ensure the query is paginated. Everything is working successfully on a custom page template page-articles.php, however I'm unable to get the same results on the archive.php template.
The pagination link renders successfully (e.g mydomain.com/category/my-cat/page/2) however upon clicking the link does not work, it just throws a 404 error? How can these links not go anywhere?
I'm assuming there is some issue with using custom wp_query on the archive.php template?
Thanks!
Query
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC'
);
$articles = new WP_Query($args );
?>
Loop
<?php if ( $articles->have_posts() ) : ?>
<?php while ( $articles->have_posts() ) : $articles->the_post(); ?>
Posts here!
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Pagination
<nav>
<div class="prev"><?php echo get_previous_posts_link( 'Previous', $articles->max_num_pages ); ?></div>
<div class="next"><?php echo get_next_posts_link( 'Next', $articles->max_num_pages ); ?></div>
</nav>
After a bit of digging around, and with help from this article, the below solution solves the issue. Just place this in your functions.php file and thats it. The below implementation works for archives of custom post types, as well as categories.
/**
* Wordpress has a known bug with the posts_per_page value and overriding it using
* query_posts. The result is that although the number of allowed posts_per_page
* is abided by on the first page, subsequent pages give a 404 error and act as if
* there are no more custom post type posts to show and thus gives a 404 error.
*
* This fix is a nicer alternative to setting the blog pages show at most value in the
* WP Admin reading options screen to a low value like 1.
*
*/
function custom_posts_per_page( $query ) {
if ( $query->is_archive('cpt_name') || $query->is_category() ) {
set_query_var('posts_per_page', 1);
}
}
add_action( 'pre_get_posts', 'custom_posts_per_page' );
Yes, it is.
It is because main query of archive.php is kept unchanged while you are playing with WP_QUERY. Try use query_posts() in archive.php.
query_posts($args);
and then default loop (instead of $articles)
<?php if ( have_posts() ) : ?>
<?php while (have_posts() ) : the_post(); ?>
Posts here!
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

Archive parameter not looping

I am using the wp_get_archives function to display a list of posts on my sidebar. Currently this does work as intended.
I want to build upon this sidebar by displaying get_the_title() and get_the_excerpt(). these also work.
However they are only displaying the current title and excerpt and not the corresponding title and excerpt to the current post.
Example:
Post 1 displays post 1's title and excerpt.
Post 2 displays post 1's title and excerpt.
Here is my full sidebar:
<aside id= "homeSideBar">
<?php if ( have_posts() ) {
while ( have_posts() ) {
the_post(); ?>
<?php }}
wp_get_archives( array( 'type' => 'postbypost', 'limit' => 10, 'after' => "<img class='showExcerpt' src=\"" . get_template_directory_uri() ."/images/plus-circle.png\"><div class='postExcerpt'><h1 class='fitHeadliner'>" . get_the_title() . "</h1><p>" . get_the_excerpt() . "</p><div class='hideExcerpt'>X</div></div>" ) ); ?>
...
</aside>
How can I make my posts display their corresponding titles and excerpts?
So as my sidebar gets more involved, I thought it best to refactor and create a sidebar.php template.
Within my sidebar.php template I use get_posts() within get_posts() you can use setup_postdata( $post ) to globalize post information like the_excerpt(), the_title(), etc.

Create a Custom WordPress Page with Post titles

I am trying to create a custom WordPress page that will contain only the links to all my post titles, divided on 4 column. I am also using Bootstrap with WordPress.
I created the php file, created a new page with her page atribute, but the post titles don't display.
This is the code i used:
<?php
/**
* The template used for displaying page content in questions.php
*
* #package fellasladies
*/
?>
<?php
<article id="post-<?php the_ID(); ?>" <?php post_class('col-md-4 col-sm-4 pbox'); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'fellasladies' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<?php edit_post_link( __( 'Edit', 'fellasladies' ), '<footer class="entry-meta"><span class="edit-link">', '</span></footer>' ); ?>
</article><!-- #post-## -->
I really appreciate your help! Thanks
You'll need to start by creating a Query which fills an array with the posts you want to iterate. Read about the get_posts() function in WordPress.
Here's an example. Note that we can't use functions which are meant to be used "in the loop" such as the_title() or the_content(). We must specify the post_id for each iteration. We should not modify the main query for situations such as this.
// the arguments for the get_posts() function
$args = array(
'post_type' => 'post', // get posts int he "post" post_type
'posts_per_page' => -1 // this means the array will be filled with all posts
);
$my_posts = get_posts($args);
// now we'll iterate the posts
foreach ( $my_posts as $p ) {
// a title
echo get_the_title($p->ID);
// the link
echo get_permalink($p->ID);
// a custom field value
echo get_post_meta($p->ID,'custom_field_key',true);
}
Theming what happens inside each iteration is up to you.
Good luck! :)
I encourage you to go read about Page Templates on Wordpress Codex, that could help you a lot!
Pages are one of WordPress's built-in Post Types. You'll probably want most of your website Pages to look about the same. Sometimes, though, you may need a specific Page, or a group of Pages, to display or behave differently. This is easily accomplished with Page Templates.
It seems that you have a <?php useless. You also don't define your template's name, which is required.

WordPress Theme from scratch, widgets don't work

I know this topic has been discussed quite a bit but I've tried everything I can find online and nothing is working. I need a single widget area in my custom template. I wrote if from scratch and didn't use a barebones type starter therefore I didn't have a functions.php to start from.
In wp-admin it shows my widget as it should but when there is a widget in that area and the page is reloaded it resets. in other words the widget doesn't persist in the area it's supposed to. am I missing something? is my wordpress install bad?
here is my functions.php in it's entirety
<?php
register_sidebar
(
array(
'name' => 'Header Widget',
'id' => 'headerBanner',
'before_widget' => '<div id="banner">',
'after_widget' => '</div>',
'before_title' => '',
'after_title' => '',
)
)
?>
and my index.php
<?php if ( !dynamic_sidebar (1)) : ?>
<h1>it didn't work</h1>
<?php endif; ?>
try:
<?php if(!dynamic_sidebar ('headerBanner')) : ?>
<h1>it doesn't work</h1>
<?php endif; ?>
You need to pass the id of the sidebar you want to display when calling dynamic_sidebar
Also note that dynamic_sidebar will return false if there are no widgets added to the sidebar so add a widget to it and see if it works
Have you tried this other approach?
<?php dynamic_sidebar( 'headerBanner' ); ?>

Categories