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(); ?>
Related
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);
I've created a customize page show all posts with template:
<?php
/*
Template Name: All posts
*/
get_header();
?>
</header>
<div role="main" id="content" class="content-warp">
<div class="container">
<div id="primary" class="content-area col-md-8 post-list">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<div class="entry">
<?php the_content(); ?>
<?php
$current_date = "";
$count_posts = wp_count_posts();
$nextpost = 0;
$published_posts = $count_posts->publish;
$myposts = get_posts(array('posts_per_page' => $published_posts));
foreach ($myposts as $post) :
get_template_part('content', 'content-single');
?>
<?php endforeach; ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php get_sidebar('page'); ?>
<div style="clear:both"></div>
</div>
</div>
<?php get_footer(); ?>
But it not show post_content of Posts (all remain data are normal).
By the way, with default UI Categories (content.php), i just call below code and everything Ok: the same UI with new template but have post_content).
<?php get_template_part( 'content', 'single' ); ?>
I don't know why post_content is null in new my template. I'm using Llorix One LiteVersion: 0.1.7
Any help, thanks.
I try read document again and realize the_content not set data yet.
So, just add setup post data, look like:
...
foreach($myposts as $post) : setup_postdata( $post );
...
Also You can use new WP Query
$myposts = new WP_QUery(array(
'posts_per_page' => $published_posts
));
if ($myposts->have_posts()): ?>
<?php while ($myposts->have_posts()) {
$myposts->the_post();
get_template_part('content', 'content-single');
} ?>
<?php endif ?>
<?php wp_reset_query(); ?>
Why is my WordPress blog.php only showing one blog entry while I have two entries in the blog at present? I have verified that both are published. And I do have my reading setup to my blog page. Any ideas?.
http://kvalixhu.digitalthinkersni.co.uk/blog/
<?php /* Template Name: BlogPosts */ ?>
<?php get_header(); ?>
<div id="contentWrap" class="group">
<?php get_sidebar(); ?>
<div id="article">
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=5'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<?php $wp_query = null; $wp_query = $temp;?>
<h2><?php the_title(); ?></h2>
<p> Posted By <?php the_author(); ?></p>
<div id="entryItem">
<?php the_content(); ?>
</div><!--entryItem-->
</div><!--article-->
<?php endwhile; ?>
</div><!--contentWrap-->
</div><!--mainWrapper-->
<?php get_footer(); ?>
Put article opening tag inside the loop
I assume, that you want to wrap each posts with a article div. Since more than one container with the same is not allow, you should change this to a class.
Remove $temp
Why are you using $temp? Just remove this, and don't set $wp_query to null.
<?php /* Template Name: BlogPosts */ ?>
<?php get_header(); ?>
<div id="contentWrap" class="group">
<?php get_sidebar(); ?>
<?php
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=5'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="article">
<h2><?php the_title(); ?></h2>
<p> Posted By <?php the_author(); ?></p>
<div id="entryItem">
<?php the_content(); ?>
</div><!--entryItem-->
</div><!--article-->
<?php endwhile; ?>
</div><!--contentWrap-->
<?php get_footer(); ?>
Having some trouble with a wordpress blog. I am trying to show posts with specific categories based on the section of the site the user came from. Everything is working except the 'query_posts' inside of the the if/else statement. I have the following php:
<?php
/*
Template Name: Blog
*/
get_header(); ?>
<?php
// Find out if the user came to the blog from 'Experienced' or 'College' section of the site
$came_from = wp_get_referer();
// Show posts with categories based on where the user came from
if (strpos($came_from,'experienced') !== false) {
$text = 'test';
query_posts('cat=experienced-professionals');
// wp_reset_query();
} else {
$text = 'heyo';
query_posts('cat=college-students');
// wp_reset_query();
}
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div style="background:red;width:100%;height:200px;"></div>
<div id="container">
<div id="content" role="main">
<h1 class="entry-title"><?php the_title(); ?></h1>
<p><?php echo $text; ?></p>
<?php print_r($came_from); ?>
</div><!-- #content -->
</div><!-- #container -->
<?php endwhile; ?>
<?php endif; ?><!--end the entire loop-->
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
I know my referer variable and if/else statement are working because the $text variable changes as it should based on the section of the site I get to the blog from. However no matter how I get to the blog, the page is showing all posts and ignoring the query_posts category inside the if/else statment. Can someone please help?
Nevermind figured this out...hope it helps someone else! Fixed it with this:
<?php
/*
Template Name: Blog
*/
get_header(); ?>
<?php
// Find out if the user came to the blog from 'Experienced' or 'College' section of the site
$came_from = wp_get_referer();
// Show posts with categories based on where the user came from
if (strpos($came_from,'experienced') !== false) {
$text = 'test';
$queryCategory = 'experienced-professionals';
// wp_reset_query();
} else {
$text = 'heyo';
// query_posts('cat=college-students');
$queryCategory = 'college-students';
// wp_reset_query();
}
?>
<?php query_posts( array ( 'category_name' => $queryCategory ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div style="background:red;width:100%;height:200px;"></div>
<div id="container">
<div id="content" role="main">
<h1 class="entry-title"><?php the_title(); ?></h1>
<p><?php echo $text; ?></p>
<p><?php echo $queryCategory; ?></p>
<?php print_r($came_from); ?>
</div><!-- #content -->
</div><!-- #container -->
<?php endwhile; ?>
<?php endif; ?><!--end the entire loop-->
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
-simple blog
-twenty twelve child theme
I need: a second loop in single.php that shows the selected post and all the other posts below.
What I have so far in single.php (results in a blank page) :
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php get_sidebar(); ?>
<?php
// The Second Query
$the_query = new WP_Query();
// The Loop
if ( $the_query->have_posts() ):
while ( $the_query->have_posts() ):
$the_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->
This should do the trick:
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php
$args_second = array(
'posts_per_page' => -1,
);
// The Second Query
$second_query = new WP_Query( $args_second );
// The Loop
if ( $second_query->have_posts() ):
while ( $second_query->have_posts() ):
$second_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->
Notes:
You need to properly show the title and content using the_title() and the_content() inside the single loop.
To show other posts, you need to query them, you'll quickly understand by looking at the code above.
I'll leave the styling for you.
It is tested and working.