I have a seriously problem :( maybe someone can help me
The users registered in my wordpress page have one post only, and i created custom_post_type named "ofertas". The users can create one oferta only. With this code show the oferta title on index.php, the problem is show the oferta title in ALL POSTS. I want only show his own oferta title (if they have).
<?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; ?>
I created the custom_post_type with this code
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);