Pulling all Wordpress pages into one page with page templates - wordpress

I would like to be able to pull all of the pages from my custom theme into one page template, but still allow all of the pages to be displayed according to the selected page template.
In other words, if I have a page template called 'Main' where I pull all of the page data into, and I create a page called 'Home', I would like this page to display on the 'Main' template according to the page template I have selected for the 'Home' page. Is this possible to do?
Thanks,
JW

Alright, so I am going to answer my own question here. I did some research on my own and modified some code from an tutorial to Ajaxify a theme. What needs to happen is this.
In your page.php file, or whatever template you want to use as your main page template, add this.
<?php query_posts( array('post_type'=>'page', 'posts_per_page' => 1000, 'orderby' => 'menu_order', 'order' => 'ASC') ); ?>
<?php if(have_posts()): while(have_posts()): the_post(); ?>
<?php
global $post;
$slug = $post->post_name;
locate_template(
array(
"template-$slug.php",
'template-main.php'
), true
);
?>
<?php endwhile; endif; ?>
Then add your page templates to the theme, and name according to the page slug i.e. template-about.php, or template-home.php. You can then have all of your content displayed dynamically in a one page site and be able to use the default Wordpress pages. I hope I was clear on this, and if not, feel free to let me know, and I'll do my best to clarify.

use in all your page templates this:
include 'page.php';
and then in page.php you can use this:
if (is_page_template('page_temp_one.php')) { ... }

Related

Add sticky post to a custom post type wordpress

My version of wordpress is 5.2.3
I have a custom post type and I want to add it a sticky post.
I tried many plugins and hack to figured out how to solved this problem (sticky custom post type isn't a native feature of wordpress) but I still don't know how to solved it.
Does anyone have a solution for me ?
-----EDIT------
I try this plugin https://wordpress.org/plugins/custom-post-type-sticky/. He put the same feature than the classic post of wordpress (a checkbox for put a sticky post) he works perfectly in back but nothing appear in front. (event with an additional code found here https://www.sktthemes.org/wordpress/add-sticky-posts-wordpress/) My code is a basic loop like :
<?php
$args_ressource = array(
'post_type' => 'ressource',
'order' => 'DESC',
'posts_per_page' => 4,
);
$ressource = new WP_Query( $args_ressource );
if ( $ressource->have_posts() ) { ?>
<ul>
<?php
while ( $ressource->have_posts() ) {
$ressource->the_post();
$imageArticle = get_field('hero_image');
?>
<li>
<a href="<?php the_permalink(''); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } ?>
</ul>
<?php }
wp_reset_postdata();
?>
To be precise I need to show my sticky custom post type on the left of my page, and on the right the rest of the custom post but without this sticky post.
With the basic article a simple loop works because this is a basic feature of wordpress, but not with custom post type
Many thanks
admin:
Create meta_value mimicking sticky function on/off
logic:
query "posts" filtering only checked posts
create second query where you exclude above "post" ids
template:
run a loop for sticky
run the common loop

How can I add some posts in the homepage and have a blog page as well?

I am building my own - custom - theme in WP.
In my index.php, I have many static contents ( Don't know how to convert them into dynamic fields in WP yet, but I will learn this later ).
The issue is that I want to display a number of posts in the homepage 2 posts and below them a link to redirect the user to a blog page which displays the whole posts.
I searched a lot and I found a solution which is not doing what I want, it was about setting a static front-page and set the blog page as the Posts page under settings then reading.
After doing so, the whole static content - that was in my index.php doesn't appear on the homepage anymore and the homepage becomes an empty page and the whole content went to the blog page, which made me CRAZY!!
All I want to do; is to keep my static content and 2 of my most recent posts in the homepage and create a blog page which contains the whole posts.
Hope to find a solution for this Without Plugins.
Add This Snippet Code into your front-page.php or wherever you want to show 2 posts recently published.And Also a Blog Page Link to navigate the user to all blog posts..
$the_query = new WP_Query( array( 'post_type' => 'post',
'posts_per_page' => 2,
'orderby' => 'publish_date',
'order' => 'DESC'
)
);
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_excerpt();
the_author();
// add whatever you want
endwhile;
wp_reset_postdata();
echo 'See All Blog Posts Here : <a href='.site_url("blog").'>All Blog Posts</a>';
else :
esc_html_e( 'Sorry, no posts yet published.' );
endif;
?>
This is going to be a detailed answer to my problem to help anyone who might face the same problem, it will cover everything in steps for a WordPress newbie, like me! :D
As mentioned in my Question, my problem is that I want to display 2 posts in my index.php and make a separate blog page contains the whole posts.
The Most Detailed Solution:
1) Go to your index.php and use the normal posts loop:
<?php
// The Loop
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
2) From WP Dashboard, go to Settings then Reading and change the Blog pages show at most value to 2 posts Or whatever number of posts you want.
Now, we've done the first step and show only a specific number of posts in our index.php, let's continue the steps to display the whole posts in our blog page.
3) Create a PHP Page called page-blog.php.
Notice: if you don't have a page.php you have to create it, for more information about how to create a custom PHP Page, I recommend #Adam's answer here.
4) Go to your WP Dashboard and create a new page called blog.
5) In your page-blog.php you need to add this snippet:
// Posts Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 6, 'paged' => $paged );
query_posts($args);
// The Loop
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} else {
echo "Sorry, There is no posts to be displayed";
} // end if
?>
Notice: We used the same loop as we did before in index.php, only the new 2 lines of codes which above the loop is the new different thing here in our page-blog.php page
6) I set the posts_per_page to 6 because it is my blog page and I want to display more posts there. Set its value to whatever number you want, it depends on you
7) BOOM, it works and you did what you wanted, just like me!!
This problem seemed VERY COMPLICATED for me, but when I searched and read more information about this, I found that it is a VERY EASY to solve problem, and I hope this answer help you if you faced it!

