0

我正在使用 Magento 的 Fishpig WordPress 扩展(带有 CPT 扩展),但我似乎无法根据当前帖子的类型确定如何加载侧边栏块。我只想在以下情况下加载特定块:

  • 我正在查看一个类型为的帖子recipe
  • 我正在查看该类型的存档recipe
  • 我正在查看自定义分类的术语页面recipe_category

对于单个帖子视图,我在 local.xml 中添加了该块,如下所示:

<wordpress_post_view>
    <reference name="right">
        <remove name="wordpress.widget.categories" />
        <block type="wordpress/sidebar_widget_categories" name="wordpress.widget.recipe_categories" before="-" as="recipe_categories" template="wordpress/sidebar/widget/categories.phtml">
            <action method="setTitle"><title>Recipe Categories</title></action>
            <action method="setTaxonomy"><title>recipe_category</title></action>
        </block>
    </reference>
</wordpress_post_view>

这很好,我只需要弄清楚如何将其限制为仅针对recipe帖子类型显示。recipe档案和recipe_category分类术语档案也是如此。

4

3 回答 3

2

上面的海报是正确的,使用布局句柄是一个很好的方法(虽然 wordpress_post_view_POSTTYPE 布局句柄已经存在,所以不需要通过观察者创建它)但我认为这种方法对于大型部分用户。

对此,我刚刚发布了Magento WordPress 集成的 3.1.1.25 版本,增加了对自定义边栏插件的支持。此插件允许您在 WordPress 管理员中创建额外的侧边栏,并根据帖子类型、存档类型(类别、日期、主页、搜索等)等内容触发它们显示,并为每个特定帖子指定不同的侧边栏。这一切都可以通过 WordPress 管理 > 小部件页面完成。

要添加此功能,请将扩展升级到最新版本,然后在 WordPress 管理员中安装自定义边栏插件。然后,您将能够创建自定义侧边栏,而无需接触任何代码。

于 2015-03-06T18:05:41.863 回答
0

wordpress/sidebar/widget/categories.phtml通过检查模板文件中的帖子类型,我设法拼凑出一个解决方案。仍然对更清洁的解决方案感兴趣。

$post_type = 'post';
if( $post = Mage::registry('wordpress_post') ) {
    $post_type = $post->getPostType();
} elseif( $type = Mage::registry('wordpress_post_type') ) {
    $post_type = $type->getPostType();
} elseif( $term = Mage::registry('wordpress_term') ) {
    $post_type = $term->getTaxonomy() == 'recipe_category' ? 'recipe' : 'post';
}

if( $post_type == 'recipe' ) {
    $this->setTaxonomy('recipe_category');
    $this->setTitle('Recipe Categories');
}

$categories = $this->getCategories();
于 2015-03-02T23:45:40.560 回答
0

感谢@BenTideswell 提醒我们这个扩展已经提供了一个适当的布局句柄,可以用于这个目的,所以我们不需要创建另一个。我们只需要针对适当的帖子类型进行几次布局 XML 更新

<wordpress_post_view>
    <reference name="right">
        <remove name="wordpress.widget.categories"/>
    </reference>
</wordpress_post_view>

<wordpress_post_view_recipe>
    <reference name="right">
        <block type="wordpress/sidebar_widget_categories" name="wordpress.widget.recipe_categories" before="-" as="recipe_categories" template="wordpress/sidebar/widget/categories.phtml">
            <action method="setTitle"><title>Recipe Categories</title></action>
            <action method="setTaxonomy"><title>recipe_category</title></action>
        </block>
    </reference>
</wordpress_post_view_recipe>
于 2015-03-03T01:43:44.807 回答