Ich arbeite mit einer stark angepassten Installation von WP. Ich habe einen bestimmten Beitragstyp, eine Taxonomie und Rollen eingerichtet, die nur diesen Beitragstyp verwenden können.
Im Idealfall möchte ich Mitglieder dieser Rolle einschränken, indem Sie diesen Beitragstyp verwenden, um nur einen Beitrag zu erstellen. Dies scheint mehr Ärger zu bereiten, als es wert ist. Vorbehaltlich dessen möchte ich nur den ältesten ihrer Beiträge in einer Archivansicht anzeigen.
Ich habe die folgenden Informationen, die eine funktionsfähige Archivschleife erstellen, aber ich habe nicht herausgefunden, ob das Bit "Wenn die Anzahl der Beiträge> 1 ist, wird nur der älteste Beitrag angezeigt".
$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;
Um die Benutzer auf nur einen Beitrag zu beschränken, lassen Sie sie keine neuen Beiträge erstellen. Fügen Sie einfach eine wp_editor()
Instanz zu ihrem Profil hinzu.
'show_user_profile'
und 'edit_user_profile'
ein, um den Editor anzuzeigen.'personal_options_update'
und 'edit_user_profile_update'
ein, um den Inhalt in einem Benutzer-Meta oder einem verborgenen benutzerdefinierten Beitragstyp zu speichern.Jetzt müssen sie nicht mehr suchen und können keine neuen Posts erstellen.
Um zu veranschaulichen, was ich meine, habe ich ein altes Plugin aktualisiert:
<?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: Thomas Scholz <[email protected]>
* Author URI: http://toscho.de
* License: MIT
* License URI: http://www.opensource.org/licenses/mit-license.php
*
* Copyright (c) 2012 Thomas Scholz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// 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 );
}
}
Sie müssen die Sprachdateien selbst hinzufügen (oder ich stelle sie auf GitHub, wenn jemand interessiert genug ist).
Sie erhalten jetzt einen netten Editor am Ende des Benutzerprofils:
Im author.php
Ihres Themas können Sie die vordefinierte Aktion verwenden, um auf die Daten zuzugreifen:
<?php do_action( 'print_t5_user_rich_text', $GLOBALS['authordata']->ID ); ?>
Dieses Plugin berührt keine anderen Funktionen des Benutzers. Es ist an die Fähigkeit gebunden, ihr eigenes Profil zu bearbeiten, sonst nichts.