简体   繁体   English

当我在wordpress菜单中使用类别时,在index.php上设置当前页面?

[英]Set current page on index.php when I use categories in wordpress menu?

I use categories to the main navigation in wordpress. 我将类别用于wordpress中的主要导航。 To show my category "Home" (start page), I use the following code in index.php. 为了显示我的类别“主页”(开始页),我在index.php中使用以下代码。 "Home" is category 4. “家”是类别4。

<?php if ( have_posts() ) : ?> <?php query_posts("cat=4"); //set cat 4 to start page ?>
            <?php /* Start the Loop */ ?>

The problem is that when someone visits my site are "Home" not marked as current page. 问题是,当有人访问我的网站时,其“主页”未标记为当前页面。 But if a click on "Home" is it highlighted as current page. 但是,如果单击“主页”,它将突出显示为当前页面。

How do I set category home as the current page when someone visits my site? 当有人访问我的网站时,如何将“类别首页”设置为当前页面? Can i include the css in my php code above? 我可以在上面的PHP代码中包含CSS吗?

Have the following in in my css that works when you click on a link (category) in the main menu, but not when someone visits the web page without clicking in the menu. 当您单击主菜单中的链接(类别)时,我的css中包含以下内容,而当某人未单击菜单而访问网页时,则不起作用。

.main-navigation .current-menu-item > a,
.main-navigation .current-menu-ancestor > a,
.main-navigation .current_page_item > a,
.main-navigation .current_page_ancestor > a {
    color: #ffffff;
            background-color: #f68a1f;
}

My menu code looks like this (use wordpress menu) 我的菜单代码如下所示(使用wordpress菜单)

<nav id="site-navigation" class="main-navigation" role="navigation">
            <h3 class="menu-toggle"><?php _e( 'Meny', 'twentytwelve' ); ?></h3>
            <div class="skip-link assistive-text"><a href="#content" title="<?php esc_attr_e( 'Skip to content', 'twentytwelve' ); ?>"><?php _e( 'Skip to content', 'twentytwelve' ); ?></a></div>
            <?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) ); ?>
        </nav><!-- #site-navigation -->

Instead of using index.php, create a new "Page of Posts" page template as described here: http://codex.wordpress.org/Pages#A_Page_of_Posts 代替使用index.php,而是按此处所述创建新的“帖子页面”页面模板: http : //codex.wordpress.org/Pages#A_Page_of_Posts

In that template you use the query_posts("cat=4"); 在该模板中,您使用query_posts("cat=4"); code to only have it show the posts from that category. 代码,使其仅显示该类别的帖子。

Then you make a new page in your Wordpress dashboard, call it anything you like, and set its template (in the "Page Attributes" block on the edit page) to your newly created template. 然后,您可以在Wordpress仪表板中创建一个新页面,随意命名,并将其模板(在编辑页面的“页面属性”块中)设置为新创建的模板。

Last of all, in your Settings > Reading set "Front page displays" to "A static page", and select your newly created page as the front page. 最后,在设置>阅读中,将“首页显示”设置为“静态页面”,然后选择新创建的页面作为首页。

This way you use the built-in functionalities of Wordpress to create a custom page with specific types of contents in it, and still have it recognize that the page is active when you're on it. 通过这种方式,您可以使用Wordpress的内置功能来创建具有特定内容类型的自定义页面,并且仍然可以让您识别页面处于活动状态。

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

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