If you followed my previous tutorial, you will now have a theme (or subtheme) on your site that contains links to the top-level pages in the header of your site.
I created a 26 child theme and this is what my links look like now:
In this tutorial, I'm going to show you how to add some CSS to your theme to make those links a little better. Let's start by removing bullets and adding floats.
Open the theme's style sheet. If you created a child theme it will be empty, but if you are using your own theme I recommend adding this style in the section of your stylesheet that holds the header style.
Code review of output page link (if there is a page to be linked):
<ul class="top-level-page-links"> <?php // using a foreach loop, output the title and permalink for each page foreach ( $pages as $page ) { ?> <li class="page-link"> <a href="<?php echo get_page_link( $page->ID ); ?>"> <?php echo $page->post_title; ?> </a> </li> <? } ?> </ul>
This means we are targeting a ul
element with class top-level-page-links
and within it a li
element where # The ##page-link class is followed by the
a element (that is, the link).
ul.top-level-page-links { list-style: none; }
margin-left statement:
ul.top-level-page-links { list-style-type: none; margin-left: 0; }
Next let these links float next to each other. Add this to your stylesheet:
.page-link { float: left; }
Next, let’s move on to making the link look more like a button.
Add margins, padding and background
Add this to your stylesheet:
.page-link a { margin-right: 10px; padding: 0.5em 10px; background-color: #454545; }
When you refresh the screen, your buttons will look more like buttons:
They look much better, but require a little skill. Let's edit the color of the text and background so that when someone hovers over the button, it changes color.
Add hover effect
Add two more declaration blocks to the stylesheet, making sure to add them after the declaration block of the link you just added:
.page-link a:link, .page-link a:visited { color: #fff; text-decoration: none; } .page-link a:hover, .page-link a:active { background-color: #dddddd; color: #454545; text-decoration: none; }
Let’s see how it looks on the page:
When I hover over the link:
much better!
Summary
This gives you a nice, prominent way to get your visitors directly to these pages, which is useful if you want to ensure that a large number of visitors can access the top pages.
The above is the detailed content of Beautify the website's top page link button: use the get_pages() method. For more information, please follow other related articles on the PHP Chinese website!