In my new wordpress theme, i made a static front page and a sub page for blog posts, but the problem is, the posts dont get displayed. If i use it without the static front page the posts display normally. What could be wrong?
Here is my code for the blog page:
<?php
/*
Template Name: Blog page
*/
get_header();
?>
<!--parallax section 1-->
<div class="section-one">
<div class="parallax-section-1">
</div>
<div class="container-fluid">
<h1>News</h1>
</div>
</div>
<div class="container-fluid">
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post">
<h1><?php the_title(); ?></h1>
<h6>Posted on <?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></h6>
<div class ="entry">
<?php the_content(); ?>
</div>
</div>
<?php
endwhile;
else:
?>
<p> Sorry, no posts to display.</p>
<?php endif; ?>
</div>
<?php get_footer(); ?>
insert before "if"
$args=array(
'post_status' => 'publish',
'posts_per_page' => 4,
'order' => 'ASC'
);
$my_query = new WP_Query($args);
Related
I'm designing a theme from scratch. It's an One page site and my CMS is WordPress.
My folder structure is :
front-page.php
page-about.php
page-work.php
page-contact.php
I can display all my pages on the front-page.php as section but I lose all my custom classes (and specific styles). Here the loop in my front-page.php :
<?php get_header(); ?>
<?php query_posts('post_type=page&order=ASC'); ?>
<?php
if (have_posts()):
while (have_posts()) : the_post(); ?>
<section id="page-<?php the_ID(); ?>">
<div class="container">
<h2><?php the_title(); ?></h2>
<article>
<p><?php the_content(); ?></p>
</article>
</div>
</section>
<?php endwhile;
endif; ?>
<?php get_footer(); ?>
How can I keep the style (therefore, their classes/id's) of the page-xxx.php as section in my front-page.php ?
Thank you
you can add a custom class to specific page id, example :
add_filter('body_class', 'your_body_class');
function your_body_class($classes) {
if (is_page(xxx))
$classes[] = 'new-class';
return $classes;
}
you can use this plugin Custom Body Class
I did this (in the front-page.php) :
<?php get_header(); ?>
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ( $query->have_posts()) {
$query->the_post();
$tn = get_page_template_slug($post_id);
$word = array("page-", ".php",' ');
$template = str_replace($word,'',$tn);
get_template_part('page', $template);
}
}
?>
<?php get_footer(); ?>
Look like it works, I have three sections with my three pages and their classes. But now, I have no content.
My page-about.php :
<?php get_header();
/* Template Name: About */ ?>
<section id="about" class="section section-about">
<div class="container">
<h2 class="title-2"><?php the_title(); ?></h2>
<div class="col-md-6 col-lg-12"></div>
<article class="article-about">
<?php
if (have_posts()):
while (have_posts()): the_post(); ?>
<p><?php the_content(); ?></p>
<?php endwhile;
endif;
?>
</article>
</div>
</section>
<?php get_footer(); ?>
I'm building a theme and on my category.php page I want to show several full posts (let's say 3, but need to be able to change this to 2 or 1 easily), and then the rest of the posts in the category as title links.
I have quite a bit of HTML in my loop for styling my posts and adding custom fields so sorry about all the code, but this is what my category.php page looks like now. I've tried a few things that haven't worked so have edited this to show my original code which just has a normal list of posts. I'm somewhat new to editing The Loop so would appreciate as much explanation/clarity as possible.
<?php
/**
* The template for displaying Category Archive pages.
*/
get_header(); ?>
<div id="primary" class="<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>">
<div id="feature-container" class="full-width-container">
<div class="full-width-container content-page" id="tagline-wrapper">
<div id="left-black"></div>
<div class="page-width-container">
<div id="tagline-box">
<h1 class="category-title">Transactions</h1>
</div>
</div>
</div>
</div>
<div id="content-wrapper">
<div id="project-menu" class="page-width-container">
<?php wp_nav_menu( array( 'theme_location' => 'project-types' ) ); ?>
</div>
<div id="content" role="main" >
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="story-container" class="module-container">
<div class="our-story">
<div class="story-image">
<?php
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<div class="story-text">
<article class="post" id="post-<?php the_ID(); ?>">
<div class="entry-container">
<h2><?php the_title(); ?></h2>
<div class="project-details">
<p><span class="details-location"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-location', true);
wp_reset_query();
?></span><br />
<span class="details-funding"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-type', true);
wp_reset_query();
?> | <?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-source', true);
wp_reset_query();
?></span><br />
<span class="details-value"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-value', true);
wp_reset_query();
?></span></p>
</div>
<div class="entry">
<?php the_content(); ?>
<?php wp_link_pages(array('before' => __('Pages: ','html5reset'), 'next_or_number' => 'number')); ?>
</div>
<?php edit_post_link(__('Edit this entry','html5reset'), '<p>', '</p>'); ?>
</div>
</article>
</div>
</div>
</div>
<?php endwhile; endif; ?>
</div><!-- #content -->
</div>
</div><!-- #primary -->
<?php get_footer(); ?>
You can achive above thing using following code:
First you have to loop all post and and put counter when it reach more then 2 its stop to print a content.but title will be there always.
<?php $countPost=1;?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<h2 id="post-<?php the_ID(); ?>">
<?php the_title(); ?></h2>
<?php if($countPost>2) : /*Condition for Content*/
the_content();
endif;
?>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft">
<?php posts_nav_link('','','« Previous Entries') ?>
</div>
<div class="alignright">
<?php posts_nav_link('','Next Entries »','') ?>
</div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
<?php endif; ?>
</div>
For more details please refer :
https://codex.wordpress.org/The_Loop_in_Action
I figured out a bit of a workaround solution on my own, although it relies on using plugins/widgets which isn't what I'd prefer.
I simply set the Reading settings to display 2 posts, and then below the Loop I added a widget area and used the Recent Posts Extended widget to display a list of titles/links. This widget allows you to skip a certain amount of posts in the list, so I set it to start at post #3. There was no option to show posts from the current category only, so I had to use the Widget Context plugin as well and make individual widgets with a specific category to show on each corresponding category page. As I said, a bit of a convoluted solution, but the end result is exactly what I wanted to achieve.
I created a page template for join all posts from a specific post_type in one page, everything is working except the pagination. It show the right number of posts for page but, in this case, I have 8 posts and it only show 5. I did some changes but without success. Any ideas?
<?php
/*
Template Name: News
*/
?>
<?php get_header(); ?>
<!-- begin colLeft -->
<div class="container">
<main id="main" class="container" role="main">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array('paged' => get_query_var('paged'), 'posts_per_page'=>5, 'post_type'=>'our-work', 'order' => 'ASC'))
?>
<br><br><br>
<!--header-->
<div class="page-header">
<img src="www.wtk.com/img/3428245.png">
</div>
<div class="inform">
Display text here
</div>
<br>
<div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="news-title">
<h3><?php the_title(); ?></a></h3>
</div>
<div class="news-box">
<?php the_post_thumbnail(); ?>
<?php the_excerpt(); ?>
Read more →
</div>
<br><br>
<?php endwhile;?>
<?php if (function_exists("emm_paginate")) {
emm_paginate();
} ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Not found'); ?></p>
<?php endif; ?>
</div>
</div>
<!-- end colleft -->
<?php get_footer(); ?>
I can see in your code you have 'posts_per_page' => 5. That would explain why you are seeing only 5 posts on the page.
I have WordPress onepage and I displayed posts from specific category in one of page.
When I click to link with permalink href, I am redirecting to home page with added /post-name/ to url, but no to post page. I have index.php and single.php.
I have this index.php:
<?php
query_posts(array(
'post_type' => 'page',
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => 'menu_order'
));
$tpl_parts = array(
'5' => 'about',
'7' => 'team',
'76' => 'tech',
'81' => 'services',
'101' => 'contact',
);
?>
<?php get_header('home'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if(array_key_exists($post->ID, $tpl_parts)) : ?>
<?php get_template_part('template-parts/'. $tpl_parts[$post->ID], 'template'); ?>
<?php else: ?>
<section id="<?php echo $post->post_name; ?>">
<div class="container">
<div class="row">
<?php the_content(); ?>
</div>
</div>
</section>
<?php endif; ?>
<?php endwhile; else : ?>
<?php endif; ?>
<?php get_footer(); ?>
This code show all pages in index.php by template part, it's working.
When I added to services page, a few posts like this:
<section id="services" class="services-section">
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2 class="text-left">Services</h2>
</div>
</div>
<?php $inner_query = new WP_Query( 'category_name=services' ); ?>
<?php if ( $inner_query->have_posts() ) : while ( $inner_query->have_posts() ) : $inner_query->the_post(); ?>
<div class="row box-service">
<div class="col-sm-6 col-xs-12">
<?php the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
</div>
<div class="col-sm-6">
<h3><?php the_title(); ?></h3>
<p class="intro"><?php echo the_field('short_caption'); ?></p>
More
</div>
</div>
<?php endwhile; else: endif; wp_reset_postdata(); ?>
</div>
</section>
This code isn't working, becouse when I want to click on link and go to post, website is refreshing with url localhost/mywebsite/name-of-post/ but I want to redirect to post page. I have a single.php file:
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
</div>
</section>
<?php endwhile; else: endif; ?>
<?php get_footer(); ?>
What's wrong ? How can I fix that ? My theme ignore files like page.php or single.php
Thanks for help.
Are You trying to use Wp_Query to single.php too ?
And maybe try to change posts_per_page in top index to number of your pages instead of -1.
you can try this for display single post page.
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
</h2>
<div class="entry">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
Ok, after a few hours spending on this problem, I tried to find why page.php and single.php is ignored.
Polylang plugin - after turned it off. Everything working fine.
Thanks everybody for help me.
I'm trying to use WP pagination on post archive page but exclude posts from one category to be shown there.
When I add this to my code the page2,3,4... of the archive display the same first 10 posts:
<?php query_posts('cat=-4');?>
This is the whole code of my page template so I would be grateful for all your help:
<?php
/*
Template Name: Post archive
*/
?>
<?php get_header(); ?>
<div class="container">
<div class="content col-md-9">
<div class="home-content">
<!-- Show posts -->
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged'=> $paged,
'posts_per_page'=> 10
);
query_posts($args); ?>
<?php query_posts('cat=-4');?>
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
<div style="float:left; margin:1%;">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'thumbnail', array( 'class' => 'img-post')); // show featured image
}
?>
</div>
<h1 class="post-thumb"><?php the_title(); ?></h1>
<h4>Category: <?php the_category(', '); ?></h4>
<p><?php the_excerpt(); ?></p>
<hr style="margin-bottom:5%">
<?php endwhile; ?>
<!-- pagination -->
<div class="nav-previous alignleft" style="margin-top:-1%"><?php next_posts_link( 'See older posts' ); ?></div>
<div class="nav-next alignright" style="margin-top:-1%"><?php previous_posts_link( 'See newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
</div>
<div class="col-md-3 sidebar unstyled">
<?php dynamic_sidebar( 'home1' ); ?>
</div>
<div class="col-md-3 sidebar unstyled sidebar-space">
<?php dynamic_sidebar( 'home2' ); ?>
</div>
<div class="col-md-3 sidebar unstyled sidebar-space">
<?php dynamic_sidebar( 'articles1' ); ?>
</div>
</div>
</div>
</body>
</html>
<?php get_footer(); ?>
This code fixed my page:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'cat'=> -4,
'paged'=> $paged,
'posts_per_page'=> 10
);
query_posts($args); ?>
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
Modifying to reflect OP's solution for benefit of future readers
Change query() slightly as shown below
$args = array(
'cat'=> -4,
'posts_per_page'=> 10,
'paged'=> $paged
);