Is there away to Create a custom php template for a page this is not the front page
For Example:
When you have front-page.php for the homepage i want to do some thing like that.
I don't Know if this is possible but if it is Thank You for your answers
This code you can use to create a custom template:
<?php
/**
* Template Name: Alphabetical Posts
*/
get_header(); ?>
<div id="main-content" class="main-content">
<?php
if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
// Include the featured content template.
get_template_part( 'featured-content' );
}
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
$custom_posts = new WP_Query( array(
'order_by' => 'title',
'order' => 'asc'
));
if ( $custom_posts->have_posts() ) :
while ( $custom_posts->have_posts() ) : $custom_posts- >the_post();
get_template_part( 'content', get_post_format() );
endwhile;
twentyfourteen_paging_nav();
else :
get_template_part( 'content', 'none' );
endif;
?>
</div><!-- #content -->
</div><!-- #primary -->
</div><!-- #main-content -->
<?php
get_sidebar();
get_footer();
the snipet is from https://premium.wpmudev.org/blog/creating-custom-page-templates-in-wordpress/
Related
I am making Wordpress theme from scratch. My page.php not showing/loading Lorem page content
//page.php
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php if ( comments_open() || get_comments_number() ) : ?>
<?php comments_template(); ?>
<?php endif; ?>
<?php endwhile; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_sidebar( 'tertiary' ); ?>
<?php get_footer(); ?>
I have
//header.php
<!DOCTYPE html>
<html>
<head>
<?php wp_head();?>
</head>
<body>
<h1>Test Header</h1>
</html>
Another
//functions.php
<?php
function remove_posts_menu() {
remove_menu_page('edit.php');
}
add_action('admin_menu', 'remove_posts_menu');
function add_taxonomies_to_pages() {
// register_taxonomy_for_object_type( 'post_tag', 'page' );
register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'add_taxonomies_to_pages' );
if ( ! is_admin() ) {
add_action( 'pre_get_posts', 'category_and_tag_archives' );
}
function category_and_tag_archives( $wp_query ) {
$my_post_array = array('post','page');
if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
$wp_query->set( 'post_type', $my_post_array );
if ( $wp_query->get( 'tag' ) )
$wp_query->set( 'post_type', $my_post_array );
}
function remove_page_attribute_support() {
remove_post_type_support('page','page-attributes');
}
add_action( 'init', 'remove_page_attribute_support' );
In functions.php I have disabled post in admin menu, and enabled taxonomy for pages.
Please rectify where I am doing wrong.
Actually copied code from another theme -
the main reason - page not rendered is page.php is calling other theme template files to show content.
But scratch theme has no content folder and no additional template for rendering content in WebPage content-xx.php
//page.php
<?php
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content();?>
<?php endwhile; ?>
</main><!-- #main -->
</div><!-- #primary -->
<!-- <?php get_sidebar(); ?> -->
<?php get_footer(); ?>
Fianlly rendered the lorem-imsum page
Please try with below:
HEAD:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php wp_head(); ?>
</head>
<body>
Function:
Use your default theme function twentytwelve or newer
page.php
<div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
<?php get_footer(); ?>
Pagination is not showing in my custom homepage template. Here is my code for the page:
<?php
/**
* Template Name: Homepage Template
* The main template file.
*
*/
get_header(); ?>
<div id="primary" class="content-area container">
<div id="main" class="site-main col-lg-9 col-md-9 col-sm-12 col-xs-12" role="main">
<?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; ?>
<?php
$args = array(
'post_type' => 'article',
'paged' => $paged
);
$articles = new WP_Query($args);
if ( $articles->have_posts() ) :
/* Start the Loop */
while ( $articles->have_posts() ) : $articles->the_post();
get_template_part( 'template-parts/content-article', get_post_format() );
endwhile;
the_posts_navigation();
else :
get_template_part( 'template-parts/content-article', 'none' );
endif; ?>
</div><!-- #main -->
<?php get_sidebar(); ?>
</div><!-- #primary -->
<?php get_footer(); ?>
Does anybody know how to get pagination working?
I am using i-Max theme.
I have a method call "imax_paging_nav" in the function.php file.
if ( ! function_exists( 'imax_paging_nav' ) ) :
function imax_paging_nav() {
global $wp_query;
// Don't print empty markup if there's only one page.
if ( $wp_query->max_num_pages < 2 )
return;
?>
<?php
$big = 999999999; // need an unlikely integer
$args = array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'type' => 'list',
'prev_text' => '<span class="text">« ' . __( 'Previous', 'i-max' ) . '</span>',
'next_text' => '<span class="text">' . __( 'Next', 'i-max' ) . ' »</span>',
'add_args' => false
);
?>
<nav class="navigation paging-navigation" role="navigation">
<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'i-max' ); ?></h1>
<div class="nav-links">
<div id="posts-nav" class="navigation">
<?php echo paginate_links( $args ); ?>
</div><!-- #posts-nav -->
</div><!-- .nav-links -->
</nav><!-- .navigation -->
<?php
}
endif;
I saw this method called on index.php
<?php get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<div class="blog-columns" id="blog-cols">
<?php /* The loop normal posts */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
</div>
<?php imax_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!-- #content -->
<?php get_sidebar(); ?>
</div><!-- #primary -->
then I have the page pagination like this
BUT when I create a new page template "page-news.php" and the pagination method doesn't work.
<?php
/**
* Template Name: News
*
*/
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php $allPosts = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish')); ?>
<?php if ( $allPosts->have_posts() ) : ?>
<?php /* The loop normal posts */ ?>
<?php while ( $allPosts->have_posts() ) : $allPosts->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php imax_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!-- #content -->
<?php get_sidebar(); ?>
</div><!-- #primary -->
Can anybody help me? Thanks in advance!
I found the solution here: https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops
I changed my custom page template as bellow then it works correctly.
<?php
/**
* Template Name: News
*
*/
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
$customArgs = array('post_type'=>'post', 'post_status'=>'publish');
$customArgs['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$allPosts = new WP_Query($customArgs);
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $allPosts;
?>
<?php if ( $allPosts->have_posts() ) : ?>
<?php /* The loop normal posts */ ?>
<?php while ( $allPosts->have_posts() ) : $allPosts->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php imax_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif;
// Reset postdata
wp_reset_postdata();
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;?>
</div><!-- #content -->
<?php get_sidebar(); ?>
</div><!-- #primary -->
Hi i'm trying to create a custom page (custom-page.php) that contains some of my blog posts, but when I test the loop i'm not seeing any posts. Its only returning the name of my custom page template as the h2.
I have two posts already published but they are not showing up.
I have a front-page.php which users land on when they first come to my site and I have not changed any settings under the reading tab.
I've read over the Wordpress codex and can't find any solutions so far.
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1>BLOG INDEX PAGE
BLOG INDEX PAGE</h1>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
?>
Please follow this code.
$newsQuery = newWP_Query('post_type=post','post_status=publish');
if ( $newsQuery->have_posts() ) {
while ($newsQuery->have_posts()) {
$newsQuery->the_post();
echo get_the_title();
echo get_the_excerpt();
}
}
and Your complete template will be like this.
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1>BLOG INDEX PAGE
BLOG INDEX PAGE</h1>
<?php
$newsQuery = new WP_Query('post_type=post','post_status=publish');
if ( $newsQuery->have_posts() ) : while ( $newsQuery->have_posts() ) : $newsQuery->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
?>
Create a page named "Blog" from wp admin and then create a file in theme folder named page-blog.php and write the following code below.
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1>BLOG INDEX PAGE</h1>
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'id',
'order' => 'desc'
);
$loop = new WP_Query($args);
if($loop->have_posts()) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
?>
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'offset' => 0
);
$the_query1 = new WP_Query( $args );
if (count($the_query1->posts)>0) {
while ( $the_query1->have_posts() ) : $the_query1->the_post();
get_template_part( 'loop-archive-template-location' );
endwhile;
}
?>
As we mentioned, WP_Query is a PHP class used by the WordPress database. This particular class can do several things, but primarily it’s used to pull posts from the database.
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
// wp_reset_postdata();
here are two main scenarios you might want to use WP_Query in. The first is to find out what type of request WordPress is currently dealing with. The $is_* properties are designed to hold this information: use the Conditional Tags to interact here. This is the more common scenario to plugin writers (the second normally applies to theme writers).
The second is during The Loop. WP_Query provides numerous functions for common tasks within The Loop. To begin with, have_posts(), which calls $wp_query->have_posts(), is called to see if there are any posts to show.
i am trying to personalize a new theme made in underscore.me, but i can't seem to create a new template page with a loop that can show me posts of one specific category, how can i do this?
I'm gonna paste here the index.php of the underscore.me theme, witch has a generic loop, sadly, copying and pasting this loop on a template page does
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : ?>
<?php if ( is_home() && ! is_front_page() ) : ?>
<header>
<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
</header>
<?php endif; ?>
<?php /* Start the Loop */ ?>
<?php 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.
*/
get_template_part( 'template-parts/content', get_post_format() );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
And this is the page.php.
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content', 'page' ); ?>
<?php
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
?>
<?php endwhile; // End of the loop. ?>
</main><!-- #main -->
</div><!-- #primary -->
Good news for you, you dont need a specific loop for a specific category.
if you have a category.php file in your theme, and assume that your category name is "Lux Cars" with id of "35". if you copy your category.php as category-35.php or category-lux-cars.php (slug of your category). When you open the posts with this category. wordpress call this file and show posts in that loop. you can edit your category-35.php and add any category detail or sth.
But if you still want a loop for a specific category then you can use that code;
<?php
$args = array ( 'category' => ID, 'posts_per_page' => 5);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
?>
//Style Posts here
<?php endforeach; ?>