II’ve created a post type to add campaigns to my site. And I’m trying to get all the posts and display them on an archive.php page and also a single.php page to display the individual campaigns.
But when I display them on the site I get 404 errors: page not found.
I try to create two files to display alls campaigns:
This is my custom-post-type.php file
<?php
// Register a custom post type for campaigns
function fp_create_campaign_post_type() {
register_post_type('campaign',
array(
'labels' => array(
'name' => __('Campagnes'),
'singular_name' => __('Campagne'),
'add_new' => __('Ajouter une campagne'),
'add_new_item' => __('Ajouter une campagne'),
'edit_item' => __('Modifier la campagne'),
'new_item' => __('Nouvelle campagne'),
'view_item' => __('Voir la campagne'),
'search_items' => __('Rechercher parmi les campagnes'),
'not_found' => __('Aucune campagne trouvée'),
'not_found_in_trash' => __('Aucune campagne trouvée dans la corbeille')
),
'public' => true,
'has_archive' => "campaign",
'supports' => array('title', 'editor', 'thumbnail')
)
);
}
function custom_post_type_list_shortcode() {
ob_start();
$type = 'custom_post_type';
$args = array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => true
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) :
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
endwhile;
echo '</ul>';
else :
echo '<p>Aucune publication trouvée.</p>';
endif;
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode('custom_post_type_list', 'custom_post_type_list_shortcode');
add_action('init', 'fp_create_campaign_post_type');
This is my template-campaign.php file, it must display alls projects
<?php
/*
Template Name: Campagnes de financement participatif
*/
get_header();
//personnalisée pour afficher les articles de type "campaign"
$args = array(
'post_type' => 'campaign',
'posts_per_page' => -1, // Afficher tous les articles
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
// Affichage du titre de l'article
echo '<h2>' . get_the_title() . '</h2>';
// Affichage de la barre de progression
echo fp_get_campaign_progress_bar(get_the_ID());
endwhile;
wp_reset_postdata();
else :
echo 'Aucune campagne trouvée';
endif;
get_footer();
?>
David Forgo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.