i want to display posts in wp page. to do that this is what i did i created a template named mypage-page.php and copied code from page.php to mypage-page.php
this is my mypage-page.php
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ;?></h2>
<?php the_post_thumbnail(); ?>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, no posts to list</p>
<?php endif; ?>
</main><!-- .site-main -->
// this has header and footer as well .
now i created a page mytest using this template ,what i expect it should list posts but it does not, please help me to understand where i'm wrong .
i'm just a beginner
If your page is a Custom Page template then you have initialise a custom query.
WordPress's default query won't work in a custom page template.
You can do that query using WP_Query Class :
https://codex.wordpress.org/Class_Reference/WP_Query
Or
get_posts() :
https://codex.wordpress.org/Template_Tags/get_posts
I'd suggest the earlier one.
you can also use this, query_posts
Related
I am currently using the cubic theme and ran into some trouble with template creation. I have multiple sections on my site and am trying to replicate the functionality of my home page onto another page where ideally the home page would display featured articles and another page for urban exploration would be able to display posts about urban exploration in the same format. I.e. Images with nicely displayed titles in rows of three link to posts I have created.
So far this is what my template looks like:
<?php /* Template Name: Page Directory */ ?>
<?php get_header(); ?>
<?php the_content(); ?>
<div id='primary' class='content-area'>
<main id='main' class='site-main' role='main'>
</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This allows me to display the header with the integrated side bar and footer, but I cannot figure out how to replicate cubic's home page functionality.
I imagine the PHP function would have to query for tags in order to place the post on the page, then look for a post's featured image and title to display it as a square image covering 1/3 of the page. Any help would be greatly appreciated.
Update:
I found you can use WP_Query to create a loop to pull information then display it using get_template_part as a means of formatting the post information. I can now display the images for my posts however the formatting is off because of the way content.php is written. [Do any users of cubic (a child theme in WordPress) know how the homepage is constructed and how I might be able to reference that file so I can recreate its format on a different page in WordPress?]
<?php /* Template Name: Page Directory */ ?>
<?php get_header(); ?>
<?php the_content(); ?>
<main id='main' class='site-main' role='main'>
<?php
$postid = new WP_Query( array( 'tag' => 'featured' ) );
if( $postid->have_posts() ):
while( $postid->have_posts() ): $postid->the_post(); ?>
<?php get_template_part('content',get_post_format()); ?>
<?php endwhile;
endif;
?>
</main><!-- .site-main -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
You need to add the loop which fetches the posts:
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
I realized my website (www.inunfuturoaprile.it) with a minimal Wordpress theme that does not include the category.php template. I have created the template by me: the goal was to get pages that display a list of the posts belonging to a specific category. Unfortunately, due to my scarce knowledge of php, the code does not seem to work properly: I only see one post per category (e.g. https://www.inunfuturoaprile.it/politica/).
Here's the category.php file code:
<?php
/**
* Template di Categoria
*/
get_header(); ?>
<div id="content">
<?php
// Check if there are any posts to display
if ( have_posts() ) : ?>
<?php
// Since this template will only be used for Design category
// we can add category title and description manually.
// or even add images or change the layout
?>
<h1><strong><?php single_cat_title(); ?></strong>: Elenco Articoli</h1>
<div class="entry">
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?> // <small><?php the_time('j F Y') ?></small>
<?php endwhile; // End Loop
else: ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
Someone can help me? Thank you :)
I think your posts_per_page parameter is set to 1.
You can see other posts using pagination:
https://www.inunfuturoaprile.it/politica/page/2/
https://www.inunfuturoaprile.it/politica/page/3/
Check it on Settings->Reading page ( /wp-admin/options-reading.php), parameter "Blog pages show at most"
But it also can be set up somewhere in the theme code.
Changing it may affect your main page - since the main page shows only one post too.
So you can change it only on category.php page. Add this after get_header():
get_header();
global $wp_query;
$wp_query->set('posts_per_page', 10);
// or this - if you need no pagination at all:
// $wp_query->set('nopaging', true);
$wp_query->query($wp_query->query_vars); ?>
Hope someone can help me, I've been struggling for days on this trying to find the answer...
Basically, I have a wordpress site that has a slider (not a plug-in just open source code) which is called to using a 'get_template' but it displays the same three posts on every single page. I have different posts in different categories and want the slider to correspond on each separate page and echo the posts from each particular category.
<div id="slidorion">
<div id="slider">
<?php
query_posts( 'posts_per_page=3' );
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="slide">"><?php the_post_thumbnail(); ?></div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div id="accordion">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="link-header"><?php the_title(); ?></div>
<div class="link-content">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
here is a link to the site if you need to see it to totally understand what I mean and need to do...
http://www.kaijocreative.co.uk/footballnatter
Thanks!
You should alter your query adding cat or category to your query_posts( 'posts_per_page=3' ); according to what you exactly want
see Query_posts () and also have a look at WP_Query class
you need to use find out the category ids from each post, then use these ids in the
$category = get_the_category();
$post_catid= $category[0]->term_id;
$querystr='cat='.$post_catid.'&posts_per_page=3';
query_posts($querystr);
I have my front page set to a static page and am trying to build my custom template. How do I actually show the selected front page in front-page.php? I have googled and googled but can't seem to figure out how to do it.
The front-page.php actually loads like it should, but I can't seem to find documentation on exactly how to show the page that is assigned as the static home page. Any suggestions?
I have tried
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
but that didn't seem to work...
Your static page uses a page template (usually page.php for the default template)
You can create a new one for the homepage if you wish. see : Creating_Your_Own_Page_Templates copy page.php to homepage.php and change the template name
Example template (homepage.php) :
<?php
/*
Template Name: Homepage
*/
//the content of page.php and now you can do what you want.
?>
$id = 0; /* The id of your page */
$page = get_page($id);
echo apply_filters('the_content', $page->post_content);
If its a static page, I should not use a loop.
First, take a look to topic to show something only on home page. a related question is Wordpress Post Thumbnail Issue (Only 1 thumbnail on frontpage). Also, it can be useful how to create a static front page in wordpress.
I was missing something obvious. The loop I was using I had copied out of wordpress template. It actually called another template file. What I should have used was:
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<div class="page-links">' . __('Pages:', 'twentytwelve'), 'after' => '</div>')); ?>
</div><!-- .entry-content -->
<footer class="entry-meta">
<?php edit_post_link(__('Edit', 'twentytwelve'), '<span class="edit-link">', '</span>'); ?>
</footer><!-- .entry-meta -->
</article><!-- #post -->
<?php endwhile;?>
Its been a while since ive done wordpress templating and for some reason i cant get the archives.php to work. i have this:
<?php
/* Template Name: Archives */
get_header(); ?>
<div id='content'>
<?php get_sidebar('left'); ?>
<div id='column2-wide'>
<?php if (have_posts()): while (have_posts()): the_post(); ?>
<?php if ( in_category(16) ) { ?>
<h2><?php the_title(); ?></h2>
<div class="post">
<?php echo the_content(); ?>
</div>
<?php } ?>
<?php endwhile; endif; ?>
</div><!-- column2 -->
<?php get_footer(); ?>
Then created a page in the admin and chosen the Archives template to be used from the dropdown.
However the posts just dont seem to show. Am i missing something? The very same code works in the index.php file. It seems its just not working when im trying to display posts in a page.
It could well be im missing a file as I started developing the theme using a skeleton theme by Kennethreitz which can be found here:
https://github.com/kennethreitz/wordpress-theme-skeleton/
Any help would be appreciated.
Thanks for reading.
fl3x7
EDIT--> Also ive removed the category check so it should just list all posts but instead what it does is just echo the title of the current page if that helps
I'm assuming that "16" is the category ID? According to the WordPress docs for in_category, if you're querying by ID it should be passed as an integer.
$category
(mixed) (required) One or more categories specified by ID
(integer), name or slug (string), or an array of these
With your current code, the in_category check is failing every time since it is checking for an association with the category named "16." Try this instead:
if ( in_category(16) ) {
...
}
Thanks to the help provided by Hans. This is what I did:
query_posts( array ( 'category_name' => 'foo', 'posts_per_page' => 10 ) );
if (have_posts()): while (have_posts()): the_post(); ?>
<h2><a href='<?php the_permalink(); ?>'><?php the_title(); ?></a></h2>
<div class="post">
<?php echo the_excerpt(); ?>
</div>
<?php endwhile; endif; ?>