Displaying page content

I have two codes. The first one I am using on page.php and it displays content from any pages I create in the admin panel. The second code works to display my posts on the mainpage just not sure where that code should go.
It works if I place it in the page.php but then the same content (posts) are shown on any pages I create. I tried placing the second code in home.php and index.php at the same time as using the first code in page.php but does not work.
if (have_posts()): while (have_posts()): the_post();
wp_title(''); echo '<br />';
the_content(); echo '<br />';
endwhile; endif;
<--- Second Code -->
$args = array( 'posts_per_page' => 10 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
setup_postdata( $post ); ?>
<div id="pbox">
<div id="pthumb"><?php the_post_thumbnail( array(100,100) ); ?></div>
<div id="pcontent">
<?php the_title(); ?>
<?php the_excerpt(); ?><br />
Post Category: <?php the_category( ', ' ); ?>
</div>
</div>
<?php endforeach;
wp_reset_postdata(); ?>
I think you should take a look at the following: https://developer.wordpress.org/themes/basics/template-hierarchy/#home-page-display
You'll notice that, if present, home.php is the file that WP will use to display your posts page - if of course a page separate to your normal front page has been set in Settings > Reading > Posts page.
Then in home.php, you can add whatever you like, ie -
<?php
$args = array(
'show_option_all' => '',
'orderby' => 'name'
);
wp_list_categories( $args );
?>
To clarify, if home.php doesn't exist in your theme, WP will then look for index.php.
There are same query post for page.php,single.php because it will just fetch content. you can differentiate by as follows.
Home Page display add same code (query post) in fallowing
1) home.php
2)index.php
front-page.php – Used for both “your latest posts” or “a static page”
as set in the front page displays section of Settings → Reading.
home.php – If WordPress cannot find front-page.php and “your latest
posts” is set in the front page displays section, it will look for
home.php. Additionally, WordPress will look for this file when the
posts page is set in the front page displays section.
page.php – When “front page” is set in the front page displays
section. index.php – When “your latest posts” is set in the front
page displays section but home.php does not exist or when front page
is set but page.php does not exist.
Single Post
The single post template file is used to render a single post. WordPress uses the following path:
single-{post-type}-{slug}.php – (Since 4.4) First, WordPress looks
for a template for the specific post. For example, if post type is
product and the post slug is dmc-12, WordPress would look for
single-product-dmc-12.php.
single-{post-type}.php – If the post type is product, WordPress would
look for single-product.php.
single.php – WordPress then falls back to single.php.
singular.php – Then it falls back to singular.php.
index.php – Finally, as mentioned above, WordPress ultimately falls
back to index.php.
Single Page
The template file used to render a static page (page post-type). Note that unlike other post-types, page is special to WordPress and uses the following patch:
custom template file – The page template assigned to the page. See
get_page_templates().
page-{slug}.php – If the page slug is recent-news, WordPress will
look to use page-recent-news.php.
page-{id}.php – If the page ID is 6, WordPress will look to use
page-6.php. page.php
singular.php
index.php
Detailed Explanation: click here

