Wordpress - Only the latest post shows up on post page? - php

I'm creating a custom theme and in the single.php file, i have the main loop, however if i navigate to different posts, all that shows up is the latest post. I could go to the different permalinks, but the only post that shows up is the latest one. How do I make it so that the correct post shows up?
I tried changing the permalinks and deleting and adding pages, but I get the same result.
Here is single.php
<?php while(have_posts()): the_post();?>
<section class="main-section">
<header>
<h1 class="section-header"><?php the_category('/');?></h1>
<ul class="section-nav">
<?php
$cat = get_the_category()[0];
$categoryPosts = get_posts(array('category' => $cat->term_id));
foreach($categoryPosts as $post): setup_postdata($post);
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
</header>
<h2 class="section-blurb"><?php the_title();?></h2>
<div class="info">
<?php the_content(); ?>
</div>
</section>
<?php endwhile;?>
Everything works..but only for the latest post. How do I make it so that the appropriate post is shown?

you can use orderby for sorting order
'orderby'
$categoryPosts = get_posts(array('category' => $cat->term_id, 'orderby' => 'post_date', 'order' => ASC));
For reference see this link
http://codex.wordpress.org/Template_Tags/get_posts

Hi it sounds like a simple fix. Is this a child theme. Try saving the current single.php file and replace it with a new single.php file use a wp theme the following is from wp 2012 single.php markup
<?php
/**
* The Template for displaying all single posts
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
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() ); ?>
<nav class="nav-single">
<h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
<span class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '←', 'Previous post link', 'twentytwelve' ) . '</span> %title' ); ?></span>
<span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '→', 'Next post link', 'twentytwelve' ) . '</span>' ); ?></span>
</nav><!-- .nav-single -->
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Related

Pagination doesnt work in custom post type

Pagination template part includes common pagination function with style. The template part works for archive.php (it's for "single", you know default wp file) but doesn't work for custom post type.
Why not? How to solve it?
<?php get_header(); ?>
<main role="main">
<!-- section -->
<?php get_template_part( 'breadcrumb' );?>
<!-- Inner Pages Main Section -->
<section class="ulockd-service-details">
<div class="container">
<div class="col-md-12">
<div class="row">
<?php
/**
* Setup query to show the ‘services’ post type with ‘8’ posts.
* Output the title with an excerpt.
*/
$args = array(
'post_type' => 'team',
'post_status' => 'publish',
'posts_per_page' => 1,
);
$loop = new WP_Query( $args );
if (have_posts()): while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php //if (have_posts()): while (have_posts()) : the_post(); ?>
<?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
?>
<div class="col-md-12 ulockd-mrgn1210">
<div class="ulockd-project-sm-thumb">
<img class="img-responsive img-whp" src="<?php printf( '%s', esc_url($image_src[0]) ); ?>" alt="">
</div>
</div>
<?php
}
?>
<div class="col-md-12 ulockd-mrgn1210">
<article class="ulockd-pd-content">
<div class="ulockd-bp-date">
<ul class="list-inline">
<li class="ulockd-bp-date-innner">On <span class="text-thm2"><?php the_time('j'); ?></span> / <?php the_time('F Y') ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-nurse-head text-thm1"></span> <?php the_author_posts_link(); ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-chat text-thm1"></span> <?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' )); ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-black-check-box text-thm1"></span> <?php the_category(); ?></li>
</ul>
</div>
<h3><?php the_title(); ?> </h3>
<p class="project-dp-one"><?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?></p>
<a class="btn btn-lg ulockd-btn-thm2" href="<?php the_permalink(); ?>"> Read More</a>
</article>
</div>
<?php get_template_part('pagination'); ?>
<?php endwhile; ?>
<?php else: ?>
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>
<?php endif; ?>
</div></div></div></section>
<?php get_footer(); ?>
</main>
First of all, you don't need to include that template inside while loop. That's wrong.
Then, if you want to have an archive page for your team post type, you need to provide 'has_archive' => true within register_post_type() function args.
Also, consider changing archive page default slug if needed. If you do so, you need to open Settings > Permalinks for resetting your permalinks structure.
Then you could either use standard archive.php for the whole team archive page and standard template-parts/content.php for one post inside the loop or rewrite either of those by creating archive-team.php or content-team.php. And the_posts_pagination() function will work on proper archive page (either archive.php or archive-team.php).
I would try two things.
Add the argument paged to your wp_query args :
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'team',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 1,
);
Put your pagination template outside the loop :
<?php endwhile; ?>
<?php get_template_part('pagination'); ?>

