-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.
Related
I am a beginner trying to create my own wordpress theme.
I have two nav menus one for the header one for the footer but the secondary one for the footer is not showing up. My code is currently as shown below.
- Functions.php
function base_theme_setup(){
add_theme_support('menus');
register_nav_menu('primary','Primary Header Navigation');
register_nav_menu('secondary','Secondary Footer Navigation');
}
add_action ('init', 'base_theme_setup');
- footer.php
<footer>
<?php wp_nav_menu(array('theme_location'=>'secondary')); ?>
</footer>
<?php wp_footer (); ?>
</body>
</html>
Managed to solve it.
Had some useless undeleted code at the bottom of my index.php. Once deleted the problem went away.
So went from this
<?php
if( have_posts() ):
while( have_posts() ): the_post(); ?>
<h3><?php the_title(); ?>
</h3>
<div class="thumbnail-img"><?php the_post_thumbnail('large'); ?></div>
<p><?php the_content(); ?></p>
<small>Posted on: <?php the_time('F j,Y'); ?> at <?php the_time('g:i a'); ?>, in <?php the_category(); ?></small>
<hr>
<?php endwhile;
endif;
?>
<?php
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
?>
<?php get_footer();?>
to this
<?php
if( have_posts() ):
while( have_posts() ): the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_post_thumbnail('large'); ?>
<p><?php the_content(); ?></p>
<small>Posted on: <?php the_time('F j,Y'); ?> at <?php the_time('g:i a'); ?>, in <?php the_category();?></small>
<hr>
<?php endwhile;
endif;
?>
<?php get_footer();?>
Thanks for everyones tips!
How can I create a full-width post every 4 post grids? As in the following picture.
Here is my code wordpress loop.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('col-sm-6 grid-item '); ?>>
<div class="entry-image">
<?php the_post_thumbnail('grid-thumb'); ?>
<?php unbranded_entry_footer(); ?>
</div>
<header class="entry-header">
<?php
the_title( '<h3 class="entry-title">', '</h3>' );
if ( 'post' === get_post_type() ) : ?>
<div class="entry-meta">
<?php unbranded_posted_on(); ?>
</div><!-- .entry-meta -->
<?php
endif; ?>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php echo unbranded_string_limit_words(get_the_excerpt(), 18); ?>…</p>
</div><!-- .entry-content -->
</article>
<?php endwhile; endif; ?>
May I know are you using visual composer or creating custom template?
If using visual composer you can do it from back-end or place the condition to disable the sidebar from right using
above the sidebar in page.php
<?php if(!is_page('your-page-slug' or ID)) { ?>
/*sidebar code*/
<?php } ?>
single.php has two loops, one to show selected post and the other to show all other posts.
Now I want to exclude the post returned from the first loop.
I found this snippet <?php query_posts(array('post__not_in' => array($thePostID))); ?>
I don't know how to incorporate that into my code or if there is another way to solve this
here my single.php code:
<?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 -->
Please try this code in place of your single.php code.
Test it.
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php
$firstPosts = array();
while ( have_posts() ) : the_post(); ?>
$postid = get_the_ID();
$firstPosts[] = $postid;
<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( array('post__not_in' => $firstPosts, 'order' => 'ASC', 'posts_per_page' => -1 ));
// 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 -->
I'm having problem in showing posts in my index page in wordpress the title and contents doesnt show. Currently i'm modifying the index.php of twenty-twelve template to show the custom post by id, my goal is to show 3 posts in my index page, here's my code below:
<?php
/**
* The main template file
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* For example, it puts together the home page when no home.php file exists.
*
* #link http://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<br><br>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<?php if ( current_user_can( 'edit_posts' ) ) :
// Show a different message to a logged-in user who can add posts.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'No posts to display', 'twentytwelve' ); ?></h1>
</header>
<div class="entry-content">
<p><?php printf( __( 'Ready to publish your first post? Get started here.', 'twentytwelve' ), admin_url( 'post-new.php' ) ); ?></p>
</div><!-- .entry-content -->
<?php else :
// Show the default message to everyone else.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
</header>
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
The code above is the default index.php of twenty-twelve template in wordpress. I replaced this code from:
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
To this, because i want to show only post 23 by id.
<?php $the_query = new WP_Query( 'page_id=23' ) ?>
<?php while ( $the_query->have_posts() ) : ?>
<?php $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
So the whole code for my new index.php is:
<?php
/**
* The main template file
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* For example, it puts together the home page when no home.php file exists.
*
* #link http://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header();
?>
<div id="primary" class="site-content">
<div id="content" role="main">
<br><br>
<?php if (have_posts()) : ?>
<?php $the_query = new WP_Query('page_id=23') ?>
<?php while ($the_query->have_posts()) : ?>
<?php $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<?php
if (current_user_can('edit_posts')) :
// Show a different message to a logged-in user who can add posts.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('No posts to display', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php printf(__('Ready to publish your first post? Get started here.', 'twentytwelve'), admin_url('post-new.php')); ?></p>
</div><!-- .entry-content -->
<?php
else :
// Show the default message to everyone else.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('Nothing Found', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php _e('Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve'); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
That's all what i have guys. Can you help me? Is there anything wrong on showing the posts? Thanks in advance guys. :)
An approach if u dont want to use Wp_query
Try this:
<div id="primary" class="site-content">
<div id="content" role="main">
<br><br>
<?php if (have_posts()) : ?>
<?php $post = get_post(23);?>
<?php echo $post->post_title; ?>
<?php echo $post->post_content; ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<?php
if (current_user_can('edit_posts')) :
// Show a different message to a logged-in user who can add posts.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('No posts to display', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php printf(__('Ready to publish your first post? Get started here.', 'twentytwelve'), admin_url('post-new.php')); ?></p>
</div><!-- .entry-content -->
<?php
else :
// Show the default message to everyone else.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('Nothing Found', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php _e('Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve'); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
You need to display post with id 23. Right?
<?php if (have_posts()) : ?>
<?php $the_query = new WP_Query('p=23') ?>
<?php while ($the_query->have_posts()) : ?>
<?php $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
You are missing little like this :
$query = new WP_Query( 'p=7' );
ALternate you can try query_posts() for specific post in query like this:
<?php query_posts('p=23'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>
I want to have my latest post show up with all info(title, author, thumbnail, and content) but only the title of the second most recent post in another div. Here is my code. It renders the divs correctly but the 'title' in the second div is the 'title' of the latest post still.
<div id="blog-pane">
<div id="blog-post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-title">
<?php the_title(); ?>
</div>
<div id="post-author">
<?php the_author(); ?>
<?php the_date(); ?>
<?php the_time(); ?>
</div>
<div id="post-image">
<?php the_post_thumbnail(); ?>
</div>
<div id="post-text">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php rewind_posts(); ?>
</div>
<div id="post-link-1">
<?php
query_posts( 'p' );
while ( have_posts() ) : the_post();
the_title();
endwhile;
wp_reset_query();
?>
</div>
</div>
</div>
<?php get_footer(); ?>
you can try to introduce some skip logic to have it skip the first post,
<div id="post-link-1">
<?php
query_posts( 'p' );
$count = 0;
while ( have_posts() ) {
if ($count++ == 0) {
the_post();
continue;
}
the_post();
the_title();
}
wp_reset_query();
?>
</div>