回访:2016年2月2日,原始代码有许多问题
未对数据进行清理和验证,这可能导致严重的安全问题
有些部分是重复的
有点凌乱,有时难以阅读
一些部门只起到了部分作用
使用globals真的很邪恶
这就是为什么我重新审视这个答案并更新代码以解决上述问题的原因。代码现在更干净、更安全、更易于阅读和调试。确保在ORIGINAL ANSWER 部分
在我去看原作之前ORIGINAL ANSWER 第节,我想添加一个我认为更好使用的替代方案
替代方法这是一种直接的替代解决方案,不涉及自定义模板(,除了content.php
)或修改任何模板。你需要做的就是
使用所需的任何页面模板创建新页面
创建content.php
如果您的主题在默认情况下没有可用的模板部分,则任何此类模板部分的模板部分
添加以下代码并完成
$query = new PreGetPostsForPages(
251, // Page ID we will target
\'content\', //Template part which will be used to display posts, name should be without .php extension
true, // Should get_template_part support post formats
false, // Should the page object be excluded from the loop
[ // Array of valid arguments that will be passed to WP_Query/pre_get_posts
\'post_type\' => \'post\',
\'posts_per_page\' => 2
]
);
$query->init();
的
PreGetPostsForPages
可以找到类
in my answer here 以及如何使用它的详细说明
据我所知,最初的答案是,您正在专门寻找page template 您可以在页面管理添加/编辑屏幕的页面属性下设置,并自动为其分配类别。
这里有一个页面模板,它可以做到这一点。您可以在后端创建新页面,将此模板分配给该页面,然后为其设置特定类别,所有这些都可以从后端进行。
Here is how it work
我根据教程改编了此模板
digitalraindrops 您可以在提供的链接上查看。我对它做了一些修改,以便在2144年工作。我还删除了原件
WP_Query
因为它的使用方式实际上与正常的
query_posts
不应使用的查询。
Note: 此功能不适用于插件或主题。如后文所述,有更好、性能更好的选项来更改主查询。query\\u posts()是一种过于简单且有问题的方法,通过将页面的主查询替换为新的查询实例来修改它。它效率低下(重新运行SQL查询),并且在某些情况下会彻底失败(尤其是在处理POST分页时)。
在我开始之前应该注意,您也可以创建一个适当的类别。php模板,创建自定义菜单并将所需类别添加到菜单中。您还应该注意,您可以将类别名称用作页面slug,但是cannot 这些页面有任何子页面,此will 干扰并中断Template Hierarchy
为了完成我们需要的所有工作,您需要创建一个自定义元框,当选择特定模板时,该元框将添加到页面添加/编辑屏幕。此元框将用于选择将用于所选页面的类别。您还可以设置自定义posts_per_page
并对帖子进行排序。下面是添加自定义元框的代码。将这些代码添加到函数中。php或任何函数相关文件
<?php
add_action( \'admin_init\', function ()
{
$post_id = filter_input( INPUT_GET, \'post\', FILTER_VALIDATE_INT );
if ( $post_id ) {
// Get the current page template
$post_meta = get_post_meta( $post_id );
// Make sure that we only target our desired template
if ( isset ( $post_meta[\'_wp_page_template\'][0] )
&& \'page-pop.php\' === $post_meta[\'_wp_page_template\'][0]
) {
add_meta_box(
\'pop_meta_box\',
__( \'Page of Posts with the same name\' ),
\'pop_metabox_options\',
\'page\',
\'side\',
\'core\'
);
} else {
if( isset( $meta[\'_cat_id\'][0] ) ) {
$meta_value_array = [
\'_cat_id\',
\'_page_title\',
\'_posts_title\',
\'_order_by\',
\'_asc\',
\'_post_count\'
];
foreach ( $meta_value_array as $value )
pop_helper_update_post_meta( $post_id, $value, \'\' );
remove_meta_box( \'pop_meta_box\', \'page\', \'side\' );
}
}
}
add_action( \'save_post\', \'pop_update_post_meta_box\' );
});
function get_pop_order_by_list()
{
// Set the sort order
$sort = [
[
\'DESC\' => [
\'value\' => \'DESC\',
\'label\' => \'Descending\'
],
\'ASC\' => [
\'value\' => \'ASC\',
\'label\' => \'Ascending\'
],
]
];
// Create an array of values to order the posts by
$order_list = [
[
\'none\' => [
\'value\' => \'none\',
\'label\' => \'None\'
],
\'id\' => [
\'value\' => \'ID\',
\'label\' => \'Post ID\'
],
\'author\' => [
\'value\' => \'author\',
\'label\' => \'Author\'
],
\'title\' => [
\'value\' => \'title\',
\'label\' => \'Post Title\'
],
\'date\' => [
\'value\' => \'date\',
\'label\' => \'Post Date\'
],
\'modified\' => [
\'value\' => \'modified\',
\'label\' => \'Modified Date\'
],
\'parent\' => [
\'value\' => \'parent\',
\'label\' => \'Parent Post\'
],
\'rand\' => [
\'value\' => \'rand\',
\'label\' => \'Random\'
],
\'comment_count\' => [
\'value\' => \'comment_count\',
\'label\' => \'Comment Count\'
],
\'menu_order\' => [
\'value\' => \'menu_order\',
\'label\' => \'Menu Order\'
],
]
];
return $list = array_merge( $sort, $order_list );
}
function pop_metabox_options()
{
$post_id = filter_input( INPUT_GET, \'post\', FILTER_VALIDATE_INT );
if ( !$post_id )
return;
// Make sure the current user have the edit_page ability
if ( !current_user_can( \'edit_post\', $post_id ) )
return;
// Get the current page template
$template_file = get_post_meta( $post_id, \'_wp_page_template\', true );
// Make sure that we only target our desired template
if ( \'page-pop.php\' !== $template_file )
return;
// Get all the post meta values and sanitize/validate them
$post_meta = get_post_meta( $post_id );
$filters = [
\'_cat_id\' => [
\'filter\' => FILTER_VALIDATE_INT,
\'default\' => 1
],
\'_page_title\' => [
\'filter\' => FILTER_SANITIZE_STRING,
\'default\' => \'\'
],
\'_posts_title\' => [
\'filter\' => FILTER_SANITIZE_STRING,
\'default\' => \'\'
],
\'_order_by\' => [
\'filter\' => FILTER_SANITIZE_STRING,
\'default\' => \'ID\'
],
\'_asc\' => [
\'filter\' => FILTER_SANITIZE_STRING,
\'default\' => \'DESC\'
],
\'_post_count\' => [
\'filter\' => FILTER_VALIDATE_INT,
\'default\' => get_option( \'posts_per_page\' )
],
];
foreach ( $filters as $key=>$value ) {
if ( !array_key_exists( $key, $post_meta ) ) {
$post_meta[$key][0] = $value[\'default\'];
} else {
$post_meta[$key][0] = filter_var( $post_meta[$key][0], $value[\'filter\'], $value[\'default\'] );
}
}
?>
<!-- Sart the meta boxes -->
<div class="inside">
<p>
<label>
<strong><?php _e( \'Page Title\' ); ?></strong>
</label>
</p>
<input id="_posts_title" name="_posts_title" type="text" style="width: 98%;" value="<?php echo $post_meta[\'_page_title\'][0]; ?>"/>
<p>
<label>
<strong><?php _e( \'Post Title\' ); ?></strong>
</label>
</p>
<input id="_page_title" name="_page_title" type="text" style="width: 98%;" value="<?php echo $post_meta[\'_posts_title\'][0]; ?>"/>
<p>
<label>
<strong><?php _e( \'Category\', \'pietergoosen\' ); ?></strong>
</label>
</p>
<select id="_cat_id" name="_cat_id">
<?php
// Get all the categories
$categories = get_categories();
foreach ( $categories as $cat ) {
$selected = ( $cat->cat_ID == $post_meta[\'_cat_id\'][0] ) ? \' selected = "selected" \' : \'\';
$option = \'<option \'.$selected .\'value="\' . $cat->cat_ID;
$option = $option .\'">\';
$option = $option .$cat->cat_name;
$option = $option .\'</option>\';
echo $option;
} //endforeach
?>
</select>
<?php
if ( function_exists( \'get_pop_order_by_list\' ) ) {
$list = get_pop_order_by_list();
?>
<p>
<label>
<strong><?php _e( \'Order\' )?><strong>
</label>
</p>
<select id="_asc" name="_asc">
<?php
foreach ( $list[0] as $output ) {
$selected = ( $output[\'value\'] == $post_meta[\'_asc\'][0] ) ? \' selected = "selected" \' : \'\';
$option = \'<option \'.$selected .\'value="\' . $output[\'value\'];
$option = $option .\'">\';
$option = $option .$output[\'label\'];
$option = $option .\'</option>\';
echo $option;
} //endforeach
unset ( $output );
?>
</select>
<p>
<label>
<strong><?php _e( \'Sort by\' )?></strong>
</label>
</p>
<select id="_order_by" name="_order_by">
<?php
foreach ( $list[1] as $output ) {
$selected = ( $output[\'value\'] == $post_meta[\'_order_by\'][0] ) ? \' selected = "selected" \' : \'\';
$option = \'<option \'.$selected .\'value="\' . $output[\'value\'];
$option = $option .\'">\';
$option = $option .$output[\'label\'];
$option = $option .\'</option>\';
echo $option;
} //endforeach
unset ( $output );
?>
</select>
<?php
}
?>
<p>
<label>
<strong><?php _e( \'Posts per Page\' ); ?><strong>
</label>
</p>
<input id="_post_count" name="_post_count" type="text" value="<?php echo $post_meta[\'_post_count\'][0]; ?>" size="3" />
</div>
<!-- End page of posts meta box -->
<?php
}
function pop_update_post_meta_box( $post_id )
{
// Make sure we have a valid $_POST method
if ( !$_POST )
return;
// Make sure the current user have the edit_page ability
if ( !current_user_can( \'edit_page\', $post_id ) )
return;
// Get the current page template
$template_file = get_post_meta( $post_id, \'_wp_page_template\', true );
// Make sure that we only target our desired template
if ( \'page-pop.php\' !== $template_file )
return;
// Do nothing on auto save, just bail
if ( defined( \'DOING_AUTOSAVE\' )
&& DOING_AUTOSAVE
)
return;
$args = [
\'_cat_id\' => [
\'filter\' => FILTER_VALIDATE_INT,
\'default\' => 1
],
\'_page_title\' => [
\'filter\' => FILTER_SANITIZE_STRING,
\'default\' => \'\'
],
\'_posts_title\' => [
\'filter\' => FILTER_SANITIZE_STRING,
\'default\' => \'\'
],
\'_order_by\' => [
\'filter\' => FILTER_SANITIZE_STRING,
\'default\' => \'date\'
],
\'_asc\' => [
\'filter\' => FILTER_SANITIZE_STRING,
\'default\' => \'DESC\'
],
\'_post_count\' => [
\'filter\' => FILTER_VALIDATE_INT,
\'default\' => get_option( \'posts_per_page\' )
],
];
$meta = filter_input_array( INPUT_POST, $args );
if ( !$meta )
return;
// Loop throught the array and update meta values
foreach ( $meta as $k=>$v )
pop_helper_update_post_meta( $post_id, $k, $v );
}
function pop_helper_update_post_meta( $post_id = \'\', $key = \'\', $data = \'\' )
{
// Make sure we have valid values, if not, return false
if ( !$post_id
|| !$key
)
return false;
// Sanitize and validate values
$post_id = filter_var( $post_id, FILTER_VALIDATE_INT );
$key = filter_var( $key, FILTER_SANITIZE_STRING );
$data = filter_var( $data, FILTER_SANITIZE_STRING );
// Get the post meta values
$post_meta = get_post_meta( $post_id, $key, true );
if( $data
&& $post_meta != $data
) {
update_post_meta( $post_id, $key, $data );
}
if ( $post_meta
&& !$data
) {
delete_post_meta( $post_id, $key );
}
}
现在是页面模板。在根目录中创建一个文件并调用它
page-pop.php
. 你必须这样称呼你的模板。如果需要重命名,请签入上面的代码并更改的所有实例
page-pop.php
到您选择的模板名称
第一部分将从元框中调用所有保存的值。这些值将反馈到您的自定义WP_Query
按类别收回所有帖子。
第一个循环是正常的页面模板循环,可用于在页面添加/编辑屏幕中显示添加到WYSIWYG编辑器的内容。第二个循环/查询将返回您的帖子。您应该注意,这里的所有标记都是针对默认的214主题的,您需要修改它以适合您的主题。
下面是应该进入您的page-pop.php
样板
<?php
/**
* Template Name: Page of Posts
*/
get_header(); ?>
<?php
//See if we have any values and set defaults in case
$post_meta = get_post_meta( get_queried_object_id() );
$catid = isset( $post_meta[\'_cat_id\'] ) ? $post_meta[\'_cat_id\'][0] : 1;
$page_title = isset( $post_meta[\'_page_title\'] ) ? $post_meta[\'_page_title\'][0] : \'\';
$posts_title = isset( $post_meta[\'_posts_title\'] ) ? $post_meta[\'_posts_title\'][0] : \'\';
$orderby = isset( $post_meta[\'_order_by\'] ) ? $post_meta[\'_order_by\'][0] : \'date\';
$asc = isset( $post_meta[\'_asc\'] ) ? $post_meta[\'_asc\'][0] : \'DESC\';
$post_count = isset( $post_meta[\'_post_count\'] ) ? $post_meta[\'_post_count\'][0] : get_option(\'posts_per_page\');
?>
<div id="main-content" class="main-content">
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<!-- Page Title -->
<?php if( $page_title ) { ?>
<article id="posts-title">
<header class="entry-header">
<h2 class="entry-title"><?php echo $page_title; ?></h2>
</header><!-- .entry-header -->
</article><!-- #posts-title -->
<?php } ?>
<?php the_post(); ?>
<?php global $post;
if( $post->post_content || $page_title ) : ?>
<div class="entry-content">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if( $posts_title ) : ?>
<header class="entry-header">
<h1 class="entry-title"><?php echo $posts_title; ?></h1>
</header><!-- .entry-header -->
<?php endif; ?>
<?php if( $post->post_content ) : ?>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( [\'before\' => \'<div class="page-link"><span>\' . __( \'Pages:\' ) . \'</span>\', \'after\' => \'</div>\'] ); ?>
</div><!-- .entry-content -->
<footer class="entry-meta">
</footer><!-- .entry-meta -->
<?php endif; ?>
</article><!-- #post-<?php the_ID(); ?> -->
</div>
<?php endif; ?>
<?php
/**-----------------------------------------------------------------------------
*
* Start our custom query to display category posts
*
*------------------------------------------------------------------------------*/
$args = [
\'cat\' => $catid,
\'posts_per_page\' => $post_count,
\'paged\' => $paged,
\'orderby\' => $orderby,
\'order\' => $asc,
\'ignore_sticky_posts\' => 1,
];
$cat_query = new WP_Query($args);
// Output
if ( $cat_query->have_posts() ) {
// Start the Loop.
while ( $cat_query->have_posts() ) {
$cat_query->the_post();
get_template_part( \'content\', get_post_format() );
}
if ( function_exists( \'pietergoosen_pagination\' ) )
pietergoosen_pagination();
wp_reset_postdata();
} else {
get_template_part( \'content\', \'none\' );
}
?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
</div><!-- #main-content -->
<?php
get_footer();
我希望我正确地理解了你,希望这就是你所需要的