Trying to display all posts in my archive page but it is only returning the page content

I'm having a problem with displaying all posts in my archive page using the have_posts loop. When I try to return the title and the content, it returns the template page's title and content instead of the posts' content.
What I did to try and fix it is I ran a WP_Query post loop which grabs all the posts of post type = post and it displays the content just fine. The only problem is I want a dynamic archive page so when users select a tag - it will take them to an archive page with all the posts containing that tag. Since I am running a specific WP_Query, all my archive pages display all the posts with the post-type = post.
My guess is that I have to run a simple have_posts loop instead of running WP_Query in order for my archive page to dynamic.
How I set up my archive page: I have a file called archive.php which I gave the template name of Archives and created a page for it on wp-admin and titled it Blog.
<?php
/**
* The template for displaying archive pages
* Template Name: Archives
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Twenty_Seventeen
* #since 1.0
* #version 1.0
*/
get_header(); ?>
<div class="d-flex flex-wrap archive-wrap">
<div class="content-area">
<main id="main" class="site-main d-flex justify-content-center flex-wrap" role="main">
<?php // Show the selected frontpage content.
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="archive-header col-10 d-flex justify-content-center align-items-center flex-column">
<?php
the_title( '<h1 class="archive-title">', '</h1>' );
the_content( '<h1 class="archive-title">', '</h1>');
?>
</div><!-- .page-header -->
<?php
get_template_part( 'template-parts/post/content', 'archive' );
endwhile;
the_posts_pagination( array(
'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',
'next_text' => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
) );
endif; ?>
<div class="col-10 col-md-3 offset-lg-1 sidebar-4-container">
<?php dynamic_sidebar( 'sidebar-4' ); ?>
</div>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
<?php get_footer();
Inside my template content archive file I have the following:
<?php
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post',
'post_status'=>'publish', 'posts_per_page'=>-1)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="col-12 article-container">
<div class="article-image-div" style="background-image: url('<?php echo get_the_post_thumbnail_url( $post_id, 'large' ); ?> ');"></div>
<h3><?php the_title(); ?></h3>
<h5>by <?php echo get_the_author(); ?></h5>
<?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 55 ) ); ?>
<hr align="left">
</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Try to use Query_posts or get_posts instead of
https://developer.wordpress.org/reference/functions/get_posts/
https://developer.wordpress.org/reference/functions/query_posts/

show category featured image in category page

I have featured images for categories and I'd like to show them on the category page, like here: http://aquadiva.it/en/category/beauty-en/
here's what my category.php looks like
<?php
/**
* The template for displaying Category Archive pages.
*
* #package Cryout Creations
* #subpackage Nirvana
* #since Nirvana 1.0
*/
get_header(); ?>
<section id="container" class="<?php echo nirvana_get_layout_class(); ?>">
<div id="content" role="main">
<?php cryout_before_content_hook(); ?>
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><div class="page-title-text"><?php
printf( __( '%s', 'nirvana' ), '<span>' . single_cat_title( '', false ) . '</span>' );
?></div></h1>
<?php
$category_description = category_description();
if ( ! empty( $category_description ) )
echo apply_filters( 'category_archive_meta', '<div class="category-archive-meta">' . $category_description . '</div>' );
?>
</header>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post();
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID));
?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to overload 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( 'content/content', get_post_format() );
?>
<?php endwhile; ?>
<?php if($nirvana_pagination=="Enable") nirvana_pagination(); else nirvana_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'nirvana' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'nirvana' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
<?php cryout_after_content_hook(); ?>
</div><!-- #content -->
<?php nirvana_get_sidebar(); ?>
</section><!-- #primary -->
what am I missing?
I have been able to do what I wanted with a new plugin (I deleted the "get the image" plugin and used "categories images"). everythin's working fine

