• Resolved mad_griffith

    (@niccolomineo)


    I am trying to implement this example in a page store-page.php which has posts.

    1. Inside the loop, I am just calling <?php get_template_part( 'content', 'store-product' ); ?> and nothing else.
    2. Then I am assigning the page as a template to all the categories I want to turn into a store.
    3. Each post is placed in a specific category that matches the name of one page (because e.g. I want to display posts from “Courses” category in “Courses” page).

    Though, WP, instead of showing up articles, it shows the message as if no articles were there at all.

    Is there anything I forgot to do? How do you get the current category in a loop on a page?

    This is my code inside the store-page.php:

    <?php
    /**
    * Template Name: Store Page
     * The template for displaying the store page.
     *
     * 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 govpress
     */
    get_header(); ?>
    
    	<div id="primary" class="content-area">
    		<div id="main" class="site-main" role="main">
    
    			<?php
            /* The loop: the_post retrieves the content
             * of the new Page you created to list the posts,
             * e.g., an intro describing the posts shown listed on this Page..
             */
            if ( have_posts() ) :
                while ( have_posts() ) : the_post();
    
                  // Display content of page
                  get_template_part( 'content', get_post_format() );
                  wp_reset_postdata();
    
                endwhile;
            endif;
    ?>
    
    <?php if ( is_page() ) {
        $category = get_post_meta( $posts[0]->ID, 'category', true );
        $cat = get_cat_ID( $category );
    }
    
    if ( $cat ) :
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $post_per_page = 4; // -1 shows all posts
        $do_not_show_stickies = 1; // 0 to show stickies
        $args=array (
          'category__in' => array($cat)
    		);
    
    $temp = $wp_query; // assign original query to temp variable for later use
        global $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query( $args );
        if ( $wp_query->have_posts() ) :
            while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    
    				<?php get_template_part( 'content', 'store-product' ); ?>
    
    			<?php endwhile; ?>
    
    <?php endif; // if ( $wp_query->have_posts() ) ?>
    	<?php $wp_query = $temp; //reset back to original query ?>
    
    <?php else : ?>
    			<?php get_template_part( 'content', 'none' ); ?>
    		<?php endif; // if ( $cat ) ?>
    
    		</div><!-- #main -->
    	</div><!-- #primary -->
    
    <?php get_footer(); ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter mad_griffith

    (@niccolomineo)

    Ok, I will answer to my own question. I don’t know whether this is the most elegant/quicker code or not, but this is a working solution code for anybody in need. I commented to make it more understandable.
    N.B. The code only works if your page slugs have a matching category slug (a choice I went for in order to keep my taxonomy naming consistent).

    <?php
    				// gets the current page slug
    				global $page_id;
    				$page_slug = get_post( $page_id )->post_name;
    
    				// gets the current category slug by matching all the cats' slugs against the current page slug
    				$cat_args=array(
    					'hide_empty' => 0
    				);
    				$categories = get_categories($cat_args);
    				foreach ($categories as $category) {
    					$category_slug = $category->slug;
    					if ($category_slug == $page_slug) : ?>
    
    <?php
    				// uses the current category slug to query posts
    						$args = array(
    								'category_name' => $category_slug
    						);
    
    						$list_of_posts = new WP_Query( $args );
    
    						?>
    						<?php if ( $list_of_posts->have_posts() ) : ?>
    
    						<?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?>
    						<?php get_template_part( 'content', 'store-product' ); ?>
    						<?php endwhile; ?>
    
    						<?php else : ?>
    						<?php get_template_part( 'content', 'none' ); ?>
    						<?php endif; ?>
    
    					<?php endif; ?>
    			<?php	}; // foreach ?>
    Thread Starter mad_griffith

    (@niccolomineo)

    I am self-marking this as resolved

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Understanding how do pageofposts custom fields work’ is closed to new replies.