• Denki

    (@denki-denise)


    Category page is reload wrong template:
    Eng-:
    Hi! Thank you for your attention and help. I am creating my first WordPress Theme and I want to show two categories on different pages. I have in each page a layout of two asymmetrical parts one with the content and the other with the titles and permalinks of all posts in that category. When loading the category (category template) the loop filters well. But when I access another post in the list reloads the entire page and loads what I have in “content.php”. I think maybe I have problems calling the “content.php”. How could you do to get only the content (maybe with ‘get template part’) and to continue filtering the result of the loop with the same parameters, keeping this list of post filtered by category?
    Create two content in a “frontcategory /” subfolder: “content-1.php” and “content-2.php”
    I think it would be an option but I do not know how to apply it

    ——————————————————————-

    Esp-:
    ¡Hola! Desde ya agradezco su atención y ayuda. Estoy creando mi primer WordPress Theme y quiero mostrar dos categorías en diferentes páginas. Tengo en cada una de páginas un layout de dos partes asimétricas una con el contenido y la otra con los títulos y el permalinks de todos los post de esa categoría. Al momento de cargar la categoría (category template) el loop filtra bien. Pero cuando accedo a otro post de la lista se recarga toda la página y carga lo que tengo en “content.php” . Pienso que tal vez tengo problemas llamando al “content.php”. Como podria hacer para obtener solo el “content” (tal vez con ‘get template part’) y que se siga filtrando el resultado del loop con los mismo parámetros, manteniendo esta lista de post filtrados por categoría ?
    Cree dos content en una subcarpeta “frontcategory/” : “content-1.php” y “content-2.php”
    creo que seria una opción pero no se como aplicarla

    category-1.php
    <div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
           <div class="left-side blog">
            <article>
    <?php
    
    $args = array( 'posts_per_page' => 1, 'offset'=> 0, 'category' => 3, 'order'=> 'ASC');
    
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post ); 
    		the_title( '<h1 class="entry-title">', '</h1>' );
    		
    				$dias = array("Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sábado");
    $meses = array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
    echo "<span class='date-post'>(".$dias[get_the_date('w')]." ".get_the_date('d')."/".$meses[get_the_date('n')-1]. "/".get_the_date('Y').")</span>" ;	
    
    	the_content('<div class="entry-content">','</div>');
    	
    	
    	
    	?>
    <?php endforeach; 
    
    /*wp_reset_postdata();*/?>
    			
    
       </article>
    		</div>
    		<div class="mid-side blog">
    <h2 class="title-category"> Blog</h2>
    
    
      <?php $args = array( 'posts_per_page' => -1, 'offset'=> 0, 'category' => 3, 'order'=> 'ASC'); $myposts = get_posts( $args ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    • "><?php the_title(); ?>
    • <?php endforeach; wp_reset_postdata();?>
    </div> <?php get_footer(); ?>
    • This topic was modified 9 years, 3 months ago by Denki.
    • This topic was modified 9 years, 3 months ago by Denki.
    • This topic was modified 9 years, 3 months ago by Denki.
    • This topic was modified 9 years, 3 months ago by Jan Dembowski.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You have offset hardcoded as 0, so the content will always be from the first post returned no matter which title is clicked. Each title link should link to the same page, but also includes an URL parameter for each posts’s offset into the results. When the page loads, use the URL parameter to set the offset value in the content query. If there is no URL parameter, then you may use 0.

    A really nice improvement would be to use AJAX to load the content so the entire page does not need to reload. This would increase the complexity of your code though.

    Thread Starter Denki

    (@denki-denise)

    Thanks for your answer, first of all

    Modify my category template (before seeing your answer):

    Category-1.php is composed of: “get-template-part (tempparts / content, contcat1);” + “Get-template-part (tempparts / content, navcat1);”

    Category-2.php is composed of: “get-template-part (tempparts / content, contcat2);” + “Get-template-part (tempparts / content, navcat2);”

    Contcat-id = to content
    Navcat-id = the post list for that category

    In this case when I click on any link it takes me to the respective post but loaded from the template “content.php”, which does not have the post list of the category.
    Site.com/category/category-1.php —–> click on post link —-> site.com/post-id.php (another template, “content.php”)

    Could you help me with some example to be able to see its operation?
    How can I give the parameter and reload in the same template?

    Moderator bcworkz

    (@bcworkz)

    You need to append an URL parameter (AKA query argument) to all the permalinks. You can use add_query_arg() to do this. The result should be something like example.com/category/my-category/?offset=3

    To know what the offset is you need to maintain a loop counter that increments each time the loop code runs. In your first query arguments where you have ‘posts_per_page’ => 1, ‘offset’=> 0, replace the 0 with the value from $_GET[‘offset’]. Before getting the value, check that an ‘offset’ key exists in $_GET with array_key_exists(). If not, the offset value should be 0.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Category page is reload wrong template’ is closed to new replies.