WordPress - 如何为上一页/下一页链接添加锚点 [英] WordPress - How to add anchor to previous/next page links

查看:255
本文介绍了WordPress - 如何为上一页/下一页链接添加锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的WordPress主题顶部有一个画廊,博客文章在它下面。每次我进入下一页时,它都会进入我不想要的顶部。我想添加一个名为#blog的锚点,位于图库下方,并将其添加到上一页/下一页链接。在代码中的哪个位置应该放置#blog以使其工作? 您应该可以通过两次调整来做到这一点。第一个将创建锚点,第二个将调整链接以使用锚点。



锚点可以被引用为< a>带有名称属性的标签或几乎任何具有 id 属性的标签。我会说大多数WordPress主题已经有一个可以在 #content 中使用的锚。浏览任何博客文章并查看源代码。在源代码中搜索 id =content并验证它是否存在。如果没有,请在您的实际文章内容中找到 id ,然后您可以使用该内容。如果找不到,则需要创建一个。



在您的WordPress主题文件中,查找名为 single的文件。 PHP 。这通常是管理单个帖子如何呈现的文件。这是您要编辑的文件,以添加锚点(如有必要)并调整链接。



如果您无法找到 id 来使用(不管它是'content'还是其他任何东西),你会想找到你的内容被调用输出的地方并添加一个 id
$ b

例如,我的文件的精简版本如下所示:

 < div id =primary> 
< div id =content>
<?php while(have_posts()):the_post(); ?>
< div class =entry-content>
<?php the_content(); //这是你的发布内容?>
< / div><! - .entry-content - >
<?php endwhile; //循环结束。 ?>
< / div>
< / div>

有多种输出链接的方法,所以它的确依赖于主题和版本的WordPress。在 single.php 文件中,查找与next_post和previous_post相关的函数。大多数用于创建链接的函数会自动为您编写整个链接标记(< a> ),因此无法拦截链接并将其更改。 / p>

您需要自己编写链接。以下代码显示如何获取信息并创建链接。它假定您将使用 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; ?>

这应该会创建您正在寻找的链接,自动跳过图片和页面发布内容。

I have a gallery at the top of my WordPress theme and the blog posts are below it. 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. Where in the code should I put #blog to make this work?

解决方案

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. I would say that a majority of WordPress themes already have an anchor you can use in #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. If it doesn't, find an id where your actual post content is and you can use that one. If you can't find one, you'll need to create one.

In your WordPress theme files, look for a file called 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.

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. In the single.php file, look for functions related to "next_post" and "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.

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.

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

这篇关于WordPress - 如何为上一页/下一页链接添加锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