简体   繁体   English

如何链接到Wordpress另一页上的特定部分

[英]How do I link to a specific section on another page on Wordpress

I'm new to Wordpress and PHP and this might be a dumb question, but I'm trying to link one of my menu items to one of the sections on my index page. 我是Wordpress和PHP的新手,这可能是一个愚蠢的问题,但是我试图将我的菜单项之一链接到索引页面上的某一节。

I know that if I just wanted to link it to the index I should use this: 我知道,如果我只想将其链接到索引,则应使用以下代码:

<?php echo home_url(); ?> 

But I want the link to send the user to the "About" section. 但我希望该链接将用户发送到“关于”部分。 Basically, I need to know how to do this: 基本上,我需要知道如何执行此操作:

index.php#about 

but with PHP. 但使用PHP。

Thank you! 谢谢!

You're on the right track. 您走在正确的轨道上。

The ideal way to do this would be to add a <a name="about></a> tag to the appropriate section of your template. This is called an HTML anchor and is how those #tags know where to point to. 理想的方法是在模板的适当部分添加一个<a name="about></a>标签,这称为HTML锚,这就是这些#tag知道指向何处的方式。

Given that this is Wordpress, you could probably also get away with just appending that to the title of the appropriate section. 鉴于这是Wordpress,您可能还可以通过将其附加到适当部分的标题来摆脱困境。 So wherever you specified 'call this section "About"', you could probably redo it as 'call this section " <a name="about">About</a> "' and then you'll be able to link to it using anchors like in your example-- <a href="/index.php#about">About</a> 因此,无论您在何处指定“称此部分为”关于””,都可以将其重命名为“称此部分为” <a name="about">About</a> “”,然后就可以链接到该部分。使用<a href="/index.php#about">About</a>锚点- <a href="/index.php#about">About</a>

If you are new to php, maybe you should use wordpress's editor ? 如果您不熟悉php,也许应该使用wordpress的编辑器?

In your page (in the admin page), you can put any html you want. 在您的页面(在管理页面中)中,您可以放置​​任何想要的html。

In the editor, you can add custom links (with anchors or not) and you can put a div tag in the "html" tab. 在编辑器中,您可以添加自定义链接(带或不带锚),并且可以在“ html”标签中放置div标签。

So if you put your link at the top of your page and put your section in a div id="myanchor", it should do it ! 因此,如果您将链接放在页面顶部,并将您的部分放在div id =“ myanchor”中,则应该这样做!

You shouldn't do this with HTML or PHP but rather with JS. 您不应该使用HTML或PHP来执行此操作,而应该使用JS。 Specifically for long pages and require in-page navigation, I really like the scrollTo jQuery plugin. 我特别喜欢scrollTo jQuery插件,专门用于长页面并需要页面内导航。

In practice, you'll have some HTML containers that look something like this: 实际上,您将拥有一些类似于以下内容的HTML容器:

<!-- Your menu -->
<ul>
  <li id="about-button"></li>
  <li id="product-button"></li>
  <li id="something-button"></li>
  <li id="else-button"></li>
</ul>    

<!--Your page sections-->
<main class="my-page">
  <section id="about"></section>
  <section id="product"></section>
  <section id="something"></section>
  <section id="else"></section>
</main>

Once you've included jQuery and the scrollTo plugin, you'll have some JS that looks like this: 包含jQuery和scrollTo插件后,您将获得一些类似于以下内容的JS:

$('#about-button').click(function() {
  $.scrollTo($('#about'), {
    duration: 800,
    offset: -50
  });
  return false;
});

The JS is saying that once you click on the #about-button , take 800 milliseconds and animate the page down to -50px before the position of the #about HTML element. JS表示,一旦您单击#about-button ,请花费800毫秒的时间,然后将页面动画化为#about HTML元素位置之前的#about You could just setup a series of click functions for each button and you'd have a slick in-page nav system. 您只需为每个按钮设置一系列单击功能,就可以拥有一个漂亮的页面内导航系统。

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

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