0
votes

PHP noob here - all round web noob tbh. Anyhoo been trying to get this going for a while now.

The site is http://talkativebroadcasting.co.uk

Basically have a page of posts called "Posts" - thats working fine

Have a "Talkative Blog" page where I only want posts with category of "BLOG" posted - works fine-ish

Have a sub page of "BTCC" called interviews where I only want pages of category "BTCC" or any subcategory of BTCC post posted - again ok-ish

Current code in Page.php is

<?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
 */

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 comments_template( '', true ); ?>

<?php
    if (is_page('talkative-blog')) {
        query_posts('category_name=BLOG');
    } elseif (is_page(17)) {
        query_posts('category_name=BTCC');
    }
?>

            <?php endwhile; // end of the loop. ?>


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

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Main issues still present:

  • Featured images are no longer present, neither are tags etc.
  • When say 10 posts are added you cannot go older/newer as like on the "Posts" page
  • In fact it would be perfect if it operated the same as the "Posts" page which will eventually be hidden and just operate behind the scenes

Thanks

2
hi SAM, your if else condition must be define before your while loop like to define blow code. - Ravi Patel

2 Answers

0
votes

In order to only display posts from a single category (or subcategory), you do not need to write any code on page.php file of wordpress. If you want to modify the way posts or list of posts work, on twenty twelve theme, check the file category.php.

In order to display the posts of the wanted category you only have to call the category's link. For example, in order to show all the posts on BTCC category, just use this url: http://talkativebroadcasting.co.uk/category/BTCC/

In the same way, you can show all categories you need.

0
votes

your define code must be use before while loop:

<?php
    if (is_page('talkative-blog')) {
        query_posts('category_name=BLOG');
    } elseif (is_page(17)) {
        query_posts('category_name=BTCC');
    }
?>
<?php while ( have_posts() ) : the_post(); ?>



<?php
        $image_id = get_post_thumbnail_id(); 
        $image_url = wp_get_attachment_image_src($image_id,'thumbnail');   
   ?>

<?php if ( has_post_thumbnail() ) { ?>
    <a href="<?php the_permalink(); ?>"><img src="<?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" /></a>
<?php } ?>



<?php endwhile; // end of the loop. ?>

http://codex.wordpress.org/Function_Reference/query_posts

enter image description here