- single.php
- singular.php
- archive.php
- pagination
-
-
Save anonaba/eb47fdc6ceb7ec97193a0ea758f495a4 to your computer and use it in GitHub Desktop.
In WordPress, archive.php is a template file used to display archive-type pages. These pages typically list posts grouped by a specific taxonomy, date, author, or other criteria. The archive.php file serves as the default template for these archive pages unless a more specific template is defined (e.g., category.php, tag.php).
The archive.php file determines the layout and design for the following types of archive pages:
- Category archives (e.g.,
example.com/category/news) - Tag archives (e.g.,
example.com/tag/tech) - Author archives (e.g.,
example.com/author/admin) - Date archives (e.g.,
example.com/2023/11/) - Custom taxonomy archives (e.g.,
example.com/genre/actionfor agenretaxonomy) - Post type archives (e.g.,
example.com/productsfor a custom post type archive)
WordPress uses a specific hierarchy to determine which template file to load for an archive page. If a more specific file does not exist, WordPress falls back to archive.php. For example:
-
Category Archive
category-slug.phpcategory-id.phpcategory.phparchive.phpindex.php
-
Tag Archive
tag-slug.phptag-id.phptag.phparchive.phpindex.php
-
Custom Post Type Archive
archive-{post_type}.php(e.g.,archive-products.php)archive.phpindex.php
- Use
archive.phpwhen you want to define a default design for all archive pages that don't have a more specific template. - It is a good fallback template if you don’t need unique layouts for individual archive types (categories, tags, etc.).
- For custom post type archives, ensure the
has_archiveparameter is set totruewhen registering the custom post type.
-
Create or Edit the File
- Create a file named
archive.phpin your theme directory if it doesn't exist. - Open the file for editing in a text/code editor.
- Create a file named
-
Basic Structure Here’s a simple example of
archive.php:<?php get_header(); ?> <main id="main-content"> <?php if (have_posts()) : ?> <header class="archive-header"> <h1 class="archive-title"> <?php // Display archive title the_archive_title(); ?> </h1> <?php the_archive_description('<div class="archive-description">', '</div>'); ?> </header> <?php // Loop through posts while (have_posts()) : the_post(); ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <div class="entry-content"> <?php the_excerpt(); ?> </div> </article> <?php endwhile; ?> <?php // Pagination the_posts_pagination(); ?> <?php else : ?> <p><?php _e('No posts found.', 'your-theme-text-domain'); ?></p> <?php endif; ?> </main> <?php get_sidebar(); ?> <?php get_footer(); ?>
-
Customize the Layout
- Add custom styles or markup to match your theme's design.
- Use conditional logic (
is_category(),is_tag(), etc.) to tailor the layout further.
-
Test the Template
- Visit an archive page on your site (e.g., a category or tag archive) to ensure the template is being applied correctly.
-
Use Functions for Flexibility: Use functions like
the_archive_title()andthe_archive_description()to dynamically display archive-specific content. -
Support Pagination: Include pagination using
the_posts_pagination()orpaginate_links(). -
Add Breadcrumbs: Enhance user navigation by including a breadcrumb trail if your theme supports it.
-
Fallback Logic: If you want to display a unique layout for certain archives but still fall back to
archive.phpfor others, create specific templates and leavearchive.phpas the fallback.
By understanding the role of archive.php, you can create consistent and well-structured archive pages for your WordPress site.
Pagination in WordPress refers to the process of dividing content (such as blog posts, search results, or archives) into multiple pages rather than displaying all items on a single page. This improves user experience by:
- Reducing page load time.
- Enhancing navigation for large content sets.
- Making the content more digestible.
Examples include numbered pages, "Older Posts" and "Newer Posts" links, or a "Load More" button.
Use pagination when:
- Your website has a large number of blog posts, products, or other content types that would overwhelm users if displayed all at once.
- You want to improve SEO by creating separate URLs for content chunks (allowing better indexing).
- Your design requires a structured and clean layout for better user engagement.
WordPress automatically supports pagination for posts when set up correctly. To enable or modify:
- Go to Settings > Reading in the WordPress dashboard.
- Set the "Blog pages show at most" value to the desired number of posts per page.
WordPress will automatically paginate posts based on this setting.
If you're creating a custom theme or template, use functions like the_posts_pagination() or paginate_links().
<?php
if (have_posts()) :
while (have_posts()) :
the_post();
// Display post content
endwhile;
// Display pagination
the_posts_pagination();
// add array of options if is needed
// the_posts_pagination(array(
// 'mid_size' => 2,
// 'prev_text' => __('« Previous', 'textdomain'),
// 'next_text' => __('Next »', 'textdomain'),
// ));
else :
echo 'No posts found.';
endif;
?>or
if (have_posts()) :
while (have_posts()) :
the_post();
// Display post content
endwhile;
?>
<!-- Display pagination -->
<div class="wpdevs-pagination">
<div class="pages new">
<?php previous_posts_link('<< Newer Posts'); ?>
</div>
<div class="pages old">
<?php next_posts_link('Older Posts >>'); ?>
</div>
</div>
<?php
else :
echo 'No posts found.';
endif;
?>If you're using WP_Query for a custom query, pagination requires additional setup:
- Pass pagination parameters (
paged) in your query. - Display navigation using
paginate_links().
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'paged' => $paged,
);
$custom_query = new WP_Query($args);
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) :
$custom_query->the_post();
// Display post content
endwhile;
echo paginate_links(array(
'total' => $custom_query->max_num_pages,
'current' => $paged,
'prev_text' => __('« Previous', 'textdomain'),
'next_text' => __('Next »', 'textdomain'),
));
else :
echo 'No posts found.';
endif;
// Reset post data after custom query
wp_reset_postdata();For multipage content within a single post or page, use the <!--nextpage--> tag in the WordPress editor to split content. Then use wp_link_pages() to display navigation.
<?php
wp_link_pages(array(
'before' => '<div class="page-links">' . __('Pages:', 'textdomain'),
'after' => '</div>',
));
?>Plugins can enhance pagination functionality, adding features like infinite scroll, AJAX-based "Load More" buttons, or advanced designs. Popular plugins include:
- WP-PageNavi: Adds more control over pagination styling.
- Ajax Load More: Implements infinite scrolling.
- SEO-Friendly URLs: Ensure your pagination uses consistent, clean URLs (
/page/2/,/page/3/). - Avoid Duplicate Content: Use the
rel="prev"andrel="next"attributes in pagination links for SEO. - Responsive Design: Ensure pagination works well on mobile and small screens.
- Test Performance: Balance the number of items per page for usability and performance.
Using pagination effectively improves both user experience and website performance.
In WordPress, single.php is a template file in a theme that is used to display a single post of a custom post type or a standard post. It provides the layout and structure for viewing the details of a single post, including its title, content, metadata, and any other elements specific to the post.
- Purpose: Displays the content of individual posts.
- Hierarchy: WordPress uses the template hierarchy to determine which file to use for a single post. If
single-{post-type}.phpexists, WordPress uses it; otherwise, it falls back tosingle.php. - Custom Post Types: If you have custom post types (e.g.,
productorportfolio), you can create specific templates likesingle-product.phporsingle-portfolio.phpfor those posts.
<?php get_header(); ?>
<div id="primary">
<div id="main">
<div class="container">
<?php while (have_posts()): the_post() ?>
<article id="post-<?php the_ID() ?>" <?php post_class() ?>>
<header>
<?php the_title('<h1>', '</h1>') ?>
<div class="meta-info">
<p>
Posted in <?php echo get_the_date() ?>
by <?php the_author_posts_link() ?>
</p>
<p>Categories: <?php the_category() ?></p>
<p><?php the_tags() ?></p>
</div>
</header>
<div class="content">
<?php the_content() ?>
</div>
</article>
<?php
if (comments_open() || get_comments_number()) {
comments_template();
}
?>
<?php endwhile ?>
</div>
</div>
</div>
<?php get_footer(); ?>- Default Post: For a blog post,
single.phpmight display the title, content, author, and categories. - Custom Post Type (e.g., "Book"):
- Create a
single-book.phpfile for the "Book" custom post type. - Add fields like "Author," "Publisher," and "Publication Date" using WordPress meta fields.
- Create a
By using single.php and its variations, you can control the layout and content display for specific types of posts in WordPress.
When working with the singular.php template in WordPress, you are handling a template that can be used to display individual posts or pages. This file serves as a fallback template for both single post and page views if no other specific templates exist, such as single.php or page.php.
Here’s a basic outline of how you can work with singular.php:
The singular.php template is typically located within your theme's directory:
/wp-content/themes/your-theme/singular.php.
If it doesn't exist, you can create one.
Here's an example of how a simple singular.php file might look:
<?php get_header(); ?>
<main id="primary" class="site-main">
<?php
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', get_post_type() );
// If comments are open or there are at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
?>
</main><!-- #main -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>WordPress uses a hierarchy to determine which template to use. If there is no single.php or page.php, it will use singular.php to display single posts or pages.
To customize the singular.php template:
-
Custom Post Type Support: You can adjust the template to check for custom post types and load different parts based on the content type. This can be done using
get_post_type()as seen in the example above. -
Custom Layouts: Add specific layouts for different post types or use conditionals to include different templates or structures for pages versus posts.
-
Comments Section:
comments_template();is included to load the comments section if comments are enabled. -
Custom Fields/Metadata: If you need to display custom fields or metadata, you can use
get_post_meta()orthe_field()(if you are using Advanced Custom Fields plugin).
<?php if ( get_post_meta( get_the_ID(), 'your_custom_meta_key', true ) ) : ?>
<p><?php echo esc_html( get_post_meta( get_the_ID(), 'your_custom_meta_key', true ) ); ?></p>
<?php endif; ?>Ensure that the content displayed by singular.php is styled properly by including your custom CSS. You can target specific elements or classes in the theme’s stylesheet (style.css) or use inline styles within the template itself.
If you're making changes or adding new functionality, consider using hooks or filters so you can maintain compatibility with theme updates.
Let me know if you need any specific help regarding the singular.php template!
In WordPress, both singular.php and single.php are template files used to display individual content, but they serve slightly different purposes in the template hierarchy:
-
single.php:- This template is specifically for displaying individual posts of the 'post' post type (i.e., regular blog posts). It is used when a single post is viewed on the front end.
- If WordPress detects that you are viewing a single post (like a blog post), it will look for
single.phpin the theme directory. Ifsingle.phpdoesn't exist, WordPress will fall back to other templates in the hierarchy.
-
singular.php:- This is a more general template used to display any singular content, whether it's a post, page, or any other custom post type.
- WordPress will use
singular.phpas a fallback if a specific template likesingle.phporpage.phpdoesn't exist for that particular content type. - In essence,
singular.phpacts as a universal template for individual pieces of content that are not archive or list-based.
When WordPress tries to display a single post, it looks for templates in this order:
single-{post-type}.php(for custom post types)single.phpsingular.phpindex.php
When it tries to display a page:
page.phpsingular.phpindex.php
In summary:
- Use
single.phpto target individual blog posts (or posts of specific post types). - Use
singular.phpas a more general fallback for displaying any singular piece of content, including posts, pages, or custom post types.