5

How I can display content for a single post? My code:

if (is_single()){
    get_header();
    get_sidebar('left-article');
    render_article();
    get_footer();
}

In render_article I call single_post_title() to get title of post. However I don't know how to get time and contents of the post as I can't find any function like single_post_content() or single_post_time().

11
  1. Create a file named single.php. This will automatically get all you single posts. For more information about WordPress' template hierarchy, read the Codex

  2. Inside single.php, run the default loop and get header.php, sidebar.php and footer.php

    <?php get_header(); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
     <?php the_title(); ?>
     <?php the_content(); ?>
     <?php echo get_the_date(); ?>
    
    <?php endwhile; ?>
    <?php endif; ?>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
    
2

you can use below function to get content of Wordpress post

     <?php echo get_the_content(); ?>

and you can use below function to get title

    <?php echo get_the_title(); ?>

to get date you can use this function

    <?php echo echo get_the_date(); ?>
1
function my_category_templates($single_template) {
    global $post;

    if ( in_category( 'Offers' )) {
        $single_template = dirname( __FILE__ ) . '/single-offer.php';
    }

    // Copy the above for your other categories

    return $single_template;
}

add_filter( "single_template", "my_category_templates" );
0

Use get_post() to get specofic post

get_post()

Example :

<?php $postData = get_post( $id, $output, $filter ); 

echo "<pre>";
print_r($postData);
?>

https://developer.wordpress.org/reference/functions/get_post/

https://www.tipsandtricks-hq.com/query-or-show-a-specific-post-in-wordpress-php-code-example-44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.