WordPress在单个页面上对自定义帖子类型隐藏帖子

时间:2020-11-16 作者:hendrik-eg

初始情况

  • 自定义帖子类型(项目)[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"> ) 显示来自帖子类型项目的随机帖子的内容。

      我不想在头版看到一些帖子。

      Note: 该帖子必须处于活动状态,因为还有另一个页面包含这些帖子。

      在我的后端,我有一个自定义字段,我想在其中选择它是否显示在首页。

      您可以帮助我的方式给我一个解决方案的想法给我一个关于它的文档,如果您需要我的帮助,请给我一个代码片段

      更新现在我有一个功能,可以通过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\');
      
      

1 个回复
最合适的回答,由SO网友:Deepak Kamat 整理而成

而不是使用pre_get_posts 钩子只需修改WP_Query 获取具有特定条件的帖子。

$loop = new WP_Query( array(
                    \'post_type\' => \'projects\',
                    \'orderby\'   => \'rand\',
                    \'posts_per_page\' => 1,
                    \'meta_query\' => array(
                            array(
                                \'key\'     => \'your_field_name\',
                                \'value\'   => \'the_value\'
                            ),
                        ),
                )
            );
这从一开始就只获取与value 对于您提供的元密钥。