【问题标题】:Wordpress latest posts menu itemWordpress 最新帖子菜单项
【发布时间】:2013-01-16 15:16:03
【问题描述】:

我正在尝试让 Wordpress 为我提供一个菜单项以转到“最新帖子”。它们出现在首页,但是一旦我离开,我想要一个菜单​​项回到那里。这似乎很明显,但几个小时后,我能做的最好的事情就是创建一个自定义菜单,其中包含指向“未分类”的链接作为一种解决方法。一定会有更好的办法!这样,我会得到一个框,上面写着“归档在未分类类别下的帖子存档。”不想要!

【问题讨论】:

  • 当您在菜单屏幕侧面的页面框中单击“查看全部”时,是否有“主页:主页”选项?
  • 如果没有,您是否尝试过创建指向首页 URL 的自定义链接?

标签: wordpress menu posts


【解决方案1】:

在您的模板目录 (http://codex.wordpress.org/Pages#Page_Templates) 中使用自定义查询创建一个自定义页面(请查看 http://codex.wordpress.org/Class_Reference/WP_Queryhttp://codex.wordpress.org/Function_Reference/query_postshttp://codex.wordpress.org/Template_Tags/get_posts)。

在您的管理员中创建一个页面并选择您创建的模板。

在您的菜单中添加指向此页面的链接,您就完成了。

【讨论】:

  • 谢谢,但这听起来有点吓人,超出了我的能力。我敢肯定,我见过的大多数博客都会自动执行此操作 - 单击“主页”,您将返回主页,上面有最新的帖子。我在这里想念什么?由于某种原因,我无法在设置>阅读中设置首页和帖子页面。我感觉要放弃了。这么基本的东西,浪费了一整天!上面的解决方案是唯一的吗?我不确定我能不能理解它!
  • 您还可以编辑archive.php页面模板并更改h1标签内的内容(

    在此处更改内容

    )。您的类别页面将有一个不同的标题,并且不会找到“归档在未分类类别下的帖子存档。”。这样就可以了。
【解决方案2】:

也许这会有所帮助:http://www.viper007bond.com/2011/09/20/code-snippet-add-a-link-to-latest-post-to-wordpress-nav-menu/

这是一个过滤器,它将“搜索并替换”占位符锚点,例如“#latestpost1”,并使用最新帖子的实际网址,从而在呈现之前动态修改菜单。

我不确定这对 SEO 有什么影响,但这是一个聪明的解决方案。

【讨论】:

    【解决方案3】:

    为您的所有帖子命名。使用诸如“新闻”、“文章”或“博客”之类的通用内容。然后,使用您从类别下的菜单页面中选择的名称选择类别。将此类别链接添加到您的菜单。随意重命名链接 - 例如“博客”。而且,viola - 当人们点击该链接时,您的所有帖子都会出现。

    【讨论】:

      【解决方案4】:
      【解决方案5】:

      简单的解决方案:

      我拿了这个人的代码:http://www.viper007bond.com/2011/09/20/code-snippet-add-a-link-to-latest-post-to-wordpress-nav-menu/

      基本上他写的是菜单项链接到最新的帖子,而不是帖子(复数),所以我只是修改了它并且它正在工作:

      <?php
      if ( ! is_admin() ) {
          // Hook in early to modify the menu
          // This is before the CSS "selected" classes are calculated
          add_filter( 'wp_get_nav_menu_items', 'replace_placeholder_nav_menu_item_with_latest_post', 10, 3 );
      }
      
      // Replaces a custom URL placeholder with the URL to the latest post
      function replace_placeholder_nav_menu_item_with_latest_post( $items, $menu, $args ) {
      
          // Loop through the menu items looking for placeholder(s)
          foreach ( $items as $item ) {
      
           // Is this the placeholder we're looking for?
              if (!strpos(($item->url), 'latestpost'))
                  continue;
            //  if ( 'latestpost' != $item->url )
              //    continue;
      
              // Get the latest post
              $latestpost = get_posts( array(
                  'numberposts' => 1,
              ) );
      
              if ( empty( $latestpost ) )
                  continue;
      
              // Replace the placeholder with the real URL
              $new_link = $item->url;
              $new_link = substr($new_link, 0, strlen($new_link) - 12);
              $item->url = $new_link;
          }
      
          // Return the modified (or maybe unmodified) menu items array
          return $items;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-28
        相关资源
        最近更新 更多