首页 > 解决方案 > 如何仅为我的cpt“投资组合”创建单独的“存档小部件”?

问题描述

我正在为我的投资组合创建一个自定义 wordpress 主题,尝试通过使用字段和元框从头开始做所有事情,并且不想使用任何插件。

我创建了一个cpt,如下所示:

function codex_custom_init() {

     register_post_type(
        'Portfolio', array(
        'labels' => array('name' => __( 'Portfolio' ), 'singular_name' => __( 'Portfolio' ) ),
        'public' => true,
        'has_archive' => true,
        'supports' => array('title', 'editor', 'thumbnail', 'comments'),
        'menu_icon' => 'dashicons-sos',
        )
    );

    //register taxonomy for portfolio post tags
    register_taxonomy( 
        'portfolio-tag', //taxonomy 
        'portfolio', //post-type
        array( 
            'hierarchical'  => false, 
            'label'         => __( 'Portfolio Tags','taxonomy general name'), 
            'singular_name' => __( 'Tag', 'taxonomy general name' ), 
            'rewrite'       => true, 
            'query_var'     => true 
        )
    );

    // add categories for Portfolio

    register_taxonomy(
            'portfoliocategories',
            'portfolio',
            array(
                'labels' => array(
                    'name' => 'Portfolio Categories',
                    'add_new_item' => 'Add New Portfolio Category',
                    'new_item_name' => "New Portfolio Type"
                ),
                'show_ui' => true,
                'show_tagcloud' => false,
                'hierarchical' => true
            )
        );

    }
    add_action( 'init', 'codex_custom_init' );

我已经创建了所有必需的页面和侧边栏。我想在我的“存档投资组合页面”上显示存档小部件,这是一个单独的存档小部件,其中包含仅与投资组合相关的帖子。感谢你的关心。

如何为 cpt 编辑和自定义:

<?php
    wp_get_archives(
    apply_filters(
    'widget_archives_args',
    array(
        'type'   => 'monthly',
        'show_post_count' => $count,
        ),
        $instance
        )
        );
    ?>

标签: phpwordpresswordpress-theming

解决方案


你在哪里显示你的小部件,把你的代码放在这个条件内。

if ( is_singular( 'yourcpt' ) ) {
    // conditional content/code

}

推荐阅读