• I′m using this code to show of child subcategories of categorie 12. I′m using type plugin to show imagem from custom type. Ok. It runs ok. So if I click in one of this subcategories, It goes to a news page with posts of subcategory, but how can I do when you click in a image or name of this post, go to the post. Now, if I click in one of the post, goes to the same page.

    The link of childs of id=12:
    https://www.ashermedical.com/site/?cat=12

    if you click in “CIRURGIA GERAL”, you go to a page with all products/posts of this subcategory:

    https://www.ashermedical.com/site/?cat=22

    But how Can I do when you click in one of this products/posts, goes to it single post like:

    https://www.ashermedical.com/site/?p=283

    The code I′m using:

    <?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(12, $childcat)) {
    echo '<span class="cat-links"><h3 class="entry-title"><a href="'.get_category_link($childcat->cat_ID).'">';
     echo $childcat->cat_name .'</a></h3></span>';
     echo '<div id="thumb-category"><a href="'.get_category_link($childcat->cat_ID).'">';
     echo do_shortcode('[types field="imagem-1" alt='.($childcat->cat_ID).' title="'.get_category_link($childcat->cat_ID).'" size="thumbnail" align="none"]') . '</a><div>';
    }}
    ?>
    <?php endif; ?>

    Thanks a lot for any help!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Thread Starter carlosgonca

    (@carlosgonca)

    any help?

    I think where you have .get_category_link, you need to place the_permalink instead.

    Thread Starter carlosgonca

    (@carlosgonca)

    I tried this but I get error. I think I have to have three “if” the code that I put here, the second to do a loop to show the subcategory select, but with permalink to the post and other to show the single post, but I don′t know how.

    The code that you posted does not return posts permalinks and can not provide it. If you want to show post titles and links to each post in a category archive like this https://www.ashermedical.com/site/?cat=22 you need to place a WP loop in your archive.php or category.php template. This is the basics.
    What theme are you using? Each theme has a proper loop in archive.php. If you changed the theme code, check the original code.

    Simplest loop is

    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php endwhile; ?>
    <?php endif; ?>

    But, again, check the original code, because this is not enough.

    Thread Starter carlosgonca

    (@carlosgonca)

    I′m using twenty eleven like parent theme.

    I have a category.php file to start the loop so, goes to content-category.php

    In this second file I put the code

    <?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(12, $childcat)) {
    echo '<span class="cat-links"><h3 class="entry-title"><a href="'.get_category_link($childcat->cat_ID).'">';
     echo $childcat->cat_name .'</a></h3></span>';
     echo '<div id="thumb-category"><a href="'.get_category_link($childcat->cat_ID).'">';
     echo do_shortcode('[types field="imagem-1" alt='.($childcat->cat_ID).' title="'.get_category_link($childcat->cat_ID).'" size="thumbnail" align="none"]') . '</a><div>';
    }}
    ?>
    
    <?php endif; ?>

    And the subcategory are showing as I want, The title and a photo. But after this, I can′t only goes to subcategory page, not to a single post of one of them. My two files are in a zip for anyone want help me. Thanks a lot: https://www.ashermedical.com/site/asher-category.zip

    The content-category.php file is edited content-gallery.php template. This template for a displaying of single posts content in gallery format. It’s not suitable for category archives.
    Carlos, say to clarify, what is the task of your script?
    To always show in any category archive child categories (with thumbs) of category with ID 12? May be there is more proper ways to make it.

    Thread Starter carlosgonca

    (@carlosgonca)

    Yes, but when you click in one of these categories, you go to a page where can see all products about that categories. So if you click in one of them, you go to the single post of this product.

    Thread Starter carlosgonca

    (@carlosgonca)

    I want to show the subcategories with the name and a thumbnail, like I did. So I started with gallery option. After that if you click in one of this categories, you see all posts/products from this subcategories. And when you click in one of them, you go to the single post of this product.

    I think there is mistakes in content-category.php. It possible to make easier, but say,
    where, in what pages/archives you need to show subcategories with the name and a thumbnail? Is it any category archive, or category that has subcategories?
    And how it must be? E.g. after a header there is cat thumbs and a posts list below.

    You can place this code without <?php endif; ?> to categories.php after <div id="content" role="main">
    and wrap it, if you need, to divs for a styling.
    Also to change get_template_part( 'content', 'category' ); back to get_template_part( 'content', get_post_format() );

    In this case you will have cat thumbs and a posts list in any cat archive.

    Thread Starter carlosgonca

    (@carlosgonca)

    This solution didn′t run, It shows all posts with text and in a confusion way. I try to do something: after the code and before <?php endif; ?> I have to put other <?php else: ?> <if ($childcat)…. or some like that but all time I get error. It didn′t accept a new <?php else: ?>, but I′ve already have <?php else: ?> without problems, why one more I′ts not permitted?

    https://www.w3schools.com/php/php_if_else.asp
    https://php.net/manual/en/control-structures.elseif.php

    <?php if ( condition ) { ?>
    execute1
    <?php } elseif ( condition2 ) { ?>
    execute2
    <?php } else { ?>
    execute3
    <?php } ?>

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘show posts of a subcategories’ is closed to new replies.