Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detecting within a template if the "home page"? #116

Open
sweetappleuk opened this issue Apr 13, 2022 · 1 comment
Open

Detecting within a template if the "home page"? #116

sweetappleuk opened this issue Apr 13, 2022 · 1 comment

Comments

@sweetappleuk
Copy link

sweetappleuk commented Apr 13, 2022

In prior versions of the module you could detect if the template rendering was the Homepage within a template with

if( $this->getRequest()->getFullActionName() == 'wordpress_homepage_view')

This seems to have stopped working with one of the more recent refactors, as this now returns 'wordpress_postType_view' which on the face of it seems much more ambiguous. How would you suggest rendering different content on the home page/ without doing something awful like hardcoding the bog home url path?

Modifying wordpress_homepage_view.xml and adding something like this to render an alternate template?

<referenceBlock name="wp.post.list">
    <action method="setTemplate">
        <argument name="template" xsi:type="string">FishPig_WordPress::list/wrapper-home.phtml</argument>
    </action>
</referenceBlock>
@bentideswell
Copy link
Owner

The layout handles changed in version 3 to more closely reflect WordPress. WordPress doesn't have a set homepage and sometimes it's the default post list and sometimes it's a static page. As a result the controller path (which generates the main layout handle) has changed.

There are many other layout handles applied though, so using one of those will allow you to apply different layout updates depending on if it's the archive for post_type=post (which is what used to be the homepage).

The best way to do this is to look at FishPig\WordPress\Controller\Action::addLayoutHandles. You can add the following to the top of that method to see all applied layout handles for the request:

echo '<pre>'; print_r($handles);exit;

Doing this shows:

wordpress_post_type_post_view

This is in the format: wordpress_post_type_{{POST_TYPE}}_view

So as you are looking for the post_type=post, this becomes:

wordpress_post_type_post_view

The wordpress_homepage_view layout handle is still set for this postType so you can use your approach by changing the template via the layout XML; that should work.

If you want to detect this in PHP, this approach won't help but what you can do is check the registry to see which postType is set.

if ($postType === $this->_coreRegistry->registry('wordpress_post_type')) {
    if ($postType->getPostType() === 'post') {
        echo 'This is the archive for post_type=post';
    } else {
        echo 'This is the archive for post_type=' . $postType->getPostType();
    }
} else {
    echo 'This is not a post type archive page.';
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants