问题
I have two separate pages showing a list of different categories linking to the full post in wordpress. These both currently open on single.php in my theme but The categories need to be styled differently on each page.
I have created the template page but do not know how to open posts on another page other than single.php
to simplify: how do I open posts on another version of single.php
the code I have that opens the full posts are:
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
?>
<div class="serve-inner-split">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
<a href="#" class="header-quote">READ CASE STUDY</a>
</div>
</div>
<div id="case-right" class="serve-grey">
<?php
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
</div>
</div>
<?php endforeach;
}
}
?>
回答1:
Hello :) I would achieve this by creating custom post type for each case you need to display the single post differently. You may refer to WP Codex https://codex.wordpress.org/Post_Types on this case. Eventually it will allow you to create as much single post templates as you want.
回答2:
I'd create extra category fields. In functions.php
add
add_action ( 'edit_category_form_fields', 'mytheme_extra_category_fields');
add_action ( 'category_add_form_fields', 'mytheme_extra_add_category_fields');
if ( ! function_exists( 'mytheme_extra_category_fields' ) ){
function mytheme_extra_category_fields( $tag ) {
$t_id = $tag->term_id;
$cat_meta = get_option( "category_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="extra1"><?php esc_attr_e('Blog Layout', 'mytheme'); ?></label></th>
<td>
<select name="Cat_meta[blog_layout]">
<?php
echo '<option value="layout1" '.selected( $cat_meta['blog_layout'], 'layout1', false).'>'.esc_html__('Layout 1', 'mytheme').'</option> ';
echo '<option value="layout2" '.selected( $cat_meta['blog_layout'], 'layout2', false).'>'.esc_html__('Layout 2', 'mytheme').'</option> ';
?>
</select>
</td>
</tr>
<?php
}
}
if ( ! function_exists( 'mytheme_extra_add_category_fields' ) ){
function mytheme_extra_add_category_fields( $tag ) {
$t_id = (is_object($tag))?$tag->term_id:'';
$cat_meta = get_option( "category_$t_id");
?>
<div class="form-field">
<label for="extra1"><?php esc_attr_e('Blog Layout', 'mytheme'); ?></label></th>
<select name="Cat_meta[blog_layout]">
<?php
echo '<option value="layout1" '.selected( $cat_meta['blog_layout'], 'layout1', false).'>'.esc_html__('Layout 1', 'mytheme').'</option> ';
echo '<option value="layout2" '.selected( $cat_meta['blog_layout'], 'layout2', false).'>'.esc_html__('Layout 2', 'mytheme').'</option> ';
?>
</select>
</div>
<?php
}
}
add_action ( 'edited_category', 'mytheme_save_extra_category_fileds');
add_action ( 'created_category', 'mytheme_save_extra_category_fileds');
if ( ! function_exists( 'mytheme_save_extra_category_fileds' ) ){
function mytheme_save_extra_category_fileds( $term_id ) {
if ( isset( $_POST['Cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "category_$t_id");
$cat_keys = array_keys($_POST['Cat_meta']);
foreach ($cat_keys as $key){
if(isset($_POST['Cat_meta'][$key])){
$cat_meta[$key] = $_POST['Cat_meta'][$key];
}
}
update_option( "category_$t_id", $cat_meta );
}
}
}
This should output the select 'Blog layout' when you add categories to your posts. Then in your index.php
add
$cat_id = get_query_var('cat');
$cat_data = get_option("category_$cat_id");
<?php if(isset($cat_data['blog_layout']) && $cat_data['blog_layout'] == 'layout1'): ?>
\\Your layout1 here
<?php else: ?>
\\Your layout2 here
<?php endif; ?>
This should work.
回答3:
To fix this issue I replaces the entire single.php file with this code:
<?php
if (in_category('2')) {include (TEMPLATEPATH . '/page-case-study-post.php');
}
else { include (TEMPLATEPATH . '/page-services-post.php');
}
?>;
sending either categories to two different pages depending on what its set to.
来源:https://stackoverflow.com/questions/33546585/open-wordpress-post-in-a-different-page