WordPress.org

Ready to get started?Download WordPress

Forums

Single post! (11 posts)

  1. sly_g
    Member
    Posted 1 year ago #

    Complex logic of WP continues to elude me.

    I need to display one single post (title and content) on a page. File "single.php" is supposedly dedicated for this. So, when user goes to page "site.com/p=85", he should see the post with id of 85. And that's all! Only ONE post.

    If I'm insterting "the loop" in this page, ALL posts are listed.

    If I'm not inserting "the loop", I cannot access "the_title()", "the_content()", and other post data.

    How I am to display single post in single.php ? ALL examples of single.php on the Internet for some unknown reason include "the loop", and all of them display ALL posts! Why ??

    How I am to get current post and it's data (title, content) that should be displayed according to the current URI "/p=85" ???

  2. kjodle
    Member
    Posted 1 year ago #

    Which theme are you using? Please post a link to your site.

  3. kpry44
    Member
    Posted 1 year ago #

    Just use

    the_post();

    You don't need to loop as you are already on single.php.

  4. sly_g
    Member
    Posted 1 year ago #

    I'm building my own theme.

  5. alchymyth
    Forum Moderator
    Posted 1 year ago #

    the link should be:

    /?p=85

    a wrong link might just get to use the index.php template if your theme does not have a 404 template (?)

    does your theme have a single.php?

  6. sly_g
    Member
    Posted 1 year ago #

    OK, I suppose I shoud've gotten into details from the start.

    So.
    I'm making my own Theme. I need a page, that would display only one Post.
    I found that file called "single.php" is used for it.

    So I've made it, here it is:

    <?php get_header(); ?>
    
    <?php get_sidebar(); ?>
    
      <?php the_post(); ?>
    
      <p><?php the_date(); ?>
    
      <p><?php the_category(', ') ?>
    
      <div class="thumb"><?php the_post_thumbnail(); ?></div>
    
      <p><?php the_content(); ?>
    
      <?php previous_post_link(); ?>
      <?php next_post_link(); ?>
    
      <?php the_tags(); ?>
    
    <?php get_footer(); ?>

    Site url is http://golovanov.net/biosea/

    I've made two test posts.

    Post links are:
    http://golovanov.net/biosea/?p=85
    http://golovanov.net/biosea/?p=1

    But with this "single.php" file I always get only the post with id 85 (the latest of them), regardless of the address - either http://golovanov.net/biosea/?p=85 or http://golovanov.net/biosea/?p=1 displaying only one (latest) post.

  7. sly_g
    Member
    Posted 1 year ago #

    Interestingly enough, I discovered it just now - page title in the <title> html tag displayed properly, depending on the post id in the URI.
    It's done by <?php wp_title(); ?> function in the "header.php".

    But everything else is the same on this two pages, while it's two different posts.

  8. alchymyth
    Forum Moderator
    Posted 1 year ago #

    is there anything in header.php or sidebar.php that could possibly mess with the original query?

  9. sly_g
    Member
    Posted 1 year ago #

    Sidebar is (excluding texts):

    <!-- Sidebar -->
    <tr>
      <td id="sidebar" valign="top">
        <?php
          global $post;
          switch ( get_post( $post )->post_name ) {
            default;
              echo "<ul id='contents'>\n";
              wp_list_pages('title_li=&sort_column=menu_order&depth=2');
              echo "</ul>\n";
            break;
          }
        ?>
    
        <hr>
    
        <?php
          query_posts($query_string . '&post_type=post');
    
          while ( have_posts() ) : the_post();
            the_date();
            echo ' <a href="'.get_permalink().'">';
            the_title();
            echo '</a><br>';
          endwhile;
        ?>    
    
      </td>
      <td class="content" valign="top">
        <div class="content">
    <!-- /Sidebar -->
  10. alchymyth
    Forum Moderator
    Posted 1 year ago #

    after:

    endwhile;

    add:

    wp_reset_query();

  11. sly_g
    Member
    Posted 1 year ago #

    Well, it almost helped, thanks!

    One small problem left: date of first post doesn't show.
    The one that should be where <?php the_date(); ?> is.

    UPD:
    I found out that this could happen in a loop and when dates are the same. However, it's not a loop and dates are different.

    But with the_time('d.m.Y') date is displayed correctly. Strange.

Topic Closed

This topic has been closed to new replies.

About this Topic