自定义发布类型的标签错误,被循环调用时找不到

时间:2019-09-09 作者:SailingClassProject

I\'m new to WordPress (and to code), having just started my classes. I am working on a project with a few students and can\'t find a way to call my Custom Post Type.

I tried several solutions from StackExchange, but I can\'t really find a solution for my problems.

Problem 1 : my custom Post type doesn\'t have the right name in my back office

Here\'s how I coded my custom post type :

function custom_post_type_bateau() {
    $labels = array(
        \'name\'               => \'Bateaux\',
        \'singular_name\'      => \'Bateau\',
        \'menu_name\'          => \'Bateaux\',
        \'name_admin_bar\'     => \'Ajouter\',
        \'add_new\'            => \'Nouveau bateau\',
        \'add_new_item\'       => \'Ajouter un bateau\',
        \'edit_item\'          => \'Modifier un bateau\',
        \'view_item\'          => \'Consulter un bateau\',
        \'search_items\'       => \'Chercher un bateau\',
        \'parent_item_colon\'  => \'Bateaux parents\',
        \'not_found\'          => \'Aucun bateau n\\\'a été trouvé\',
        \'not_found_in_trash\' => \'Aucun bateau n\\\'a été trouvé dans la corbeille\'
    );

    $args = array(
        \'labels\'             => \'Bateau\',
        \'description\'        => \'Un bateau de SailingLoc\',
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'rewrite\'            => array( \'slug\' => \'bateau\' ),
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_position\'      => null,
        \'menu_icon\'          =>\'dashicons-paperclip\',
        \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
    );

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

I called it in my functions.php :

add_action(\'init\', \'initialisation\');

function initialisation(){
    //activation des menus dans le backoffice
    register_nav_menus();
    //creation d\'un custom post type bateau
    custom_post_type_bateau();
    // activation des images à l\'affiche
    add_theme_support(\'post-thumbnails\');
}

Problem is that the name displayed in my back-office isn\'t "Bateaux" but "Articles".

Backoffice screenshot

Problem 2 : I can\'t call the post-type bateau So I made a page to display my post-types. I called it with this WP Query :

<?php
    $query = new WP_Query([
        \'post_type\' => \'bateau\',
        \'posts_per_page\'=>15
    ]);
?>
<?php
    $the_query = new WP_Query();
    $the_query->query($query);
; ?>
<div class="gp3">
<?php /*BOUCLE QUI RECUPERE LES DERNIERS POSTS*/
    if ( $the_query->have_posts() ) : 
        $cpt = 0;
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
?>
    <div class="col-4">
        <div class="fiche-bateau">
            <div class="image-bateau">  
                <div class="product-thumbnails image-responsive"><?php the_post_thumbnail(); ?>
                </div>
            </div>
            <div class="product-title"><?php the_title(\'<h3>\', \'</h3>\'); ?>
            </div>
        </div>
    </div>
<?php 
    $cpt ++;
    if ($cpt%3 == 0) :
?>
</div>
<div class="gp3">
<?php endif; ?>
<?php endwhile; else : ?>
    <p>Rien à afficher</p>
<?php endif; ?>
</div>

The variable $query should link to the custom post-type \'bateau\' but doesn\'t. Instead I get the blog posts.

Could you help me pretty please ?

1 个回复
SO网友:Jacob Peattie

问题1中标签的问题是您没有将标签传递给register_post_type(). 您已经在中定义了标签$labels 变量,但这是您传递给的register_post_type():

$args = array(
    \'labels\' => \'Bateau\',
    // ...
);

register_post_type( \'bateau\', $args );
你需要通过$labels 阵列:

$args = array(
    \'labels\' => $labels,
    // ...
);

register_post_type( \'bateau\', $args );
对于问题2,您的问题是您试图通过WP_Query 实例到另一个实例WP_Query. $query 只需要是参数数组,但您已经传递了WP_Query 对象:

<?php
    $query = new WP_Query([
        \'post_type\' => \'bateau\',
        \'posts_per_page\'=>15
    ]);
?>
<?php
    $the_query = new WP_Query();
    $the_query->query($query);
; ?>
这需要公正:

<?php
$the_query = new WP_Query ( [
    \'post_type\'      => \'bateau\',
    \'posts_per_page\' =>15
] );
?>
但是,您根本不需要创建这样的页面或模板。在post type注册中,您有:

\'has_archive\' => true,
这意味着您的帖子类型的存档(列表)页面将在以下位置自动创建:http://example.com/bateau/. 这就是您屏幕截图中管理栏中的“Voir les articles”链接所链接的内容。如果这不起作用,请确保转到“设置”>“永久链接”刷新永久链接(访问“设置”页面就足够了),并确保主题模板正确使用主循环:

if ( have_posts() ) : 
    while ( have_posts() ) : the_post();

    endwhile;
endif;
注意缺少$the_query-> 以及任何new WP_Query().

相关推荐

PHP文件不能在WordPress文件夹中工作,但可以在另一个虚拟主机中工作

因此,基本上我制作了一个php文件,该文件用两个参数(订单总数和订单号)调用,它会弹出一个带有EPC二维码的png(因此人们只需扫描它,他们的银行应用程序就会填写所有细节)。这个文件在一个空的虚拟主机中工作,但当我把它放在wordpress目录/虚拟主机中的任何位置时,它都无法工作。是否存在阻止非wordpress php文件执行的设置?