在wordpress中注册的用户只有一个帖子,我创建了名为“ofertas”的custom_post_type。用户只能创建一个。使用此代码在index.php上显示oferta标题,问题是在所有POSTS中显示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_post_typeadd_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);
答案 0 :(得分:1)
如果我理解正确,您想要在ofertas CPT中显示当前登录的用户帖子?尝试更改此行:
author_id = get_query_var('author');
对此:
$author_id = get_current_user_id();
get_query_var(&#39; author)用于检索很可能未在主页上设置的公共查询变量。