Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于post类型加载边栏块_Php_Wordpress_Magento_Custom Post Type_Fishpig - Fatal编程技术网

Php 基于post类型加载边栏块

Php 基于post类型加载边栏块,php,wordpress,magento,custom-post-type,fishpig,Php,Wordpress,Magento,Custom Post Type,Fishpig,我正在为Magento使用Fishpig WordPress扩展(带有CPT扩展),但我似乎不知道如何根据当前帖子的类型加载边栏块。我只想在以下情况下加载特定块: 我正在查看一篇类型为recipe 我正在查看类型recipe 我正在查看自定义分类法recipe\u类别的术语页面 对于single post视图,我在我的local.xml中添加了如下块: <wordpress_post_view> <reference name="right">

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

  • 我正在查看一篇类型为
    recipe
  • 我正在查看类型
    recipe
  • 我正在查看自定义分类法
    recipe\u类别的术语页面
对于single post视图,我在我的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
post类型中。对于
recipe
归档和
recipe\u类别
分类术语归档也是如此。

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

$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();

感谢@BenTideswell提醒我们,这个扩展已经提供了一个合适的布局句柄,可以用于此目的,所以我们不需要再创建一个。我们只需要针对适当的职位类型做几项工作:

<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>

配方类别
配方类别

上面的海报是正确的,使用布局句柄是一种很好的方法(尽管wordpress\u post\u view\u POSTTYPE布局句柄已经存在,因此不需要通过观察者创建),但我认为这种方法对于大部分用户来说可能太过技术化

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


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

在撰写本文时,这是最好的解决方案。由于它对大多数用户来说可能太复杂了,我已经更新了扩展,并添加了对自定义边栏插件的支持,允许您通过WordPress管理员自动创建边栏。@BenTideswell-感谢您更新扩展,让用户更容易完成这项工作。我更新了我的答案,只显示布局更新XML概念,并以现有句柄为目标。