How can I add a page with posts to a menu with Wordpress?

I'm very new to Wordpress so sorry if this is a basic question. I looked on the internet but can't seem to find any answer.
I know how to add a page to a sub-menu with Wordpress. But what I want to do is to have a page that contains posts and not just static content. Can someone tell me how I can do this.
If you just need a page that list all your posts, you can tell Wordpress which page to use for displaying posts in Settings > Reading.
If you need to query some specific posts, you can create a template in your theme folder (file name didn't matter). You just put this at the head of the file to tell Wordpress that it's a template file and then can be selected in the page options :
/*
Template Name: My Custom template
*/
Then to query post, use get_posts function.
Example of use :
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
You can achieve this task by admin section.
Procedure:
Go to Settings -> Reading in your wordpress admin area.
In that page, you will have the option choosing which page will display the list of posts in your site.
In "Front page displays" question, In "A static page (select below)" option, you have 2 things namely "Front Page" and "Posts Page".
For "Posts Page" option, select a page from the page list to display all posts.
After saving the options, access the page that you selected for "Posts Page".
You can see the posts are displaying in that page with pagination.

Create a wordpress page with archives of a category

I'm a bit stumped on this one. I'd like to create a page that is an archive of all posts of a certain category - and show excerpts. However, Reading through the Wordpress docs, they say a page cannot be a post or associated with categories.
So, I'm now wondering if this is possible or if there is a work around. I'd really like to put this on a page as I'd like to have a custom sidebar as well. Is this possible with a custom page template? Any other ways this can be done that I am not thinking of?
Thanks!
You can add this function to functions.php and then you can call it from any page template.
If you need to create a new page template for the sidebar.
FTP, download page.php
rename page.php to page-custom.php (custom can be anything, just make sure its page-whatever.php
In the page-custom.php
Replace the comments section with (Custom Template Name can be anything you want)
/**
* Template Name: Custom Template Name
*
*/
replace the loop in your new template with a call to get_posts_custom('catSlug');
then add this to your functions.php
if(!function_exists('get_posts_custom')){
function get_posts_custom($catSlug){
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => $catSlug,
'ignore_sticky_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<article class="post">
<time class="date"><?php the_time('m.d.y') ?></time>
<p><strong><?php the_title(); ?></strong>
<?php the_excerpt(); ?>
</p>
</article>
<?php
endwhile;
}
wp_reset_query();
}
}
Reference http://codex.wordpress.org/Class_Reference/WP_Query
I didn't fully test the function, but I am using a modified version of the same code. Mine doesn't filter on the category slug, so that may need tweaking, but I did test to make sure it didnt break functions.php
Create a page, lipsum for example.
Create a PHP file with the page-[the title].php, page-lipsum.php for this example.
Write your loop in that file.
FTP that file to the directory of your theme.
When you go to the URL for that page, the results should be what you're looking for.

Resources