$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>theme development - the_content() in single-{post-type}.php problem|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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="<?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.

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

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="<?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.

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

Share Improve this question edited Jan 23, 2019 at 22:26 Kashif Rafique 2351 gold badge3 silver badges12 bronze badges asked Jan 23, 2019 at 21:23 Zoran KoprekZoran Koprek 235 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

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="<?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();
Post a comment

comment list (0)

  1. No comments so far