简体   繁体   English

正确获取页面名称(使用 Barba.JS 和 Wordpress 的 data-barba-namespace)

[英]Get page name correctly (data-barba-namespace with Barba.JS and Wordpress)

Right now, i'm using Sage 9 to create a theme.现在,我正在使用 Sage 9 创建一个主题。 I've installed Barba and it works fine except for one thing.我已经安装了 Barba,它工作正常,除了一件事。 I'm trying to get the page name correctly in my data-barba-namespace.我正在尝试在我的 data-barba-namespace 中正确获取页面名称。 For now, i was using the following:现在,我正在使用以下内容:

data-barba-namespace="{{ get_post_field('post_name', $post_id) }}"

But for one page called "Work" (that shows works from a custom post type i've created using CPT UI), i've created a template (template-work.blade.php) and in it i have different queries for different works in different categories.但是对于一个名为“Work”的页面(显示来自我使用 CPT UI 创建的自定义帖子类型的作品),我创建了一个模板(template-work.blade.php),并且在其中我有不同的查询在不同的类别中工作。 My main issue is that my previous code on this page returns the name of one work/project in my data-barba-namespace and not the name of the page... I can't figure out why我的主要问题是我之前在此页面上的代码在我的 data-barba-namespace 中返回了一个工作/项目的名称,而不是页面的名称......我不知道为什么

So i've tried to use the following code:所以我尝试使用以下代码:

<?php
      $pagename = get_query_var('pagename');
      if ( !$pagename && $id > 0 ) {
      $post = $wp_query->get_queried_object();
      $pagename = $post->post_name;
      }
?> 

And so所以

data-barba-namespace="{{ $pagename }}"

But the issue with this is since i'm using a static front page (created a front-page.blade.php file), the data-barba-namespace is empty for the homepage, even though it works for the other pages.但问题是因为我使用的是 static 首页(创建了一个 front-page.blade.php 文件),因此主页的 data-barba-namespace 是空的,即使它适用于其他页面。 And this solution doesn't get the work pages names correctly as get_post_field('post_name', $post_id) does.并且此解决方案无法像 get_post_field('post_name', $post_id) 那样正确获取工作页面名称。

My last issue is with the custom post type Work i've created.我的最后一个问题是我创建的自定义帖子类型工作。 When using my first solution, then i have the issue mentioned with my work page (putting the name of a work instead of "work" in the data-barba-namespace) and another one that's obvious: in the data-barba-namespace, i have the name of the project.当使用我的第一个解决方案时,我的工作页面中提到了一个问题(在 data-barba-namespace 中放置作品的名称而不是“work”)和另一个显而易见的问题:在 data-barba-namespace 中,我有项目的名称。 Which should be great except how i have to create a barba transition for all of the projects.. which is bad even for a beginner like me.除了我必须为所有项目创建一个 barba 过渡之外,这应该很棒。即使对像我这样的初学者来说也很糟糕。 i'd rather have a "work-page" or something like this in my data-barba-namespace to create only one transition for all my work pages, But i have no clue how to do this.我宁愿在我的 data-barba-namespace 中有一个“工作页面”或类似的东西来为我的所有工作页面创建一个过渡,但我不知道如何做到这一点。 i couldn't find nothing..我什么都找不到。。

Any help would be much much appreciated!任何帮助将不胜感激!

EDIT: EDIT: Using the second solution, i figured out i could just add a conditional statement to check if it's the homepage.编辑:编辑:使用第二种解决方案,我发现我可以添加一个条件语句来检查它是否是主页。 For some reasons, it didn't work the first time.. Here is what i'm using:由于某些原因,它第一次没有用..这是我正在使用的:

@php
      if( is_home() || is_front_page() ){
        $pagename = "home";
      } else {
        $pagename = get_query_var('pagename');
        if ( !$pagename && $id > 0 ) {
          // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
          $post = $wp_query->get_queried_object();

          $pagename = $post->post_name;
        }
      }
@endphp

But i still have trouble to find a solution for the work pages, so to give them a unique namespace name但是我仍然很难找到工作页面的解决方案,所以给他们一个唯一的命名空间名称

Problem solved: I've used the following :问题已解决:我使用了以下内容:

@php
    function is_post_type($type){
      global $wp_query;
      if($type == get_post_type($wp_query->post->ID))
          return true;
      return false;
    }

      if( is_home() || is_front_page() ){
        $pagename = "home";
      } elseif (is_single() && is_post_type('work')){
        $pagename = "work-item";
      } else {
        $pagename = get_query_var('pagename');
        if ( !$pagename && $id > 0 ) {
          // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
          $post = $wp_query->get_queried_object();

          $pagename = $post->post_name;
        }
      }
@endphp

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

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