I have been trying to change my code from my index.php to a custom page template instead as this is better practice I have been told.
The original code for the index.php does not work anymore so I have tried to this code, and it retrieves the posts correctly - however it appears to be retrieving the posts in 'Singular' format and displays all of the content rather than an excerpt.
Here is the current homepage php code:
<h3>Read Our Latest Cleaning Posts</h3>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<div id="blog-homepage">
<?php
$query_options = array('posts_per_page' => 3);
$the_query = new WP_Query( $query_options );
while ($the_query -> have_posts()) : $the_query -> the_post();
get_template_part( 'template-parts/content', get_post_type() );
endwhile;
the_posts_navigation();
wp_reset_postdata(); ?>
</div>
</main>
</div>
And here is the template-parts/content file:
<div class="article-container">
<?php scores_post_thumbnail(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post-text">
<header class="entry-header">
<?php
if ( is_singular() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
endif;
if ( 'post' === get_post_type() ) :
?>
<div class="entry-meta">
<?php
scores_posted_on();
scores_posted_by();
?>
</div><!-- .entry-meta -->
<?php endif; ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php
if ( is_singular() ) :
the_content( sprintf(
wp_kses(
/* translators: %s: Name of current post. Only visible to screen readers */
__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'scores' ),
array(
'span' => array(
'class' => array(),
),
)
),
get_the_title()
) );
else :
the_excerpt();
endif;
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'scores' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<footer class="entry-footer">
<?php
if ( is_singular() ) :
scores_entry_footer();
else: ?>
<div>
<a href="<?php the_permalink(); ?>">Read More</a>
</div>
<?php endif ?>
</footer><!-- .entry-footer -->
</div><!-- end of the text content container (excl. img tmb) -->
</article></div><!-- #post-<?php the_ID(); ?> -->
From what I gather the 'is_singular' is resulting in true, therefore causing it to display all the content. What am I doing wrong here?
--> Bonus: the code that did work on the index.php page but not on the custom template:
<h3>Read Our Latest Cleaning Posts</h3>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<div id="blog-homepage">
<?php
if ( have_posts() ) :
if ( is_home() && ! is_front_page() ) :
?>
<header>
<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
</header>
<?php
endif;
/* Start the Loop */
while ( have_posts() ) :
the_post();
/*
* Include the Post-Type-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 Type name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_type() );
endwhile;
the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</div>
</main><!-- #main -->
</div><!-- #primary -->
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744851231a4597138.html
评论列表(0条)