每个作者在其自己的帖子中的自定义帖子类型标题

时间:2014-01-09 作者:Vektor Unbreakable

我有一个严重的问题:(也许有人能帮我

在我的wordpress页面中注册的用户只有一篇帖子,我创建了名为“ofertas”的自定义post\\u类型。用户只能创建一个RTA。使用此代码在索引上显示oferta标题。php,问题是在所有帖子中显示oferta标题。我只想展示他自己的欧足联头衔(如果他们有)。

<?php 
// First loop
while ( have_posts() ): the_post(); 
author_id = get_query_var(\'author\');
    $ofertas = array();

// Second Loop
$i = 0;
$args = array(
    \'author\' => $author_id,
    \'post_type\' => \'ofertas\'
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) : 
    while ($the_query->have_posts()) : $the_query->the_post(); // check if it has offers, if it has, loop over the offers
        $ofertas[$i][\'title\'] = get_the_title(); // or anything you need
        $i++;
    endwhile; // close the loop
else: // if it has no post
    continue; // we don\'t display the post
endif;

wp_reset_postdata(); 
?>
<div class="post">
<?php
the_title(); // or anything you need

foreach ($ofertas as $oferta):
    echo $oferta[\'title\']; // we display the title of the offer
endforeach;
?>
</div>
<?php  endwhile; ?>
我使用此代码创建了custom\\u post\\u类型

add_action(\'init\', \'crear_tipo_ofertas\', 0);

function crear_tipo_ofertas() {
$labels = array(
  \'name\'               => _x( \'Ofertas\', \'Post Type General Name\', \'text_domain\' ),
  \'singular_name\'      => _x( \'Oferta\', \'Post Type Singular Name\', \'text_domain\' ),
  \'menu_name\'          => __( \'Ofertas\', \'text_domain\' ),
  \'parent_item_colon\'  => __( \'Oferta Padre\', \'text_domain\' ),
  \'all_items\'          => __( \'Ofertas\', \'text_domain\' ),
  \'view_item\'          => __( \'Ver Oferta\', \'text_domain\' ),
  \'add_new_item\'       => __( \'Añadir Oferta Nueva\', \'text_domain\' ),
  \'add_new\'            => __( \'Añadir\', \'text_domain\' ),
  \'edit_item\'          => __( \'Editar Oferta\', \'text_domain\' ),
  \'update_item\'        => __( \'Actualizar\', \'text_domain\' ),
  \'search_items\'       => __( \'Buscar Ofertas\', \'text_domain\' ),
  \'not_found\'          => __( \'Ofertas no encontradas\', \'text_domain\' ),
  \'not_found_in_trash\' => __( \'Ofertas no encontradas en Papelera\', \'text_domain\' ),
  );

$rewrite = array(
  \'slug\'                => \'oferta\',
  \'with_front\'          => true,
  \'pages\'               => true,
  \'feeds\'               => true,
  );

$args = array(
  \'label\'               => __( \'oferta\', \'text_domain\' ),
  \'description\'         => __( \'Ofertas Spas y Balnearios\', \'text_domain\' ),
  \'labels\'              => $labels,
  \'supports\'            => array( \'title\', \'editor\', \'comments\', \'thumbnail\', \'custom-fields\'),
  \'taxonomies\'          => array( \'category\', \'post_tag\' ),
  \'hierarchical\'        => false,
  \'public\'              => true,
  \'show_ui\'             => true,
  \'show_in_menu\'        => true,
  \'show_in_nav_menus\'   => true,
  \'show_in_admin_bar\'   => true,
  \'menu_position\'       => 5,
  \'menu_icon\'           => site_url().\'/wp-content/plugins/my_plugin/images/logo.png\',
  \'can_export\'          => true,
  \'has_archive\'         => \'ofertas\',
  \'exclude_from_search\' => false,
  \'query_var\'           => \'ofertas\',
  \'rewrite\'             => $rewrite,
  \'capability_type\'     => \'post\',
  );

register_post_type(\'ofertas\', $args);
}

add_action(\'init\', \'crear_tipo_ofertas\', 0);

1 个回复
最合适的回答,由SO网友:MrJustin 整理而成

如果我没弄错的话,你想用自定义帖子类型显示某个用户的帖子

这是我将使用的代码,它可能会使用更多的自定义,但它应该会给你想要的效果。

<?php
function display_oftera_post () {
    global $current_user;
    get_currentuserinfo(); // Logged in User

    if ( is_user_logged_in() ) { // If User is logged in, display the post

        $args = array( // Post Arguments
            \'post_type\' => \'oftera\', // Post Type
            \'author\'    => $current_user->ID
        );

        $the_query = new WP_Query( $args);
                while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
                                     <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                                     <p> <?php the_content(); ?> </p>
        <?php endwhile;
        }
    }
?>

结束