初始情况
- custom post-type (projects)[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 );
- custom field (with 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' );
- front-page.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">
)正在顯示帖子型別專案中的隨機帖子。
有些內容我不想在首頁上看到。
Note: 该帖子必须處於活動狀態,因為這些帖子還有另一个頁面。
在後端,我有一个自定義欄位,我想在其中選擇是否將其顯示在首頁上。
您可以帮助我的方式- give me an idea how a solution could be
- give me an documentation about it
- give me a code snippet
- if you need something from me to help me leave a comment there.
- please don't hate me if I have some problems... I work for a short time now with code....
感谢您對我的帮助!!!! :))))
更新現在我有一个功能,可以通過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');
最新回復
- 7月前1 #
相似問題
- wordpress:建立退出確认弹出視窗wordpresspluginswordpressphpwordpressjavascript2021-01-05 22:52
- wordpress:使用類进行插件開發-公共和私有回撥wordpresscustomposttypeswordpressphpwordpressplugindevelopmentwordpresshookswordpressoop2021-01-02 06:25
- wordpress:需要替換货币縮寫wordpresspluginswordpressphpwordpressfunctionswordpresspluggablewordpressfunctionexists2020-12-26 15:25
- wordpress:从Php檔案到Ajax的响應未發送wordpresspluginswordpressphpwordpressajax2020-12-26 00:53
- wordpress:如何將自定義HTML添加到存档頁面帖子的內容中?wordpresscustomposttypeswordpressphpwordpressplugindevelopmentwordpressfilterswordpressarchives2020-12-18 08:23
而不是使用
pre_get_posts
钩子只需修改您的WP_Query
在特定條件下获取帖子。从一開始就仅获取与
value
相匹配的帖子 您提供的元密钥。