admin管理员组

文章数量:1122832

I want the Title of page/post (in content area) to show up topmost when reading the post/page.

If you use #anchor link you can only link to the topmost textblock, but then the Title of the post becomes hidden above.

How to link it to the Title?

I want the Title of page/post (in content area) to show up topmost when reading the post/page.

If you use #anchor link you can only link to the topmost textblock, but then the Title of the post becomes hidden above.

How to link it to the Title?

Share Improve this question asked Jun 14, 2024 at 14:05 ThomasMoxThomasMox 1 2
  • It really depends on the theme you're using. If you're using a custom theme, you can add its header and post template code here for help. – WebElaine Commented Jun 14, 2024 at 14:24
  • Thank you for answer! Not sure to get the temlate codes. I´m using "twenty sixteen" as just an ordinary blog. URL: blog.mox.se Perhaps I could solve my problem attaching some code to #anchor at the topmost block, making the page load 50-60 px up, the title then showing up? – ThomasMox Commented Jun 14, 2024 at 18:44
Add a comment  | 

1 Answer 1

Reset to default 1

To ensure the title of the page or post is displayed at the top when linked with an anchor, you need to adjust the HTML structure of your WordPress theme slightly. The title should be placed within an anchor element that can be targeted.

Step-by-Step Guide to Link Directly to the Title of a Post/Page

1. Modify the Theme Template

  1. Access Theme Files:

    • Connect to your WordPress installation via FTP or use the theme editor in the WordPress admin dashboard.
    • Navigate to the theme directory (typically wp-content/themes/your-theme).
  2. Edit the Single Post/Page Template:

    • Locate the template file for single posts (single.php) or pages (page.php). You may also want to edit header.php if your theme uses a header file for the title.
  3. Add an ID to the Title:

    • Open the single.php or page.php file in a text editor.

    • Find the code that displays the title, which usually looks something like this:

      <h1 class="entry-title"><?php the_title(); ?></h1>
      
    • Add an id attribute to the title element:

      <h1 class="entry-title" id="post-title"><?php the_title(); ?></h1>
      
  4. Save and Upload the Modified Template:

    • Save the changes to the template file.
    • If you edited the file locally, upload it back to the theme directory on your server.

2. Use the Anchor Link

  • When you want to link directly to the title of a specific post or page, use the anchor #post-title in your URL.
  • For example, if the URL of your post is https://example.com/my-post, you can link directly to the title with https://example.com/my-post#post-title.

Example:

If your post URL is:

https://example.com/my-post

The link to the title will be:

https://example.com/my-post#post-title

本文标签: I want the Title of pagepost to show up topmost when reading the postpage