the_content() in single-{post-type}.php problem

I have Custom Post Type “pjesma” (song), and the only way I managed to display the content is like archive-pjesma.php.

It works fine:

<?php get_header();
global $post;
$args = array( 'post_type' => 'pjesma', 'posts_per_page' => 10 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
  setup_postdata( $post ); ?>
  <h2><a href="https://wordpress.stackexchange.com/questions/326484/<?php the_permalink(); ?>"><?php the_title(); ?></a> 
  </h2>
<?php //the_content(); ?>
<?php endforeach; 
wp_reset_postdata(); 

get_footer(); ?>

Then in single-pjesma.php I can only use the_title() so I use this echo $post->post_content;. Content of single-pjesma.php:

<?php get_header(); ?>
<?php
echo "<h1>"; the_title();
echo "</h1>";
echo $post->post_content;
?>
<?php get_footer(); ?>

Now, the problem is that I have installed plugin Chords and Lyrics so I can properly display chords above lyrics and that plugin does not affect the content. Content is displayed as it is stored in the database I guess.

enter image description here

Is there any other way to display lyrics and chords so they are affected by the plugin?

1 Answer
1

First of all, you shouldn’t use get_posts on your CPT archive – WP already creates a query and selects posts that should be display in there. So your archive-pjesma.php should look like this:

<?php get_header(); ?>

<?php while ( have_posts() ) : the_post(); ?>
    <h2><a href="https://wordpress.stackexchange.com/questions/326484/<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php // the_content(); ?>
<?php endwhile; ?>

<?php get_footer();

And you also should use simplified version of loop in your single-pjesma.php:

<?php get_header(); ?>

<?php if ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endif; ?>

<?php get_footer();

Leave a Comment