【问题标题】:Set current page on index.php when I use categories in wordpress menu?当我在 wordpress 菜单中使用类别时,在 index.php 上设置当前页面?
【发布时间】:2012-11-30 09:51:48
【问题描述】:

我在 wordpress 的主导航中使用类别。为了显示我的类别“主页”(起始页),我在 index.php 中使用了以下代码。 “家”是第 4 类。

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

问题是当有人访问我的网站时,“主页”没有标记为当前页面。但如果点击“主页”,它会突出显示为当前页面。

当有人访问我的网站时,如何将类别主页设置为当前页面?我可以在上面的 php 代码中包含 css 吗?

当您单击主菜单中的链接(类别)时,在我的 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;
}

我的菜单代码如下所示(使用 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 -->

【问题讨论】:

    标签: php css wordpress


    【解决方案1】:

    不使用 index.php,而是创建一个新的“帖子页面”页面模板,如下所述:http://codex.wordpress.org/Pages#A_Page_of_Posts

    在该模板中,您使用query_posts("cat=4"); 代码仅显示该类别的帖子。

    然后,您在 Wordpress 仪表板中创建一个新页面,将其命名为您喜欢的任何名称,并将其模板(在编辑页面上的“页面属性”块中)设置为您新创建的模板。

    最后,在您的设置>阅读中将“首页显示”设置为“静态页面”,然后选择您新创建的页面作为首页。

    这样,您可以使用 Wordpress 的内置功能来创建一个包含特定类型内容的自定义页面,并且当您在该页面上时,它仍然可以识别该页面是活动的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-21
      • 2016-01-06
      • 2013-10-16
      • 1970-01-01
      • 2011-04-19
      相关资源
      最近更新 更多