Wordpress posts displaying the same content - php

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;
?>

Related

Post in WordPress

I've stuck in dev WP theme, please help me.
The problem is when I wrote code for displaying post in front-page.php, it display me the_title() and the_content() from page, not from post.
There is code:
<?php if(have_posts()): ?>
<?php while(have_posts()) : the_post(); ?>
<h2 class="blog-post-title"><?php the_title( ); ?> </h2>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p><?php __('No post found') ?></p>
<?php endif; ?>
It displays me post only if I go to post location e.g. localhost/2017/10/16/first-post/.
There is picture how it correctly shows but on another location.
http://prntscr.com/gy922y
Since you are displaying in a custom page, You'll need to declare and specify post_type as post
Here is a working example:
<?php
$args = array(
'post_type' => 'post'
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
?>
<h2 class="blog-post-title"><?php the_title( ); ?> </h2>
<?php the_content(); ?>
<?php
}
}
?>

Wordpress taxonomy.php loop only showing 2 posts?

I'm having some real trouble with the below code in my taxonomy.php template in Wordpress. The query is working (i.e pulling posts only from within that custom taxonomy) but it is only displaying 2 posts (4 are in the taxonomy).
All my efforts to convert this into a standard loop using $args simply result in posts from all taxonomies being pulled into the page. I was hoping it is as simple as adding posts_per_page => -1 but this just causes every post in the entire site to display.
As I understand it from the codex, taxonomy pages should pull the relevent posts by default, without a need for a loop?
Any help much appreciated!
taxonomy.php
<?php get_header(); ?>
<main>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<figure>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<figcaption>
<h4><?php the_title(); ?></h4>
<h5><?php the_excerpt(); ?></h5>
</figcaption>
</figure>
<?php endwhile; ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
UPDATE
<main>
<?php
$args = array(
'posts_per_page' => -1
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<figure>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<figcaption>
<h4><?php the_title(); ?></h4>
<h5><?php the_excerpt(); ?></h5>
</figcaption>
</figure>
<?php endwhile; ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
If you have 6 different taxonomies then there will be 6 different template files to show those taxonomies appropriately. In your case your templates will be taxonomy-topics.php taxonomy-places.php taxonomy-dates.php taxonomy-interviewee.php taxonomy-period.php and taxonomy-a-z.php
In this way once these templates are created your templates will be showing appropriate posts. To achieve that you can use posts_per_page argument or you can visit this page for better understanding about fetching posts WP_Query Codex Page Hope that makes sense by now

Manually show specific posts on homepage

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]);
});

Display post only for specific category

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>

How do I exclude sub-pages from a page loop?

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; ?>

Categories