Have I made a mistake with my WordPress pagination?

The pagination on a site I am working on is not working; I really can't work out why as it is the same as the other templates within the site. I am wondering if there is a problem with the loop, perhaps where I have specified what category I want it to pull.
This is the page I am having trouble with:
<div class="container content newspage">
<div class="two_third newsarticles">
<?php /* Start the Loop */ ?>
<?php $catquery = new WP_Query( 'cat=388' ); while($catquery->have_posts()) : $catquery->the_post(); ?>
<?php get_template_part( 'content-archive', get_post_format() ); ?>
<?php endwhile; ?>
<nav id="nav-below">
<div class="nav-previous">
<?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'anariel' ) ); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→ </span>', 'anariel' ) ); ?>
</div>
</nav>
<!-- end nav-below -->
</div>
<!-- end two_third -->
<div class="sidebar">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Default Sidebar') ) : ?>
<?php endif; ?>
</div>
</div>
<!-- end container -->
<?php get_footer(); ?>
The template that does work correctly is here:
<div class="container content newspage">
<div class="two_third newsarticles">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<nav id="nav-below">
<div class="nav-previous">
<?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'anariel' ) ); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→ </span>', 'anariel' ) ); ?>
</div>
</nav>
<!-- end nav-below -->
<?php endif; ?>
</div>
<!-- end two_third -->
<aside>
<div class="one_third lastcolumn newssidebar">
<?php get_sidebar(); ?>
</div>
</aside>
</div>
<!-- end container -->
<?php get_footer(); ?>
I have tried copying this over into the template that doesn't work without success.
When you're using a custom query, you have to add the page number to your arguments.
<?php /* Start the Loop */ ?>
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$catquery = new WP_Query( array('cat' => 388, 'paged' => $paged) ); while($catquery->have_posts()) : $catquery->the_post(); ?>
You should also pass the number of pages to get_next_posts_link
next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'anariel' ), $catquery->max_num_pages );

The_category loops permalink wordpress

I'm currently developing my own wordpress theme and so far so good. The problem is I'm using a permalink and a category. The category is inside the permalink. The following code is what I'm using (content.php):
<?php if ( has_tag ('half_half') ) : ?>
<div class="gridHalves">
<article class="project" >
<a class="projectLink" href="<?php the_permalink(); ?>">
<div class="projectwrapper">
<div class="projectOverlayInset">
<div class="projectOverlay">
<div class="projectInfo">
<h4 class="categorie"><?php the_category(', '); ?></h4>
<h2 class="onderwerp"><?php the_title(); ?></h2>
<p class="writer">Written by <strong><?php the_author(); ?></strong></p>
</div>
</div>
</div>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
</div>
</a>
</article>
</div>
<?php endif; ?>
The following is happening when I leave the_category in there:
If i leave the_category tag out everything is working fine. I've already checked the function in wp-includes of the_category but couldn't find anything about the permalink. So any suggestions? My loop is the following:
<?php get_header(); ?>
<main id="projectengrid" class="gridmenu-closed">
<section class="gridRightSmall">
<?php if ( have_posts() ) : ?>
<?php
// Start the loop.
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
// End the loop.
endwhile;
// Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'twentyfifteen' ),
'next_text' => __( 'Next page', 'twentyfifteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
) );
// If no content, include the "No posts found" template.
else :
get_template_part( 'content', 'none' );
endif;
?>
</section>
</main><!-- .site-main -->
<?php get_footer(); ?>
I left the post navigation in there because I want to use it later on.
Hope someone knows the solution to this! Seems like I'm having some nested loops in there!
Kind regards,
Wouter
If you need any additional info please let me know!

Categories