To loop and show all WordPress posts we use:
<?php
if( have_posts() ) :
while( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endwhile;
endif;
?>
And to show only the first recent post we use:
<?php
if( have_posts() ) :
if( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endif;
endif;
?>
My question is, can I manually select what posts I want to show on my homepage without doing a loop? For example let's say I want to only show post1, post3, and post6 on my homepage, can I do that?
Without using while loop because the post contain only one post.
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=>40'); ?>
<?php if (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endif;?>
<?php get_footer(); ?>
Try this code
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=40'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>
The one I picked had an ID of 40. So, I set p=40. On the page I chose to use this on, I selected the "Query Single Post" post template. Clicked save and refreshed my page.
You need to use pre_get_posts filter.
Code:
<?php
add_action('pre_get_posts', function($query){
if ( !$query->is_home() or !$query->is_main_query() )
return false;
# And here you can use any parameters from [WP_Query Class](https://codex.wordpress.org/Class_Reference/WP_Query)
# For example
$query->set('post__in', [10, 15, 16]);
});
Related
This is my first attempt at creating a theme purely from scratch. Before this I just used underscores_me and made most of my changes to style.css and left the majority of PHP alone, because I'm a novice with it.
My issue is that plugins are not working. None of the ones I have installed work. At this point I have been trying plugins that create an event calendar, but I'm assuming that any and all plugins will have issues. In the areas that are meant to display the calendar, there are two things I'm seeing. Plugin-generated pages show nothing at all (aside from the theme visuals) and plugins that are inserted into an admin-created page display plugin-generated code.
I am using WampServer. I have wp_footer(); and wp_head(); in the correct places. My functions.php file was created from the example found at https://scanwp.net/blog/create-a-wordpress-starter-theme-from-scratch/ and the only adjustment I have made to it so far is to remove the fontawesome line of code.
My index.php file looks like this:
<?php get_header(); ?>
<h1><?php echo "☛ "; ?><?php the_title(); ?></h1>
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display post content
endwhile;
endif;
?>
<?php get_footer(); ?>
And my page.php file looks like this:
<?php get_header(); ?>
<h1><?php echo "☛ "; ?><?php the_title(); ?></h1>
<?= get_post_field('post_content', $post->ID) ?>
<?php get_footer(); ?>
First of all, you have to understand that in your displayed files, there are no functions that recall objects that will later show the page content.
Then, in your index.php file there is an error, besides what was said above, because you're calling the_title () (function) that extrapolates the title of a post or page via the post object, which in this case should be extracted within the while loop contained within the if condition.
Then try editing the files as follows.
index.php
<?php
get_header(); ?>
<?php if( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if( is_singular() ) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else: ?>
<h1>No posts to display</h1>
<?php endif; ?>
<?php get_footer(); ?>
and page.php
<?php
get_header(); ?>
<?php while( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
<?php get_footer(); ?>
However, within the wordpress codex you will find all the guides well written on any type of function, look no further.
source: https://codex.wordpress.org/
I have a simple custom page template with a simple loop that displays links to posts of the same category ID=11.
However, the problem is that although the links are working correctly, all posts are displaying the same content (the content of the first post). I can't work out why this is. Any help would be really appreciated, thanks.
Here is the loop on the custom page template
<?php
$args = array('cat' => 11);
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
else:
// no posts.
endif;
?>
And here is what I have on single.php
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
You should call the_post() in single.php before doing anything else.
Try this:
<?php the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
Leave your other code alone. It looks like it should be working as expected.
Worked it out through some trial and error. I had a list of post titles in the sidebar and needed to use wp_reset_query.
In single.php use the following code to get the content and title.
while ( have_posts() ) : the_post();
the_title(); // For post title
the_content(); //For post content
endwhile;
use this in your custom page, i used wp_reset_postdata();
<?php
$args = array('cat' => 11);
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
wp_reset_postdata();
else:
// no posts.
endif;
?>
And on single.php use this
<?php
while ( have_posts() ) : the_post();
the_title(); // For post title
the_content(); //For post content
endwhile;
?>
I'm trying to setup a way to display related posts by category and exclude current one, but when a category has only one post, my current code to display anything else, is not working.
This is the code that I have so far. Any ideas why?
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title() ?></h1>
<?php the_content();?>
<?php $current_post_id = get_the_ID(); $post_cats = wp_get_post_categories( get_the_ID(), NULL ); ?>
<?php query_posts('showposts=8&orderby=desc&cat='.implode(',',$post_cats)); if (have_posts()) : ?>
<?php $i=0; while (have_posts()) : the_post(); if($current_post_id==get_the_ID()) continue; $i++; ?>
<span class="related-title"><?php short_title('...', 43); ?></span>
<?php endwhile;?>
<?php else : ?>
<p>Sorry! There is no more related posts in this category.</p>
<?php endif; wp_reset_query(); ?>
<?php endwhile;endif ?>
<div style="clear:both;"></div>
<?php get_template_part('includes/loop-tips');?>
I am very newbie into wordpress development so i had face a issue for displaying some post for specific category...
Like, I am adding(posting) some images into database for post_picture category...
Now at front end i have a page name Picture Mania where i just want to display only those image which i added into post_picture category...
For this purpose first i installed the php-exec plugin , and now I am trying to retrieve my desire category images on that page by this code
<?php query_posts('post_picture =post_picture&showposts=5');
while (have_posts()) : the_post();
// do whatever you want
?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php cup_post_nav(); ?>
<?php comments_template(); ?>
<b><?php the_title(); ?>
<?php
endwhile;
?>
It working fine, but displaying all category images, so conclusion I am not getting my desire output...
Answer will be much appreciated....and thanks for help in advance
You can pass category id (cat=3) such as
<?php query_posts('cat=3&showposts=5');
while (have_posts()) : the_post();
// do whatever you want
?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php cup_post_nav(); ?>
<?php comments_template(); ?>
<b><?php the_title(); ?>
<?php
endwhile;
?>
Or you can use category name in arary as well:
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
query_post Reference: http://codex.wordpress.org/Function_Reference/query_posts
Although WP_Query is recommended approach instead of query_posts
for showing posts of specific category, you could add cat=YOUR_CATEGORY_ID in query_posts() like:
query_posts('post_picture=post_picture&showposts=5&cat=$your_Category_Id');
as in query_posts() example
try this:
<?php query_posts('cat=your-post_picture-category-id&showposts=5');
while (have_posts()) : the_post();
// do whatever you want
?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php cup_post_nav(); ?>
<?php comments_template(); ?>
<b><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a>
This is what I have so far. Unfortunately this grabs ALL my pages' titles and content including sub-pages. I only want to display top-level main pages. How can I do this?
<?php query_posts('post_type=page'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
*Note: I want to display both title and content for the pages. Not just title. Otherwise I would use:
<?php wp_list_pages('depth=1'); ?>
UPDATE: Following konsolenfreddy's advice I was able to loop through my pages properly. However a new problem arose. The content is being striped of its tags. Anyway I can keep them? Here's my new code:
<?php
$pages = get_pages('parent=0');
foreach ($pages as $pagg) {
$option .= $pagg->post_title;
$option .= $pagg->post_content;
echo $option;
}
?>
You can use get_pages() (see http://codex.wordpress.org/Function_Reference/get_pages ), it takes the same arguments as wp_list_pages()
Does this work?
<?php query_posts('post_type=page&post_parent='.$parent);?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>