I have created a custom post type called news
Here is the code
function create_post_type_news() {
$supports = array(
'title', // post title
'editor', // post content
'author', // post author
'thumbnail', // featured images
'excerpt', // post excerpt
'custom-fields', // custom fields
'comments', // post comments
'revisions', // post revisions
'post-formats', // post formats
);
$labels = array(
'name' => _x('News', 'plural'),
'singular_name' => _x('News', 'singular'),
'menu_name' => _x('News', 'admin menu'),
'name_admin_bar' => _x('News', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New News'),
'new_item' => __('New News'),
'edit_item' => __('Edit News'),
'view_item' => __('View News'),
'all_items' => __('All News'),
'search_items' => __('Search News'),
'not_found' => __('No News found.'),
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'description' => 'Holds our News and specific data',
'public' => true,
'taxonomies' => array( 'category', 'post_tag' ),
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'query_var' => true,
'rewrite' => array('slug' => 'news'),
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 6,
'menu_icon' => 'dashicons-megaphone',
);
register_post_type('news', $args); // Register Post type
}
add_action('init', 'create_post_type_news');
I have shown this news in my index.php like this
<div class="row">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; // Get the current page number
$args = array(
'posts_per_page' => 4, // Number of posts per page
'post_type' => 'news',
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged // Use the current page number
);
$news_query = new WP_Query( $args ); // Create a new query
if ( $news_query->have_posts() ) : ?>
<?php while ( $news_query->have_posts() ) : $news_query->the_post(); ?>
MY HTML CONTENT
<?php endwhile; ?>
<!-- Pagination -->
<div class="pagination">
<?php echo paginate_links( array( 'total' => $news_query->max_num_pages ) ); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
But the problem is in homepage. pagination ui is comeing perfectly. but when i click on page 2 it's showing page not found. This is my sample url of page 2 http://localhost/tr/page/2/
I haven't created anypage called home or index from dashboard/pages. I am using index.php as my home page http://localhost/tr
this is my home page.
This problem is making me crazy. Thank you.
pre_get_posts
filter to change its options, don't create a brand new query withWP_Query
/get_posts
/query_posts
. Also note thatindex.php
is not the homepage template, it's the fallback template for all pages. Usehome.php
orfrontpage.php
instead