初始情况
- 自定义帖子类型(项目)[projects.php]
<?php
namespace main_theme\ext\posttypes\projects;
function post_type() {
$labels = [
'name' => _x( 'Projects', 'Post Type General Name', 'main_theme' ),
'singular_name' => _x( 'Project', 'Post Type Singular Name', 'main_theme' ),
'menu_name' => __( 'Projects', 'main_theme' ),
'name_admin_bar' => __( 'Projects', 'main_theme' ),
'all_items' => __( 'Alle Projects', 'main_theme' ),
'add_new_item' => __( 'Project hinzufügen', 'main_theme' ),
'add_new' => __( 'Hinzufügen', 'main_theme' ),
'new_item' => __( 'Neues Project', 'main_theme' ),
'edit_item' => __( 'Editiere Project', 'main_theme' ),
'update_item' => __( 'Project updaten', 'main_theme' ),
'search_items' => __( 'Suche Project', 'main_theme' ),
'not_found' => __( 'Kein Project gefunden', 'main_theme' ),
'not_found_in_trash' => __( 'Kein Project im Papierkorb gefunden', 'main_theme' ),
];
$args = [
'label' => __( 'Project', 'main_theme' ),
'description' => __( 'Hier kannst du Projekte anlegen.', 'main_theme' ),
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5, // "posts" is 5
'menu_icon' => 'dashicons-media-code',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false, // products should be available for browsing
'exclude_from_search' => false,
'publicly_queryable' => true,
'supports' => [ 'title', 'editor', 'thumbnail', 'revisions','excerpt'],
'show_in_rest' => true,
'taxonomies' => ['post_tag'],
'rewrite' => [
'slug' => 'projekte',
'with_front' => false,
]
];
register_post_type('projects', $args );
}
add_action( 'init', __NAMESPACE__ . '\\post_type', 0 );
- 自定义字段(使用 cmb2)[projects.php]
<?php
namespace main_theme\ext\cmb2\projects;
/**
* Define the metabox and field configurations.
*/
function cmb2_metaboxes() {
// Start with an underscore to hide fields from custom fields list
$prefix = '_projects_';
/**
* Initiate the metabox
*/
$cmb = new_cmb2_box( array(
'id' => 'projects',
'title' => __( 'Projects', 'ideate' ),
'object_types' => array( 'projects', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
// 'cmb_styles' => false, // false to disable the CMB stylesheet
// 'closed' => true, // Keep the metabox closed by default
) );
$cmb->add_field( array(
'name' => __( 'Auf Startseite anzeigen', 'ideate' ),
'id' => $prefix . 'show',
'type' => 'select',
'preview_size' => 'large', // Image size to use when previewing in the admin.
'options' => array(
'yes' =>__( 'Ja', 'cmb' ),
'no' =>__( 'Nein', 'cmb' ),
)
) );
$cmb->add_field( array(
'name' => __( 'Logo', 'ideate' ),
'id' => $prefix . 'logo',
'type' => 'file',
// Optional:
'options' => array(
'url' => false, // Hide the text input for the url
),
'text' => array(
'add_upload_file_text' => 'Add File' // Change upload button text. Default: "Add or Upload File"
),
// query_args are passed to wp.media's library query.
'query_args' => array(
'type' => array(
'image/gif',
'image/jpeg',
'image/png',
),
),
'preview_size' => 'large', // Image size to use when previewing in the admin.
) );
}
add_action( 'cmb2_admin_init', __NAMESPACE__ . '\\cmb2_metaboxes' );
- 头版.php
<?php get_header(); ?>
<main>
<div class="m-stage">
<div class="m-stage-content">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php echo get_the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
<? $count = 2; ?>
<div class="m-rowteaser m-rowteaser--default"><?
$loop = new WP_Query( array(
'post_type' => 'projects',
'orderby' => 'rand',
'posts_per_page' => 1,)
);
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="m-rowteaser-item <?
if ($count%2 == 0){?>m-rowteaser-item--odd<?}
else
{?>m-rowteaser-item--even<?}?>">
<div class="m-rowteaser-image">
<a href="<?=get_the_permalink()?>">
<?=get_the_post_thumbnail( get_the_ID(),'full', "", ["class" => "m-rowteaser-img"] ); ?>
</a>
</div>
<div class="m-rowteaser-content">
<div>
<img src="<?=get_post_meta( get_the_ID(), '_projects_logo', true );?>" alt="<?=get_post_meta( get_the_ID(), '_projects_logo_alt', true );?>" class="m-rowteaser-logo">
<h2 class="m-rowteaser-title">
<a href="<?=get_the_permalink()?>">
<?php the_title(); ?>
</a>
</h2>
<?= wpautop( get_the_excerpt( get_the_ID(), '_projects_rte', true ) ); ?>
<a class="c-link" href="/projekte"><?=__( 'Alle Projekte', 'ideate' )?></a>
</div>
</div>
</div>
<? $count++; ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php get_template_part( 'partials/newsletter'); ?>
</main>
<?php get_footer(); ?>
我想要的是
在我的首页上有一个部分 ( <div class="m-rowteaser m-rowteaser--default">
) 显示来自帖子类型项目的随机帖子。
有些帖子我不想在首页看到。
注意:帖子必须处于活动状态,因为这些帖子还有另一个页面。
在我的后端,我有一个自定义字段,我想选择它是否显示在首页。
你可以帮助我的方式
- 给我一个想法如何解决
- 给我一份关于它的文件
- 给我一个代码片段
- 如果你需要我的东西来帮助我在那里发表评论。
- 如果我有问题,请不要恨我……我现在用代码工作了很短的时间……
感谢你们对我的帮助!!!!:))))
更新
现在我有一个功能可以通过 id 从首页隐藏帖子。我需要帮助将此代码与后端的自定义字段连接起来。
<?php
function wpb_exclude_from_home($query) {
if ($query->is_home() ) {
$query->set('post__not_in', array(134, 122,));
}
}
add_action('pre_get_posts', 'wpb_exclude_from_home');