0

I have tried many different codes:

 <a href="<?php echo get_template_part( 'template/test/post' ); ?>"> Test 1</a> 
<a href="<?php echo get_template_directory_uri(); ?>/template/posts.php"> Test 2 </a> 
<a href="<?php bloginfo('template_url'); ?>/template/posts.php"> Test 3</a> 
<a href="<?php echo get_page_link( '/template/posts.php' ); ?>">Test 4</a> 

I am getting URLs like this

enter image description here

I am using the code (Test 1) :

<a href="<?php echo get_template_part( 'template/test/post' ); ?>"

and Coming to this page after clicking:

enter image description here

I want to create a separate template page for all the posts. enter image description here

But when I add header custom page showing errors. enter image description here

how do I include the header (<?php get_header(); ?>) footer and posts loop? That's my code on posts.php

<?php get_header(); ?>
<?php 
    $blog_args = (get_query_var('paged')) ? get_query_var('paged') : 1; 
    $blog_args = array (
    'post_type' => 'post',
    'paged' => $blog_args
    );
    $blog_posts = new WP_Query($blog_args);
    while($blog_posts->have_posts()) { 
    $blog_posts->the_post();
?>

<a href="<?php the_permalink( ) ?>"> <?php echo wp_trim_words( get_the_title(), 5 ); ?></a>
<?php echo substr( get_the_excerpt(), 0, 94,)."..."; ?>


<?php } ?>
<?php wp_reset_postdata() ?>

Note: "/**

  • Template Name: Posts Page Template */ " : I don't want that type of selecting option.
1

1 Answer 1

0

For what I understood, you want to create a template that shows header, footer and blog posts in the body of the page. For that, you will have to create a custom template. Custom templates can be used in several ways, but I'm going to use an example that will give you the option to use that tamplate in any page (wordpress page, not posts or custom post types for this particular example, you can also do that but I'm not going that deep).

The first thing you have to do, is create a file in the root folder of your theme ( Child theme is you are not writing code in a parent theme you built). You can name that file arbitrarily (for this particular example), but I normally use something like `tpl-blog-posts.php' or something similar.

Then, once you have your file, you will have to write this code inside it:

<?php
/**
* Template Name: Posts Page Template
*/

get_header(); 
?>
<div id="primary" class="content-area">
   <main id="main" class="site-main" role="main">
       <?php
            $query = new WP_Query(['posts_per_page' => 12]);
            if($query->have_posts()) {
               while($query->have_posts()) {
                   $query->the_post();
                   // Your posts HTML here
               }
            }
   </main>
</div>
                               
<?php get_footer(); ?>

After you finish this, you will have an option in all pages to choose this template, so, go to the WP Dashboard, create a new page, go inside the new page's editor and choose 'Posts Page Template' from the options dropdown you have in the editor's right sidebar.

Note: the code above is just an example of what you can do, there is many ways to build a template, but I think this will give you an idea of how things work in WordPress.

If you want the new page you created to be the "official" blog page, go to the dashboard and click in the option Settings>Read in the left sidebar, and then choose your new page as the blog page in the dropdown WordPress gives you.

As @Jacob Peattie mentioned in a comment, you need to read about template hierarchy and custom page templates.

1
  • thanks but I didn't want an extra selecting option. Sep 5, 2021 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.