将显示wp帖子类型更改为wp自定义帖子类型

时间:2013-06-03 作者:user33429

我下面的代码是基于类别显示wp帖子,但我想基于其类别显示名为“wiki”的wp自定义帖子。CPT wiki来自插件,所以我想已经定义好了。

class Pyre_Homepage_1col_Widget extends WP_Widget {

function Pyre_Homepage_1col_Widget()

{
    $widget_ops = array(\'classname\' => \'pyre_homepage_1col\', \'description\' => \'Homepage 1-column recent posts widget.\');
    $control_ops = array(\'id_base\' => \'pyre_homepage_1col-widget\');
    $this->WP_Widget(\'pyre_homepage_1col-widget\', \'Avenue Theme: Home 1-column\', $widget_ops, $control_ops);
}

function widget($args, $instance)

{
    extract($args);
    $title = $instance[\'title\'];
    $post_type = \'all\';
    $categories = $instance[\'categories\'];
    $posts = $instance[\'posts\'];
    $images = true;
    $rating = true;
    $show_excerpt = isset($instance[\'show_excerpt\']) ? \'true\' : \'false\';

    echo $before_widget;
    ?>
    <?php
    $post_types = get_post_types();
    unset($post_types[\'page\'], $post_types[\'attachment\'], $post_types[\'revision\'], $post_types[\'nav_menu_item\']);
    if($post_type == \'all\') {
        $post_type_array = $post_types;
    } else {
        $post_type_array = $post_type;
    }

    ?>
    <div class="block full">
        <h3><a href="<?php echo get_category_link($categories); ?>"><?php echo $title; ?></a> <span class="arrows">&raquo;</span></h3>
        <?php
        $recent_posts = new WP_Query(array(
            \'showposts\' => $posts,
            \'cat\' => $categories,
        ));
        ?>
        <?php
        $big_count = round($posts / 9);
        if(!$big_count) { $big_count = 1; }
        ?>

        <?php $counter = 1; while($recent_posts->have_posts()): $recent_posts->the_post(); ?>
        <?php
        if(has_post_format(\'video\') || has_post_format(\'audio\') || has_post_format(\'gallery\')) {
            $icon = \'<span class="\' . get_post_format($post->ID) . \'-icon"></span>\';
        } else {
            $icon = \'\';
        }
        ?>

        <?php if($counter <= $big_count): ?>
        <?php if($counter == $big_count) { $last = \'block-item-big-last\'; } else { $last = \'\'; }?>

        <div class="block-item-big <?php echo $last; ?>">
            <?php if($images && has_post_thumbnail()): ?>
            <?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), \'widget-image\'); ?>
            <div class="block-image"><a href=\'<?php the_permalink(); ?>\' title=\'<?php the_title(); ?>\'><img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" width=\'290\' height=\'160\' /></a><?php echo $icon; ?></div>
            <?php else: ?>
            <div class="block-image"><a href=\'<?php the_permalink(); ?>\' title=\'<?php the_title(); ?>\'><img src="<?php bloginfo(\'template_directory\'); ?>/timthumb.php?src=<?php bloginfo(\'template_directory\'); ?>/images/thumbnail.png&w=290&h=160" alt="<?php the_title(); ?>" width=\'290\' height=\'160\' /></a><?php echo $icon; ?></div>
            <?php endif; ?>

            <h2><a href=\'<?php the_permalink(); ?>\' title=\'<?php the_title(); ?>\'><?php the_title(); ?></a></h2><h4><?php echo \' - \' ?><?php the_time(\'F j, Y\'); ?></h4>
            <?php if($show_excerpt == \'true\'): ?><p><?php echo string_limit_words(get_the_excerpt(), 15); ?><a href=\'<?php the_permalink(); ?>\'> [...]</a></p><?php endif; ?>
        </div>

        <?php else: ?>

        <div class="block-item-small">
            <!--<?php if($images && has_post_thumbnail()): ?>
            <?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), \'widget-image-thumb\'); ?>
            <div class="block-image"><a href=\'<?php the_permalink(); ?>\' title=\'<?php the_title(); ?>\'><img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" width=\'50\' height=\'50\' /></a><?php echo $icon; ?></div>
            <?php else: ?>
            <div class="block-image"><a href=\'<?php the_permalink(); ?>\' title=\'<?php the_title(); ?>\'><img src="<?php bloginfo(\'template_directory\'); ?>/timthumb.php?src=<?php bloginfo(\'template_directory\'); ?>/images/thumbnail.png&w=50&h=50" alt="<?php the_title(); ?>"  width=\'50\' height=\'50\' /></a><?php echo $icon; ?></div>
            <?php endif; ?>-->
            <h4><?php echo the_time(\'d/n\'); ?><?php echo \' - \' ?><a href=\'<?php the_permalink(); ?>\' title=\'<?php the_title(); ?>\'><?php the_title(); ?></a></h4>
        </div>

        <?php endif; ?>

        <?php $counter++; endwhile; ?>
    </div>

    <?php
    echo $after_widget;
}

function update($new_instance, $old_instance)
{
    $instance = $old_instance;
    $instance[\'title\'] = $new_instance[\'title\'];
    $instance[\'post_type\'] = \'all\';
    $instance[\'categories\'] = $new_instance[\'categories\'];
    $instance[\'posts\'] = $new_instance[\'posts\'];
    $instance[\'show_images\'] = true;
    $instance[\'show_rating\'] = true;
    $instance[\'show_excerpt\'] = $new_instance[\'show_excerpt\'];

    return $instance;
}

function form($instance)
{
    $defaults = array(\'title\' => \'Recent Posts\', \'post_type\' => \'all\', \'categories\' => \'all\', \'posts\' => 4, \'show_excerpt\' => null);
    $instance = wp_parse_args((array) $instance, $defaults); ?>
    <p>
        <label for="<?php echo $this->get_field_id(\'title\'); ?>">Title:</label>
        <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" value="<?php echo $instance[\'title\']; ?>" />
    </p>

    <p>
        <label for="<?php echo $this->get_field_id(\'categories\'); ?>">Filter by Category:</label> 
        <select id="<?php echo $this->get_field_id(\'categories\'); ?>" name="<?php echo $this->get_field_name(\'categories\'); ?>" class="widefat categories" style="width:100%;">
            <option value=\'all\' <?php if (\'all\' == $instance[\'categories\']) echo \'selected="selected"\'; ?>>all categories</option>
            <?php $categories = get_categories(\'hide_empty=0&depth=1&type=post\'); ?>
            <?php foreach($categories as $category) { ?>
            <option value=\'<?php echo $category->term_id; ?>\' <?php if ($category->term_id == $instance[\'categories\']) echo \'selected="selected"\'; ?>><?php echo $category->cat_name; ?></option>
            <?php } ?>
        </select>
    </p>
    <p>
        <label for="<?php echo $this->get_field_id(\'posts\'); ?>">Number of posts:</label>
        <input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id(\'posts\'); ?>" name="<?php echo $this->get_field_name(\'posts\'); ?>" value="<?php echo $instance[\'posts\']; ?>" />
    </p>
    <p>
        <input class="checkbox" type="checkbox" <?php checked($instance[\'show_excerpt\'], \'on\'); ?> id="<?php echo $this->get_field_id(\'show_excerpt\'); ?>" name="<?php echo $this->get_field_name(\'show_excerpt\'); ?>" /> 
        <label for="<?php echo $this->get_field_id(\'show_excerpt\'); ?>">Show excerpt</label>
    </p>
<?php }}

1 个回复
SO网友:user33484

自定义帖子类型是您可以创建的新帖子类型。自定义帖子类型可以通过register_post_type() 作用此功能允许您根据标签、支持的功能、可用性和其他细节定义新的帖子类型。

请注意,您必须致电register_post_type() 在admin\\u菜单之前和after\\u setup\\u主题操作挂钩之后。一个好的钩子是init钩子。

Here\'s a basic example of adding a custom post type:

add_action( \'init\', \'create_post_type\' );
function create_post_type() {
    register_post_type( \'acme_product\',
        array(
            \'labels\' => array(
                \'name\' => __( \'Products\' ),
                \'singular_name\' => __( \'Product\' )
            ),
        \'public\' => true,
        \'has_archive\' => true,
        )
    );
}
这将创建一个名为Product的post类型,标识为acme\\u Product。这个register_post_type() 函数接收两个主要参数。第一种是标签,它以复数和单数形式定义帖子类型的名称。第二个是public,它是一个预定义的标志,用于在管理屏幕上显示帖子类型,并使其在查询时显示在网站内容本身中。

您可以将更多参数传递给register_post_type() 函数,可以执行设置层次结构(类似于页面)、在搜索中显示新帖子类型、更改新帖子的URL以及在帖子编辑屏幕中隐藏或显示元框等操作。

结束

相关推荐

load more posts by category

正如你所看到的,我在每个标签中按类别加载,并在10篇帖子完成后,按我想设置的每个标签显示总共10篇帖子。我想在我已经完成的10篇文章的底部添加一个加载更多按钮,但查看者必须查看其他特定类别的文章,所以我想通过保持在同一页面中,在该选项卡中加载更多文章,按该类别显示更多文章。这是我按类别显示每个选项卡的操作(&A);按员额总数计算 <?php $rightbox = get_posts(\'&cat=8&showposts=10\'); foreach($right