Wordpress在单个页面上从自定义帖子类型隐藏帖子 初始情况 自定义帖子类型(项目)[projects.php]

Wordpress在单个页面上从自定义帖子类型隐藏帖子 初始情况 自定义帖子类型(项目)[projects.php],php,wordpress,custom-post-type,custom-fields,cmb2,Php,Wordpress,Custom Post Type,Custom Fields,Cmb2,您可以使用该字段仅显示\u projects\u show设置为yes的项目(假设该字段是您要使用的字段,请根据需要调整下面的代码): 您必须根据使用wp\u get\u post\u meta(get\u ID(),'\u projects\u show,true)从自定义字段元盒获取的数据,在if($query->is\u home())中添加或修改一个子句。 <?php namespace main_theme\ext\posttypes\projects; function

您可以使用该字段仅显示
\u projects\u show
设置为
yes
的项目(假设该字段是您要使用的字段,请根据需要调整下面的代码):


您必须根据使用
wp\u get\u post\u meta(get\u ID(),'\u projects\u show,true)从自定义字段元盒获取的数据,在
if($query->is\u home())
中添加或修改一个子句。

<?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 );


<?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 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(); ?>



<?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');

$loop = new WP_Query( array(
    'post_type' => 'projects',
    'orderby' => 'rand',
    'posts_per_page' => 1,
    'meta_query' => array(
        array(
            'key' => '_projects_show',
            'value' => 'yes',
            'compare' => '==',
        ),
    ),
));