仅按作者显示最旧的帖子

时间:2012-02-27 作者:Adam Rice

我正在使用大量定制的WP安装。我已经设置了一个特定的帖子类型、分类法和角色,可以只使用该帖子类型。

理想情况下,我希望限制使用该帖子类型的角色成员只创建一篇帖子。这似乎比它的价值更麻烦。因此,除此之外,我只想在存档视图中显示他们最老的帖子。

我有以下内容,它创建了一个可行的归档循环,但我还没有计算出“如果帖子数>1,那么只显示最旧的帖子”位。

    $args = array
    (\'post_type\' => \'ticketlisting\',
        \'posts_per_page\' => 50,
        \'tax_query\' => array
        (array
            (
            \'taxonomy\' => \'tier\',
            \'field\' => \'slug\',
            \'terms\' => \'goldstar\'
            )
        )
    );

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    the_title();
    echo \'<div class="entry-content">\';
    $post_id = get_the_ID();
    /* show post content here */
    echo \'</div>\';
endwhile;

1 个回复
SO网友:fuxia

要将用户限制为一篇帖子,请不要让他们创建新帖子。只需添加一个wp_editor() 实例到其配置文件。

钩住\'show_user_profile\'\'edit_user_profile\' 显示编辑器\'personal_options_update\' 和\'edit_user_profile_update\' 将内容保存到用户元或隐藏的自定义帖子类型

更新为了说明我的意思,我刷新了一个旧插件:

<?php # -*- coding: utf-8 -*-
/**
 * Plugin Name: T5 User text rich editor
 * Text Domain: t5_utre
 * Domain Path: /lang
 * Description: Adds a rich editor to the user profile.
 * Version:     2012.02.28
 * Required:    3.3
 * Author:      Fuxia Scholz
 * Author URI:  https://fuxia.de
 */

// Not a WordPress context? Stop.
! defined( \'ABSPATH\' ) and exit;

// Back end
add_action( \'admin_init\', array ( \'T5_User_Text_Rich_Edit\', \'init\' ) );
// Front end
// Call it your theme’s author.php:
// do_action( \'print_t5_user_rich_text\', $GLOBALS[\'authordata\']->ID );
add_action(
    \'print_t5_user_rich_text\',
    array ( \'T5_User_Text_Rich_Edit\', \'get_user_rich_text\' ),
    4
);

class T5_User_Text_Rich_Edit
{
    protected
        /**
         * Internal name for the user meta field.
         * @type string
         */
        $handle  = \'meta_post\'

        /**
         * Current user ID
         *
         *  @type int
         */
    ,   $user_id = NULL
    ;

    /**
     * Copy of $handle. Used by the static method.
     *
     * @type string
     */
    protected static $public_handle = \'meta_post\';

    /**
     * Creates a new instance. Called on \'admin_init\'.
     *
     * @see    __construct()
     * @return void
     */
    public static function init()
    {
        new self;
    }

    /**
     * Contructor. Registers filters and actions.
     *
     * @param array $params Just \'handle\', may be extended.
     * @return void
     */
    public function __construct( $params = array () )
    {
        isset ( $params[\'handle\'] )
            and $this->handle        = $params[\'handle\']
            and self::$public_handle = $params[\'handle\'];

        add_action( \'show_user_profile\',        array ( $this, \'show\' ) );
        add_action( \'edit_user_profile\',        array ( $this, \'show\' ) );
        add_action( \'personal_options_update\',  array ( $this, \'save\' ) );
        add_action( \'edit_user_profile_update\', array ( $this, \'save\' ) );

    }

    /**
     * Public access to the field in your theme or plugin.
     *
     * @param  int    $user_id
     * @param  string $before
     * @param  string $after
     * @return string
     */
    public static function get_user_rich_text(
        $user_id,
        $before = \'\',
        $after  = \'\',
        $print  = TRUE
    )
    {
        $content = get_the_author_meta( self::$public_handle, $user_id );
        ! empty ( $content )
            and $content = $before . wpautop( $content ) . $after;

        $print and print $content;
        return $content;
    }

    /**
     * Prints the form.
     *
     * @param  object $user
     * @return void
     */
    public function show( $user )
    {
        if ( ! current_user_can( \'edit_user\', $user->ID ) )
        {
            return;
        }

        $label_text = __( \'Your personal post\', \'t5_utre\' );
        $label      = "<label for=\'$this->handle\'>$label_text</label>";
        ?>
<table class="form-table">
    <tr>
        <th>
        <?php
        print $label;
        ?>
        </th>
        <td>
            <div style="width:504px">
            <?php
            $content = get_the_author_meta( $this->handle, $user->ID );
            $this->print_editor( $content );
            ?>
            </div>
        </td>
    </tr>
</table>
    <?php
    }

    /**
     * Print a new instance of wp_editor()
     *
     * @param string $content existing content
     * @return void
     */
    protected function print_editor( $content )
    {
        $editor_settings =  array (
            \'textarea_rows\' => 15
        ,   \'media_buttons\' => TRUE
        ,   \'teeny\'         => FALSE
        ,   \'tinymce\'       => TRUE
        );
        wp_editor( $content, $this->handle, $editor_settings );
    }

    /**
     * Saves the data.
     *
     * @param  int   $user_id
     * @return void
     */
    public function save( $user_id )
    {
        if ( ! current_user_can( \'edit_user\', $user_id ) )
        {
            return;
        }

        $this->user_id = $user_id;
        $text_input    = empty ( $_POST[ $this->handle ] ) ? \'\' : $_POST[ $this->handle ];
        update_user_meta( $user_id, $this->handle, $text_input );
    }
}
您必须自己添加语言文件(如果有人对此感兴趣,我也可以将其放在GitHub上)。

现在,在用户配置文件的底部有一个很好的编辑器:

enter image description here

在您的主题中author.php 您可以使用预定义的操作访问数据:

<?php do_action( \'print_t5_user_rich_text\', $GLOBALS[\'authordata\']->ID ); ?>
enter image description here

此插件不涉及用户可能拥有的任何其他功能。她必须具备编辑自己个人资料的能力,而不是其他能力。

结束