Hi i'm trying to create a custom page (custom-page.php) that contains some of my blog posts, but when I test the loop i'm not seeing any posts. Its only returning the name of my custom page template as the h2.
I have two posts already published but they are not showing up.
I have a front-page.php which users land on when they first come to my site and I have not changed any settings under the reading tab.
I've read over the Wordpress codex and can't find any solutions so far.
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1>BLOG INDEX PAGE
BLOG INDEX PAGE</h1>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
?>
Please follow this code.
$newsQuery = newWP_Query('post_type=post','post_status=publish');
if ( $newsQuery->have_posts() ) {
while ($newsQuery->have_posts()) {
$newsQuery->the_post();
echo get_the_title();
echo get_the_excerpt();
}
}
and Your complete template will be like this.
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1>BLOG INDEX PAGE
BLOG INDEX PAGE</h1>
<?php
$newsQuery = new WP_Query('post_type=post','post_status=publish');
if ( $newsQuery->have_posts() ) : while ( $newsQuery->have_posts() ) : $newsQuery->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
?>
Create a page named "Blog" from wp admin and then create a file in theme folder named page-blog.php and write the following code below.
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1>BLOG INDEX PAGE</h1>
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'id',
'order' => 'desc'
);
$loop = new WP_Query($args);
if($loop->have_posts()) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
?>
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'offset' => 0
);
$the_query1 = new WP_Query( $args );
if (count($the_query1->posts)>0) {
while ( $the_query1->have_posts() ) : $the_query1->the_post();
get_template_part( 'loop-archive-template-location' );
endwhile;
}
?>
As we mentioned, WP_Query is a PHP class used by the WordPress database. This particular class can do several things, but primarily it’s used to pull posts from the database.
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
// wp_reset_postdata();
here are two main scenarios you might want to use WP_Query in. The first is to find out what type of request WordPress is currently dealing with. The $is_* properties are designed to hold this information: use the Conditional Tags to interact here. This is the more common scenario to plugin writers (the second normally applies to theme writers).
The second is during The Loop. WP_Query provides numerous functions for common tasks within The Loop. To begin with, have_posts(), which calls $wp_query->have_posts(), is called to see if there are any posts to show.
Related
I'm currently working on a WordPress site and I've created a custom post type called 'events' using the CPT UI plugin.
I want to display the events on my home page, so I've tried to create a loop in my homepage template in the theme files. I've been using this as a guide https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/
but for the life of me, I can't get the PHP that is used in that link to work for me.
<?PHP
$args = array( 'post_type' => 'events', 'posts_per_page' => 4 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
When I try to save that I get this error
syntax error, unexpected 'else' (T_ELSE)
I've been searching for an answer for this for a while and I can't find anything.
I'm pretty new to PHP, so sorry if I'm being incredibly stupid. Any help would be appreciated :)
You have not end while loop , place this code also <?php endwhile; ?>
<?php
$args = array( 'post_type' => 'events', 'posts_per_page' => 4 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
I have set a special category for posts that I want to show on the front page here: http://aquadiva.it/en/
some of them won't show though. there should be four.
I suspect the problem lies in the fact that two of the four posts are custom posts. The category itself works, because, if I search for category, what it gives is those four posts: http://aquadiva.it/en/category/frontpage/
while on the front page I have only the ones belonging to "regular" posts (articles) and not to custom posts (products).
this is my content-frontpage.php:
<?php
$nirvanas = nirvana_get_theme_options();
foreach ($nirvanas as $key => $value) { ${"$key"} = $value; } ?>
<section id="container" class="one-column <?php //echo nirvana_get_layout_class(); ?>">
<div id="content" role="main">
<?php //cryout_before_content_hook();
$nirvana_old_posts_per_page = get_option( 'posts_per_page' );
if ( have_posts() ) :
/* Start the Loop */
update_option( 'posts_per_page', $nirvanas['nirvana_frontpostscount']);
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$the_query = new WP_Query( array('posts_per_page'=>$nirvanas['nirvana_frontpostscount'],'paged'=> $paged) );
while ( $the_query->have_posts() ) : $the_query->the_post();
global $more; $more=0;
get_template_part( 'content/content', get_post_format() );
endwhile;
if($nirvana_pagination=="Enable") nirvana_pagination($the_query->max_num_pages); else nirvana_content_nav( 'nav-below' );
else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'No Posts to Display', 'nirvana' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php printf(
__( 'You currently have no published posts. To hide this message either add some posts or disable displaying posts on the Presentation Page in theme settings.', 'nirvana' ),
esc_url( admin_url()."post-new.php"),
esc_url( admin_url()."themes.php?page=nirvana-page") ); ?>
</p>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php
endif;
update_option( 'posts_per_page', $nirvana_old_posts_per_page);
//cryout_after_content_hook();
?>
</div><!-- #content -->
<?php //nirvana_get_sidebar(); ?>
</section><!-- #container -->
what can I do?
Exchange this:
$the_query = new WP_Query( array('posts_per_page'=>$nirvanas['nirvana_frontpostscount'],'paged'=> $paged) );
with this:
$the_query = new WP_Query( array('posts_per_page'=>$nirvanas['nirvana_frontpostscount'],'paged'=> $paged, 'category_name'=>'frontpage', 'post_type' => array('post','product')) );
Let me know if this works. If not, let me know what the name of your custom post type is.
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(); ?>
Is there away to Create a custom php template for a page this is not the front page
For Example:
When you have front-page.php for the homepage i want to do some thing like that.
I don't Know if this is possible but if it is Thank You for your answers
This code you can use to create a custom template:
<?php
/**
* Template Name: Alphabetical Posts
*/
get_header(); ?>
<div id="main-content" class="main-content">
<?php
if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
// Include the featured content template.
get_template_part( 'featured-content' );
}
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
$custom_posts = new WP_Query( array(
'order_by' => 'title',
'order' => 'asc'
));
if ( $custom_posts->have_posts() ) :
while ( $custom_posts->have_posts() ) : $custom_posts- >the_post();
get_template_part( 'content', get_post_format() );
endwhile;
twentyfourteen_paging_nav();
else :
get_template_part( 'content', 'none' );
endif;
?>
</div><!-- #content -->
</div><!-- #primary -->
</div><!-- #main-content -->
<?php
get_sidebar();
get_footer();
the snipet is from https://premium.wpmudev.org/blog/creating-custom-page-templates-in-wordpress/
i am trying to personalize a new theme made in underscore.me, but i can't seem to create a new template page with a loop that can show me posts of one specific category, how can i do this?
I'm gonna paste here the index.php of the underscore.me theme, witch has a generic loop, sadly, copying and pasting this loop on a template page does
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : ?>
<?php if ( is_home() && ! is_front_page() ) : ?>
<header>
<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
</header>
<?php endif; ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
And this is the page.php.
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content', 'page' ); ?>
<?php
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
?>
<?php endwhile; // End of the loop. ?>
</main><!-- #main -->
</div><!-- #primary -->
Good news for you, you dont need a specific loop for a specific category.
if you have a category.php file in your theme, and assume that your category name is "Lux Cars" with id of "35". if you copy your category.php as category-35.php or category-lux-cars.php (slug of your category). When you open the posts with this category. wordpress call this file and show posts in that loop. you can edit your category-35.php and add any category detail or sth.
But if you still want a loop for a specific category then you can use that code;
<?php
$args = array ( 'category' => ID, 'posts_per_page' => 5);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
?>
//Style Posts here
<?php endforeach; ?>