admin管理员组文章数量:1315792
Initial situation
- 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(); ?>
What I want
On my front-page is a section ( <div class="m-rowteaser m-rowteaser--default">
) what is displaying a random post from the post-type projects.
There are some post that I don't want to see at the front-page.
Note: the post must be active because there is another page with these post.
In my backend I have a custom field where I want to select if it is displayed at the front page.
The way you can help me
- 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....
Thank you for helping me!!!! :))))
Update
Now I have a function that can hide post from the front-page by the id. I need help to connect this code with the custom field in the backend.
<?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');
Initial situation
- 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(); ?>
What I want
On my front-page is a section ( <div class="m-rowteaser m-rowteaser--default">
) what is displaying a random post from the post-type projects.
There are some post that I don't want to see at the front-page.
Note: the post must be active because there is another page with these post.
In my backend I have a custom field where I want to select if it is displayed at the front page.
The way you can help me
- 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....
Thank you for helping me!!!! :))))
Update
Now I have a function that can hide post from the front-page by the id. I need help to connect this code with the custom field in the backend.
<?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');
Share
Improve this question
edited Nov 16, 2020 at 15:40
hendrik-eg
asked Nov 16, 2020 at 14:00
hendrik-eghendrik-eg
133 bronze badges
2
- Welcome to SE WordPress - your question is very broad, unfortunately this forum is for helping with straightforward and a single concern (no matter how complex), but you seem to be looking for an extensive guidance on how to complete your project - Please ask one question at a time and try to figure out yourself how to join those. – Deepak Kamat Commented Nov 16, 2020 at 16:01
- My question is that I want to connect the last code snippet with my custom field – hendrik-eg Commented Nov 16, 2020 at 16:07
1 Answer
Reset to default 0Instead of using the pre_get_posts
hook simply modify your WP_Query
to fetch posts with certain conditions.
$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'
),
),
)
);
This, right from the beginning only fetches the posts that matches the value
for the meta key you provide.
本文标签: pluginsWordpress hide post from custom posttype on a single page
版权声明:本文标题:plugins - Wordpress hide post from custom post-type on a single page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741990721a2409054.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论