如何在wordpress中显示特定页面中特定类别的帖子

时间:2014-05-12 05:39:10

标签: php wordpress

我在导航中有不同的页面,我希望将帖子显示在类别中。 例如,我有一个页面最新消息,并已做了类别新的更新,然后我怎样才能使它工作..当我选择新闻更新时,类别应显示在最新的新闻页面。所有其他页面和类别也是如此

3 个答案:

答案 0 :(得分:2)

我更喜欢这个Posts for Page插件。用于在特定类别的页面上显示帖子非常简单。

在页面上使用此短代码

[posts-for-page cat_slug='your_specific_cat' hide_images='false' num='5' read_more='
Read More »']

答案 1 :(得分:2)

您可以使用List category posts插件。它允许使用短代码,您可以使用以下短代码显示特定类别的帖子:

[catlist id=1 numberposts=10]

其中 id 是类别ID,数字信息表示您希望在页面/帖子上显示的帖子数。

您还可以将输出包装在HTML标记中,并根据需要使用CSS对其进行样式设置。

答案 2 :(得分:1)

我从二十一岁开始使用这个模板。我必须在这里得到digitalraindrops的精彩教程。我已经重写了这个以满足我的需求,目前它正在编写二十四个主题。

您可以通过上面给出的链接查看教程。我喜欢这个模板的是它可以重复使用一千次。该模板的功能是,您可以从下拉列表中选择一个类别。此类别的帖子将显示在该页面上。这是我使用的代码

首先,所有元函数包括每页帖子,订单,页面标题,帖子标题等选项。此代码将包含在您的functions.php或自定义函数文件中

<?php
/**-----------------------------------------------------------------------------
 *
 *Add a post metabox with options to the admin page screen.
 *After selcting the page-pop.php template as a page template,
 *this metabox will appear in the admin page screen.
 *From here you can choose which category's posts to display
 *and how the posts will be displayed on the page
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 *
*------------------------------------------------------------------------------*/

/**-----------------------------------------------------------------------------
 *
 *1. Only add meta boxes for the pop template
 *
 * @since Twenty Fourteen 1.0
 *
*------------------------------------------------------------------------------*/
add_action('admin_init', 'pietergoosen_add_pop_meta_box');

function pietergoosen_add_pop_meta_box(){
$post_id = isset( $_GET['post'] ) ? $_GET['post'] : 0 ;
if($post_id) { 
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file == 'page-pop.php') { 
add_meta_box('pop_meta_box', __( 'Page of Posts with the same name', 'pietergoosen' ), 'pietergoosen_pop_meta_options', 'page', 'side', 'core');
} else {
$meta = get_post_meta($post_id, '_cat_id', true);
if( $meta ) {
pietergoosen_pop_update_post_meta($post_id, '_cat_id', '');
pietergoosen_pop_update_post_meta($post_id, '_page_title', '');
pietergoosen_pop_update_post_meta($post_id, '_posts_title', '');
pietergoosen_pop_update_post_meta($post_id, '_order_by', '');
pietergoosen_pop_update_post_meta($post_id, '_asc', '');
pietergoosen_pop_update_post_meta($post_id, '_post_count', '');
pietergoosen_pop_update_post_meta($post_id, '_days', '');
remove_meta_box( 'pop_meta_box', 'page', 'side' );
}
}
}
add_action('save_post', 'pietergoosen_pop_update_post_meta_box');
}

/**-----------------------------------------------------------------------------
 *
 *2. Built the list to display the options in the metabox
 *
 * @since Twenty Fourteen 1.0
 *
*------------------------------------------------------------------------------*/
$order_list = array(
'none' => array( 'value' => 'none','label' => 'None' ),
'id' => array( 'value' => 'ID','label' => 'Post ID' ),
'author' => array( 'value' => 'author','label' => 'Author' ),
'title' => array( 'value' => 'title','label' => 'Post Title' ),
'date' => array( 'value' => 'date', 'label' => 'Post Date'),
'modified' => array( 'value' => 'modified','label' => 'Modified Date' ),
'parent' => array( 'value' => 'parent','label' => 'Parent Post' ),
'rand' => array( 'value' => 'rand','label' => 'Random' ),
'comment_count' => array( 'value' => 'comment_count','label' => 'Comment Count' ),
'menu_order' => array( 'value' => 'menu_order','label' => 'Menu Order' ),
);

