Wordpress get template part with if statement shows both templates - wordpress

I'm using ACF and I would like to display posts in a different way according to the category. Here is my single :
<?php get_header(); ?>
<?php if(!function_exists('get_field')) return; ?>
<main class="main_flex">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post();
if ( in_category('dossiers') ) { get_template_part( 'single_dossier' );
} else { get_template_part( 'template-parts/content', 'single' );
} ?>
<article>
<!-- content here -->
</article>
<?php endwhile; ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
And in a file I named single_dossier.php, I wrote this :
<?php if(!function_exists('get_field')) return; ?>
<article>
<!-- different content here -->
</article>
When I go to an article with the category "dossiers", it loads both the contents. Anyone could tell me what I did wrong and how to correct it?
Thanks!

Maybe you have 2 posts with same id/slug (Depending on your permalink structure).
For check this use this code:
<?php get_header(); ?>
<?php if(!function_exists('get_field')) return; ?>
<main class="main_flex">
<?php if (have_posts()) : ?>
<?php $x = 0; ?>
<?php while (have_posts()) : the_post();
$x++;
echo '{{' . $x . ':' . $post->ID . '}}';
if ( in_category('dossiers') ) { get_template_part( 'single_dossier' );
} else { get_template_part( 'template-parts/content', 'single' );
} ?>
<article>
<!-- content here -->
</article>
<?php endwhile; ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
And let see if you get in output {{1:someID}} & {{2:someAnotherID}}.
Or use the following code:
<?php get_header(); ?>
<?php if(!function_exists('get_field')) return; ?>
<main class="main_flex">
<?php if (have_posts()) : ?>
<?php
the_post();
if ( in_category('dossiers') ) { get_template_part( 'single_dossier' );
} else { get_template_part( 'template-parts/content', 'single' );
} ?>
<article>
<!-- content here -->
</article>
<?php endif; ?>
</main>
<?php get_footer(); ?>

Related

WordPress single post template layout

I'm not too famiiar with WordPress but, I've created the following page to display custom post types single-opportunities.php:
<?php
/**
* The Template for displaying single opportunities posts.
**/
get_header();
global $accesspresslite_options, $post;
$accesspresslite_settings = get_option( 'accesspresslite_options', $accesspresslite_options );
$post_class = get_post_meta( $post -> ID, 'accesspresslite_sidebar_layout', true );
?>
<div class="ak-container">
<?php
if ($post_class=='both-sidebar') { ?>
<div id="primary-wrap" class="clearfix">
<?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( 'content', 'single' ); ?>
<?php // accesspresslite_post_nav(); ?>
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() ) :
comments_template();
endif;
?>
<?php endwhile; // end of the loop.
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_sidebar('left');
if ($post_class=='both-sidebar') { ?>
</div>
<?php }
?>
</div>
<?php get_footer(); ?>
I've added a custom post type and this page is displaying the information OK, but the layout has a sidebar on the right. I'm not sure why it is displaying the sidebar on the right? Can this default option be changed.
Ideally I'd like to edit the template above so the layout has a left sidebar, that I can then add a custom menu into.
Looks like youre calling the sidebar after the main body of the post/page. Call it before to have it show up on the left side.
/**
* The Template for displaying single opporunities posts.
*
*/
get_header();
global $accesspresslite_options, $post;
$accesspresslite_settings = get_option( 'accesspresslite_options',
$accesspresslite_options );
$post_class = get_post_meta( $post -> ID, 'accesspresslite_sidebar_layout', true );
?>
<div class="ak-container">
<?php
if ($post_class=='both-sidebar') { ?>
<div id="primary-wrap" class="clearfix">
<?php }
get_sidebar('left');
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'single' ); ?>
<?php // accesspresslite_post_nav(); ?>
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() ) :
comments_template();
endif;
?>
<?php endwhile; // end of the loop.
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
if ($post_class=='both-sidebar') { ?>
</div>
<?php }
?>

WordPress search page shows White Screen of Death

I have created below search page on this website. For some reason the search page returns a blank page. I have tried editing the code but still I get a blank page.
get_header(); ?>
<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
</header><!-- .page-header -->
<?php shape_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<?php shape_content_nav( 'nav-below' ); ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'search' ); ?>
<?php endif; ?>
</div><!-- #content .site-content -->
</section><!-- #primary .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Read more tag not working on page - WordPress

Not sure what is wrong here as have found the same fix all over the place for my issue. Read more tags are not showing on my page that pulls in the latest posts. I have researched that simply adding this code will fix that.
<?php
global $more;
$more = 0;
?>
//The code must be inserted ahead of the call to the content
<?php the_content('Continue Reading'); ?>
However I can't seem to get it to work. Any other ideas or am I just using the code wrong, quote possible! Here is an idea of my page, any help in identifying where to insert the code would be much appreciated. Thanks very much guys!
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="entry-page-image">
<?php the_post_thumbnail(); ?>
</div><!-- .entry-page-image -->
<?php endif; ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar( 'front' ); ?>
<?php get_footer(); ?>
try this.code.check wether you have given insert more tag in admin for pages and post
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="entry-page-image">
<?php the_post_thumbnail(); ?>
</div><!-- .entry-page-image -->
<?php endif; ?>
<?php
global $more;
$more = 0;
get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar( 'front' ); ?>
<?php get_footer(); ?>

the content function is ignoring more tag

I'm doing "page of post" in twenty eleven theme. Problem is that the_content function ignores more tag and show the whole post. It work just fine in index page.
My code:
/*
Template Name: Page Of Posts
*/
?>
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
$args= array(
'category_name' => '');
query_posts($args);
if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentyeleven_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', 'twentyeleven' ); ?></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.', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
Please help!
Can you list a file you are requesting with get_template_part('content', get_post_format() ) it should be in the theme folder named like content-gallery.php(or any other post_format).
Anyway, you can try the good old:
<?php global $more; $more = 0; ?>
Place it before the call for the content.

How can I get all the wordpress posts separately from each other?

I'm using the twentyeleven default template and I use
<?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 else : ?>
<?php endif; ?>
To get the Posts.
What I want is to have this:
<div id="wrapper">
<div id="first">
//first post
<?php get_template_part( 'content', get_post_format() ); ?>
</div>
<div id="second">
//second post
<?php get_template_part( 'content', get_post_format() ); ?>
</div>
<div id="third">
//third post
<?php get_template_part( 'content', get_post_format() ); ?>
</div>
</div><!-- end wrapper -->
Can somebody help me with this?
Could you use post-1 instead of first, post-2 instead of second, etc.? If so, dead easy. Create a variable that increments each time through the loop and tracks the number of posts.
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php $count = 0; /* <- create the counter variable */?>
<?php while ( have_posts() ) : the_post(); ?>
<div id="post-<?php echo $count; /* <- prints the variable */ $count++; /* <- increments the counter */ ?>">
<?php get_template_part( 'content', get_post_format() ); ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
From your question I'm not sure if you realize that everything between the while and endwhile bits gets inserted into the HTML for every post. That's why it's called "the loop." It loops over that section until every post on the page has been displayed.
You may also want to use the built-in post class function to add relevant classes to every post. Here is the above with the post class function added:
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php $count = 0; /* <- create the counter variable */?>
<?php while ( have_posts() ) : the_post(); ?>
<div id="post-<?php echo $count; $count++;?>" <?php post_class(); ?>>
<?php get_template_part( 'content', get_post_format() ); ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>

Resources