• Hello, i have made two custom page templates.The first is an Archive page that displays the posts and the categories of the posts.Here is the code:

    <?php
    /**
    * Template Name: Archive
    */
    /*Use Bootstramp*/
    get_header(); ?>
    
    <div class="CategoryMenu">
    <?php
    $args = array(
      'orderby' => 'name',
      'parent' => 0
      );
    $categories = get_categories( $args );
    foreach ( $categories as $category ) {
    	echo '<a class="CategoryArchive"  href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br/>';
    }
    ?>
    </div>
    
    <div class="container">
    <div class="row">
    
    <div id="PostData">
    <?php
    
    $theposts = get_posts(array('posts_per_page' => -1));
    
    foreach ( $theposts as $post ) :
      setup_postdata( $post );
    echo '<div class="ArchiveThumb">';
      echo get_the_post_thumbnail($post_id,'medium');
    echo '</div>';
      ?>
    
    	<p class="PostName"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
    
    <?php endforeach;
    ?>
    
    </div>
    </div>
    </div>
    
    <?php
    
    get_footer();
    ?>

    And a second page template that i want to display a certain category of post for example awards.Here is the code for the second page:

    <?php
        /**
        * Template Name: Awards
        */
        get_header(); ?>
    
        <div class=InfoMenu>
            <?php if(!$post->post_parent){
        $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
        }else{
        if($post->ancestors)
        {
        $ancestors = end($post->ancestors);
        $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
        }
        }
        if ($children) {
        ?>
        <?php echo '<div class="InfoPage">' .$children. '</div>'; ?>
        <?php } ?>
        </div>
    
        <?php global $post; // required
        $args = array('category' => 1); // include category 9
        $custom_posts = get_posts($args);
        foreach($custom_posts as $post) : setup_postdata($post);
    
        // put here what you want to appear for each post like:
        //the title:
        the_title();
    
        // an excerpt:
        the_excerpt();
    
        //and so on...
    
        endforeach;
        ?>
    
        <?php
    
        get_footer();
        ?>

    The problem is that i want to exclude the category from the archive page, and display the posts from the category in the second page.

Viewing 10 replies - 16 through 25 (of 25 total)
  • Thread Starter phaidonas

    (@phaidonas)

    Here is the hierarchy of the theme:

    Hierarchy1:
    Hierarchy1

    Hierarchy2:
    Hierarchy 2

    And here is the post content used from single.php:
    Post,single.php

    and here is an example mock up how i want to be seen the posts in the news category:Example post content

    Thank you.

    Shariq Khan

    (@shariqkhan2012)

    There are too many templates in your theme directory. Are you sure you are doing things correctly?
    Lets start afresh. Tell from the beginning what you are trying to accomplish and what you have achived so far. What has worked or you and what has not. Where are you facing the issues. Please be as detailed as possible.

    And here is the post content used from single.php:
    Post,single.php

    Is that a post or a page. What is the url?

    and here is an example mock up how i want to be seen the posts in the news category:Example post content

    Again, what is the url?

    Also what is the url of the archives?

    Also it would be a very good idea to install this plugin

    It shows which template is being used by WordPress to show the current page or post or archive.

    Open different links (posts, pages, archives) -especially the ones for which you have attached the screenshot in your previous reply- and see which template they are using and list it here.
    Then check whether the template being used is actually the one you are expecting to

    Lastly it would be very helpful if you could post the url to your site.
    If it is on localhost, then it is a different matter

    Thread Starter phaidonas

    (@phaidonas)

    I have this plug in, the url is post, i know i have to many templates,and i could be more optimal if i used archive-{myslug}.php but i started it like this, and now i find difficult to change it.(When i fulfill the site i will try to change it. )Both urls are posts,displaying from single.php the second i havent made it yet, but it is also a post with different css.

    Shariq Khan

    (@shariqkhan2012)

    Okay. Now if I understand correctly, the second one too being a single post, will be using single.php. Its just that you want it to be displayed differently than other single posts. Correct?

    Well, this could be easy. But for that please post the code of single.php here

    Thread Starter phaidonas

    (@phaidonas)

    here is the code of single.php:

    <?php
    /**
     * The template for displaying all single posts
     *
     * @link https://developer.ww.wp.xz.cn/themes/basics/template-hierarchy/#single-post
     *
     * @package South_Arch
     */
    
    get_header(); ?>
    
    	<div id="primary" class="content-area">
    		<main id="main" class="site-main" role="main">
    
    		<?php
    		while ( have_posts() ) : the_post();
    
    			get_template_part( 'template-parts/content', get_post_format() );
    
    		endwhile; // End of the loop.
    		?>
    
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <?php
    get_footer();

    and here is for content.php:

    <?php
    /**
     * Template part for displaying posts
     *
     * @link https://codex.ww.wp.xz.cn/Template_Hierarchy
     *
     * @package South_Arch
     */
    
    ?>
    
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    	<div class="PostContent">
    	<header class="entry-header">
    		<?php
    		if ( is_single() ) :
    			the_title( '<div class="entry-title">', '</div>' );
    		else :
    			the_title( '<div class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></div>' );
    		endif;
    
    		if ( 'post' === get_post_type() ) : ?>
    		<div class="entry-meta">
    
    		</div><!-- .entry-meta -->
    		<?php
    		endif; ?>
    	</header><!-- .entry-header -->
    </div>
    	<div class="entry-content">
    
    		<?php
    			the_content( sprintf(
    				/* translators: %s: Name of current post. */
    				wp_kses( __( 'Continue reading %s <span class="meta-nav">&rarr;</span>', 'south-arch' ), array( 'span' => array( 'class' => array() ) ) ),
    				the_title( '<span class="screen-reader-text">"', '"</span>', false )
    			) );
    
    			wp_link_pages( array(
    				'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'south-arch' ),
    				'after'  => '</div>',
    			) );
    		?>
    
    	</div><!-- .entry-content -->
    
    </article><!-- #post-## -->
    
    Shariq Khan

    (@shariqkhan2012)

    One way would be to edit single.php and change the line

    get_template_part( 'template-parts/content', get_post_format() );

    to something like this:

    if(is_single() && has_category('news')):
          get_template_part( 'template-parts/content-news', get_post_format() );
    else:
          get_template_part( 'template-parts/content', get_post_format() );
    endif;

    Then you create the file content-news.php and write the code to display the post in news category on whatever way you want

    • This reply was modified 9 years, 1 month ago by Shariq Khan.
    • This reply was modified 9 years, 1 month ago by Shariq Khan. Reason: Adding the closing bracket for the if condition
    • This reply was modified 9 years, 1 month ago by Shariq Khan.
    Thread Starter phaidonas

    (@phaidonas)

    It is also the same with post types?because I search it,a little complicated, declare the post types, assign them and then use them with single-{post-type }.php

    Shariq Khan

    (@shariqkhan2012)

    That is another question.
    First let us know whether the above solution worked for your problem

    Thread Starter phaidonas

    (@phaidonas)

    Yeah it works! Thank you very much.

    Shariq Khan

    (@shariqkhan2012)

    Good.
    Hope you have better understanding of templating system now.
    If your problem is still not solved and you have further questions related to this, please post them here.
    If they are not related, please post them as a separate topic.

Viewing 10 replies - 16 through 25 (of 25 total)

The topic ‘Posts by catecory’ is closed to new replies.