Hi Forum,
ich habe in meinem Blog eine statische Seite (Home) als Startseite angelegt, auf der ich jeweils die 5 letzten News-Einträge und die 5 letzten Projekte-Einträge anzeigen lassen will. Leider habe ich mit Tante Google nichts passendes dazu gefunden; vielleicht habe ich aber auch nur falsch gesucht.
WP-Version: 3.5.1
Theme: Twentytwelve
Plugins: Anti Spam Bee, Better WP Security
URL: http://dev.npire.de/
home.php
PHP
<?php
/**
* The template for displaying all pages.
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site will use a
* different template.
*
* @package WordPress
* @subpackage Twenty_Twelve
* @since Twenty Twelve 1.0
Template Name: Home
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Alles anzeigen
functions.php (gekürzt)
Code
//custom post type news erzeugen
add_action( 'init', 'register_cpt_news' );
function register_cpt_news() {
$labels = array(
'name' => _x( 'Alle Nachrichten', 'news' ),
'singular_name' => _x( 'Nachricht', 'news' ),
'add_new' => _x( 'Hinzufügen', 'news' ),
'add_new_item' => _x( 'Nachricht hinzufügen', 'news' ),
'edit_item' => _x( 'Nachricht bearbeiten', 'news' ),
'new_item' => _x( 'Neue Nachricht', 'news' ),
'view_item' => _x( 'Nachricht ansehen', 'news' ),
'search_items' => _x( 'Nachricht suchen', 'news' ),
'not_found' => _x( 'Nachricht nicht gefunden', 'news' ),
'not_found_in_trash' => _x( 'Keine Nachricht im Papierkorb gefunden', 'news' ),
'menu_name' => _x( 'Nachrichten', 'news' ),
'all_items' => _x( 'Alle Nachrichten', 'news' )
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => 'News auf der Zarthen Seite',
'supports' => array( 'custom-fields', 'title', 'editor', 'excerpt', 'thumbnail' ),
'taxonomies' => array( 'category', 'post_tag', 'page-category' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => false,
'capability_type' => 'post'
);
register_post_type( 'news', $args );
}
//custom post type projekte erzeugen
add_action( 'init', 'register_cpt_projekte' );
function register_cpt_projekte() {
$labels = array(
'name' => _x( 'Alle Projekte', 'projekte' ),
'singular_name' => _x( 'Projekt', 'projekte' ),
'add_new' => _x( 'Hinzufügen', 'projekte' ),
'add_new_item' => _x( 'Projekt hinzufügen', 'projekte' ),
'edit_item' => _x( 'Projekt bearbeiten', 'projekte' ),
'new_item' => _x( 'Neues Projekt', 'projekte' ),
'view_item' => _x( 'Projekt ansehen', 'projekte' ),
'search_items' => _x( 'Projekt suchen', 'projekte' ),
'not_found' => _x( 'Projekt nicht gefunden', 'projekte' ),
'not_found_in_trash' => _x( 'Kein Projekt im Papierkorb gefunden', 'projekte' ),
'menu_name' => _x( 'Projekte', 'projekte' ),
'all_items' => _x( 'Alle Projekte', 'projekte' )
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => 'Projekte der Zarthen Seite',
'supports' => array( 'custom-fields', 'title', 'editor', 'excerpt', 'thumbnail' ),
'taxonomies' => array( 'category', 'post_tag', 'page-category' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => false,
'capability_type' => 'post'
);
register_post_type( 'projekte', $args );
register_taxonomy( 'skills', 'projects',
array(
'hierarchical' => false,
'label' => __('Skills'),
'query_var' => 'skills',
'rewrite' => array('slug' => 'skills' )
)
)
}
$news_structure = '/news/%news%';
$wp_rewrite->add_rewrite_tag("%news%", '([^/]+)', "news=");
$wp_rewrite->add_permastruct('news', $news_structure, true);
add_rewrite_rule('news/page/([0-9]*)/?', 'index.php?page_id=PAGE_ID&paged=$matches[1]', 'top');
$projekte_structure = '/projekte/%projekte%';
$wp_rewrite->add_rewrite_tag("%projekte%", '([^/]+)', "projekte=");
$wp_rewrite->add_permastruct('projekte', $projekte_structure, true);
add_rewrite_rule('projekte/page/([0-9]*)/?', 'index.php?page_id=PAGE_ID&paged=$matches[1]', 'top');
?>
Alles anzeigen
news.php
PHP
<?php
/*
Template Name: News
*/
get_header();
?>
<div class="news">
<h2>News</h2>
<?php $wp_query = new WP_Query( array( 'post_type' => 'news', 'paged' => $paged ) ); ?>
<?php if ($wp_query->have_posts()) : ?>
<div class="news-box">
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php
global $more;
$more = 0;
?>
<div id="post-<?php the_ID(); ?>" class="news-thumbnail">
<a href="<?php the_permalink(); ?>"><?php if(has_post_thumbnail()){ the_post_thumbnail('thumbnail'); } ?>
<div class="title"><?php the_title(); ?></div></a>
<div class="content"><?php the_content('weiterlesen'); ?></div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Alles anzeigen
projekte.php
PHP
<?php
/*
Template Name: Projekte
*/
get_header();
?>
<div class="projekte">
<h2>Projekte</h2>
<?php $wp_query = new WP_Query( array( 'post_type' => 'projekte', 'paged' => $paged ) ); ?>
<?php if ($wp_query->have_posts()) : ?>
<div class="projekte-box">
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php
global $more;
$more = 0;
?>
<div id="post-<?php the_ID(); ?>" class="projekte-thumbnail">
<a href="<?php the_permalink(); ?>"><?php if(has_post_thumbnail()){ the_post_thumbnail('thumbnail'); } ?>
<div class="title"><?php the_title(); ?></div></a>
<div class="content"><?php the_content('weiterlesen'); ?></div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Alles anzeigen
Wie bekomme ich die jeweils fünf letzten Einträge von News und Projekte auf die Home-Seite?