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
Related
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.
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.
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')) { ... }
I have created custom post type portfolio in wordpress. I have created categories like web-design, logo design, e-commerce, photography... etc. I have retrieved all these category as the navigation menu in portfolio template file (assigned to the portfolio page). Now I want there should be a link(view all) in the navigation menu which will retrieve all the posts from all the categories in portfolio. Basically I want a way for making default category which will retrieve all the posts. Can anybody please help me!!!
So, basically, you want to create a Custom Post Type Archive page.
A way to achieve this, as explained at WPBeginner.com, is like the following:
Create a custom page template (the PHP file could have any name you want) and Add the Template Name comment at the beginning of the file:
<?php /* Template Name: Custom Post Type Archive */ ?>
Create a custom loop:
<?php
/* Template Name: Custom Post Type Archive */
get_header();
?>
<?php
global $query_string;
query_posts($query_string . "post_type={YOUR-CUSTOM-POST-TYPE}&post_status=publish&posts_per_page=10");
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php
endwhile;
endif;
?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('Previous entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next entries') ?></div>
</div>
<?php wp_reset_query(); ?>
<?php get_sidebar(); ?>
<?php get_footer();?>
Create a new page and select the template you just created in the Page Attributes box
You should be able to use the archive page template to list them all then. Try going to http://yourdomain.com/portfolio and see if that brings it up. You will need to have enabled archives for the post type when you created it though. Look for 'has_archive' => true, if you created the post type manually by dropping in the code in your functions.php file. If you see that, but its set to "false", you'll need to change it to true.
You could also create a custom archive template for this post type if you wanted to. If you create a new file in your theme directory called archive-portfolio.php and then put a custom loop in there the post type will automatically target that template file when accessing the post type archive.
Then to link from your nav menu to this archive, just create a custom menu item in your menu editor with the url http://yourdomain.com/portfolio. You won't need to create any sort of "default" category if you take advantage of the post type archives.
Let me know if you have any other questions.
I am developing a wordpress blog theme, which shows the posts on the homepage. I am trying to show some text ONLY on the homepage, and not on page 2,3 ,4 and so on of the blog posts. The code below shows the text on all blog pages:
<?php
if (is_front_page()) {
?><p>TEST FRONT PAGE</p>
<?php
} ?>
How do I show this ONLY on the front home page (page 1, not pages after 1)?
ANSWER FOUND IN COMMENT BELOW
Instead of declaring an additional variable and checking it you can access the global in wp_query with is_paged.
<?php if (is_home() && !is_paged()) : ?>
Your front page content.
<?php endif; ?>
Or if you have a static post as the front page with its own sub pages you can use:
<?php if (is_front_page() && !is_paged()) : ?>
Your static front page content.
<?php endif; ?>
To target just the first page of the static post.