$sort = array(
'DESC' => array( 'value' => 'DESC','label' => 'Descending' ),
'ASC' => array( 'value' => 'ASC','label' => 'Ascending' ),
); 

function pietergoosen_pop_meta_options(){

$post_id = !empty($_GET['post']) ? $_GET['post'] : 0;
if( !$post_id ) return;

$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file != 'page-pop.php') return;

global $order_list,$post_styles,$sort;
$categories = get_categories();

//Check if we have values
$post_meta=array();
$post_meta = get_post_meta( $post_id,false );

$cat_id = isset( $post_meta['_cat_id'] ) ? $post_meta['_cat_id'][0] : 1;
$page_title = isset( $post_meta['_page_title'] ) && $post_meta['_page_title'] ? $post_meta['_page_title'][0] : '';
$posts_title = isset( $post_meta['_posts_title'] ) && $post_meta['_posts_title'] ? $post_meta['_posts_title'][0] : '';
$order_by = 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');
if(!$post_count || !is_numeric( $post_count )) $post_count = get_option('posts_per_page');
$days = isset( $post_meta['_days'] ) ? $post_meta['_days'][0] : '0';
if($days && !is_numeric( $days )) $days = '0';
?>

<!-- Sart the meta boxes -->
<div class="inside">
<p><label><strong><?php _e( 'Page Title', 'pietergoosen' ); ?></strong></label></p>
<input id="_posts_title" name="_posts_title" type="text" style="width: 98%;" value="<?php echo $posts_title; ?>"/>

<p><label><strong><?php _e( 'Post Title', 'pietergoosen' ); ?></strong></label></p>
<input id="_page_title" name="_page_title" type="text" style="width: 98%;" value="<?php echo $page_title; ?>"/>

<p><label><strong><?php _e( 'Category', 'pietergoosen' ); ?></strong></label></p>
<select id="_cat_id" name="_cat_id">
<?php 
//Category List
foreach ($categories as $cat) :
$selected = ( $cat->cat_ID == $cat_id ) ? ' selected = "selected" ' : '';
$option = '<option '.$selected .'value="' . $cat->cat_ID;
$option = $option .'">';
$option = $option .$cat->cat_name;
$option = $option .'</option>';
echo $option;
endforeach;
?>
</select>

<p><label><strong><?php _e( 'Sort by', 'pietergoosen' )?></strong></label></p>
<select id="_order_by" name="_order_by">
<?php 
foreach ($order_list as $output) :
$selected = ( $output['value'] == $order_by ) ? ' selected = "selected" ' : '';
$option = '<option '.$selected .'value="' . $output['value'];
$option = $option .'">';
$option = $option .$output['label'];
$option = $option .'</option>';
echo $option;
endforeach;
?>
</select>

<p><label><strong><?php _e( 'Order', 'pietergoosen' )?><strong></label></p>
<select id="_asc" name="_asc">
<?php 
foreach ($sort as $output) :
$selected = ( $output['value'] == $asc ) ? ' selected = "selected" ' : '';
$option = '<option '.$selected .'value="' . $output['value'];
$option = $option .'">';
$option = $option .$output['label'];
$option = $option .'</option>';
echo $option;
endforeach;
?>
</select>

<p><strong><label><?php _e( 'Posts per Page', 'pageofposts' ); ?><strong></label></p>
<input id="_post_count" name="_post_count" type="text" value="<?php echo $post_count; ?>" size="3" />

<p><strong><label><?php _e( 'Posts in the last days', 'pageofposts' ); ?><strong></label></p>
<input id="_days" name="_days" type="text" value="<?php echo $days; ?>" size="3" />
</div>
<!-- End page of posts meta box -->
<?php
}
function pietergoosen_pop_update_post_meta_box( $post_id ){

if ( empty( $_POST ) ) {
return;
} else {
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file != 'page-pop.php') return;

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
} else {
if ( $_POST['post_type'] == 'page' ) {
if ( !current_user_can( 'edit_page', $post_id ) )
  return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
  return $post_id;
}
$meta = isset( $_POST['_cat_id'] ) ? $_POST['_cat_id'] : 1;
pietergoosen_pop_update_post_meta($post_id, '_cat_id', $meta);
$meta = isset( $_POST['_page_title'] ) ? $_POST['_page_title'] : '';
pietergoosen_pop_update_post_meta($post_id, '_page_title', $meta);
$meta = isset( $_POST['_posts_title'] ) ? $_POST['_posts_title'] : '';
pietergoosen_pop_update_post_meta($post_id, '_posts_title', $meta);
$meta = isset( $_POST['_order_by'] ) ? $_POST['_order_by'] : 'date';
pietergoosen_pop_update_post_meta($post_id, '_order_by', $meta);
$meta = isset( $_POST['_asc'] ) ? $_POST['_asc'] : 'DESC';
pietergoosen_pop_update_post_meta($post_id, '_asc', $meta);
$meta = isset( $_POST['_post_count'] ) ? $_POST['_post_count'] : get_option('posts_per_page');
pietergoosen_pop_update_post_meta($post_id, '_post_count', $meta);
$meta = isset( $_POST['_days'] ) ? $_POST['_days'] : 0;
pietergoosen_pop_update_post_meta($post_id, '_days', $meta);
return;
}
}
}

function pietergoosen_pop_update_post_meta($post_id, $key, $data) {
$post_meta = get_post_meta($post_id, $key, true);
if( $data != '' && $post_meta != $data) {
update_post_meta($post_id, $key, $data);
} elseif ( $post_meta != '' && $data == '' ) {
delete_post_meta($post_id, $key);
}
}
?>

其次,页面模板。您必须调用此模板page-pop.php

<?php
/**
 * Template Name: Page of Posts
 */
get_header(); ?>

<?php
//See if we have any values
$post_meta=array();
$post_meta = get_post_meta( $post->ID,false );
$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';
$list_style =  isset( $post_meta['_list_style'] ) ? $post_meta['_list_style'][0] : 'default';
$post_count = isset( $post_meta['_post_count'] ) ? $post_meta['_post_count'][0] : get_option('posts_per_page');
if(!$post_count || !is_numeric( $post_count )) $post_count = get_option('posts_per_page');
$days = isset( $post_meta['_days'] ) ? $post_meta['_days'][0] : 0;
if($days && !is_numeric( $days )) $days = 0;
$do_not_show_stickies = ($list_style == 'default') ? 0 : 1;
?>
<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( $posts_title ) : ?>
<article id="posts-title">
<header class="entry-header">
<h2 class="entry-title"><?php echo $posts_title; ?></h2>
</header><!-- .entry-header -->
</article><!-- #posts-title -->
<?php endif; ?>

<?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( $page_title ) : ?>
<header class="entry-header">
<h1 class="entry-title"><?php echo $page_title; ?></h1>
</header><!-- .entry-header -->

<?php endif; ?>
<?php if( $post->post_content ) : ?>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'pietergoosen' ) . '</span>', 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<footer class="entry-meta">

</footer><!-- .entry-meta -->
<?php endif; ?>
</article><!-- #post-<?php the_ID(); ?> -->
</div>
<?php endif; ?>

<?php 
/* Do we have any category */
global $post;

// Save posts for later use
$tmp_post = $post;

$args = array( 
'cat' => $catid,
'posts_per_page' => $post_count,
'paged' => $paged,
'orderby' => $orderby,
'order' => $asc,
'ignore_sticky_posts' => $do_not_show_stickies,
);

if( $days ) {
function pop_filter_where( $where = '') {
global $days;
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-' .$days .' days')) . "'";
return $where;
}
add_filter( 'posts_where', 'pop_filter_where' );
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query( $args );
remove_filter( 'posts_where', 'pop_filter_where' );
} else {
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query( $args );
}
// Output
if ( $wp_query->have_posts() ) :

// Start the Loop.
while ( $wp_query->have_posts() ) : $wp_query->the_post();

 get_template_part( 'content', get_post_format() ); 

endwhile;

pietergoosen_pagination();

else : 

get_template_part( 'content', 'none' );

endif; ?>

<?php 
// Reset the post to the page post
$post = $tmp_post; 
?>

    <?php if ( comments_open() || get_comments_number() ) {
comments_template();
} ?>

</div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar( 'content' ); ?>

</div><!-- #main-content -->

<?php
get_footer();

您现在可以简单地创建一个新页面并选择“帖子页面”模板,然后发布您的页面。完成后,将显示“帖子”页面元框。从中您可以选择要在该页面上显示的类别。希望这可以帮助你