简体   繁体   English

WordPress - 如何将锚点添加到上一页/下一页链接

[英]WordPress - How to add anchor to previous/next page links

I have a gallery at the top of my WordPress theme and the blog posts are below it.我的 WordPress 主题顶部有一个画廊,博客文章在它下面。 Each time I go to the next page it goes to the top which I don't want.每次我转到下一页时,它都会转到我不想要的顶部。 I would like to add an anchor called #blog just below the gallery and add it to previous/next page links.我想在图库下方添加一个名为 #blog 的锚点,并将其添加到上一页/下一页链接。 Where in the code should I put #blog to make this work?我应该将#blog 放在代码中的哪个位置来完成这项工作?

You should be able to do this with two adjustments.您应该可以通过两次调整来做到这一点。 The first will be creating the anchor and the second will be adjusting the links to use the anchor.第一个将创建锚点,第二个将调整链接以使用锚点。

Anchors can be referenced as <a> tags with a name attribute or almost any tag with an id attribute.锚点可以被引用为带有name属性的<a>标签或几乎任何带有id属性的标签。 I would say that a majority of WordPress themes already have an anchor you can use in #content .我想说的是,大多数 WordPress 主题已经有了一个可以在#content使用的锚点。 Navigate to any of your blog posts and view the source code.导航到您的任何博客文章并查看源代码。 Search for id="content" in the source code and verify that it exists.在源代码中搜索id="content"并验证它是否存在。 If it doesn't, find an id where your actual post content is and you can use that one.如果没有,请在您的实际帖子内容所在的位置找到一个id ,然后您可以使用该id If you can't find one, you'll need to create one.如果找不到,则需要创建一个。

In your WordPress theme files, look for a file called single.php .在您的 WordPress 主题文件中,查找名为single.php的文件。 This is typically the file that governs how single posts are rendered.这通常是控制如何呈现单个帖子的文件。 This is the file that you'll edit to add the anchor (if necessary) and adjust the links.这是您将编辑以添加锚点(如有必要)和调整链接的文件。

If you were not able to find an id to use (be it 'content' or anything else), you'll want to find where your content is being called for output and add an id to whatever HTML tag is wrapped around it.如果您无法找到要使用的id (无论是“内容”还是其他任何内容),您将需要找到您的内容被调用以进行输出的位置,并将一个id添加到其周围的任何 HTML 标记中。

For example, a stripped down version of my file looks like this:例如,我的文件的精简版本如下所示:

<div id="primary">
  <div id="content">
    <?php while ( have_posts() ) : the_post(); ?>
      <div class="entry-content">
       <?php the_content(); // this is your post content ?>
      </div><!-- .entry-content -->
    <?php endwhile; // end of the loop. ?>
  </div>
</div>

There are a variety of methods for outputting the link, so it really depends on the theme and the version of WordPress.有多种输出链接的方法,所以这实际上取决于WordPress的主题和版本。 In the single.php file, look for functions related to "next_post" and "previous_post".single.php文件中,查找与“next_post”和“previous_post”相关的函数。 Most of the functions used for creating the links automatically write the entire link tag ( <a> ) for you, so there's no way to intercept the link and change it.大多数用于创建链接的函数会自动为您编写整个链接标记 ( <a> ),因此无法拦截链接并更改它。

You'll need to write the links yourself.您需要自己编写链接。 The below code shows how to get the information and create the links.下面的代码显示了如何获取信息和创建链接。 It assumes you'll be using id="content" as your anchor reference.它假设您将使用id="content"作为您的锚点参考。

  <?php
  $prev_post = get_previous_post();
  if (!empty( $prev_post )): ?>
    <a href="<?php echo get_permalink( $prev_post->ID ); ?>#content"><?php echo $prev_post->post_title; ?></a>
  <?php endif; ?>

  <?php
  $next_post = get_next_post();
  if (!empty( $next_post )): ?>
    <a href="<?php echo get_permalink( $next_post->ID ); ?>#content"><?php echo $next_post->post_title; ?></a>
  <?php endif; ?>

This should create the links you're looking for that automatically jump the page past the images and to the post content.这应该会创建您正在寻找的链接,这些链接会自动将页面跳过图像并跳转到帖子内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM