• Hi there,

    Was just wondering if anyone knows how to call a parent-tag using PHP? I would like it to appear on a project page above the project content. Although I don’t want all the tags of the project to appear, only the parent – if that’s possible?

    I tried adding this:

    <h1><?php single_tag_title( '', true ); ?></h1>

    But it didn’t work ??

    Hope someone can help.

    Thanks!

    Jonas

Viewing 6 replies - 1 through 6 (of 6 total)
  • Is the ‘child’ to the project ‘parent’ you’re referring to a post type or a tag?

    if term:

    get_terms() $args set child_of = parent term_id

    if post type:

    “don’t want all the tags of the project to appear, only the parent”

    i would recommend gathering all the terms, again with wp_get_object_terms(), and finding if the terms have a parent, if they do, don’t show the term, if they don’t show it.

    $terms = wp_get_object_terms( ... );
    foreach ($terms as $term) {
        if ($term->parent == 0)
           echo "<h1> {$term->name} </h1>";
    }

    that’s off of the top of my head, you may have to print_r($term) to see if “$term->parent” is actually where the parent term info is stored.

    Thread Starter skafte

    (@skafte)

    Thanks David for your quick response.

    I’ve tried the different codes you gave me, however, unfortunately I didn’t get to them work…?

    I basically just want the ‘Politik’ heading (which is a portfolio tag) from this page: https://theme.alternativet.dk/politik/ (password: alternative) to also appear above the project title on all projects within this section (those tagged ‘politik’), and similarly in all other sections. So, above the title, and underneath ‘? FORSIDE’ and ‘? TILBAGE’ on an open project like this one: example. However, what might be tricky to do is to make sure that the projects don’t show all the other tags they’ve also got; for example, a project will be tagged with at least two tags: ‘news’ (so that it will appear on the front page) and, for example, ‘Politik’ so that it will appear in the politics section. Adding the tag above the title will make it clear to the reader on which section he/she is, and, furthermore, be a link so that it’s easy to get back to the portfolio view with all the other projects of the same kind (e.g. those tagged ‘politik’, ‘inspiration’ or ‘events’).

    Do you know if this possible, and if so, how?

    Hope my explanation makes sense? If not I am happy to try elaborate some more.

    Thanks a lot for your help! I am really struggling with this one :-/

    Cheers,

    Jonas

    so what you’ll want to do it hook into the_title. Granted your theme was written correctly, the_title hook you’re able to add in whatever text you want around the title

    add_action('the_title','add_tag_to_title');
    
    function add_tag_to_title($title) {
        global $wp_query;
    
        // make sure we're on a tag page
        if (is_archive()) {
    
            // get the current tag
            $title = "...put your tag here....".$title;
    
         }
    
        return $title;
    }

    to find the name of your tag, in the function add

    echo "<pre>";
    print_r($wp_query);
    echo "</pre>";

    it will contain the current TAG slug, which you can prepend to the title where I’ve put “…put your tag here….”

    (If you can’t figure it out, copy/paste the output of $wp_query into pastebin)

    Thread Starter skafte

    (@skafte)

    Thanks David,

    I really appreciate your willingness to help.

    I tried adding your codes, but I couldn’t make it work (sorry, I’m new to php!).

    Would you mind taking a quick look at my single-project.php file and see if you can make more sense of it than I can? I’ve added it to pastebin as you suggested: https://pastebin.com/4Fq19n63

    Thanks! – I’m learning ??

    Jonas

    Thread Starter skafte

    (@skafte)

    Aha! I worked it out! Great!

    Last thing; do you know if it’s possible to either make a line break between the tag and the title, or, even better, can I detach the tag from the title so that I can wrap it in it’s own div and style it?

    Thanks! This is very exciting! ??

    Jonas

    Thread Starter skafte

    (@skafte)

    Oh no! – sorry to bother you again – but it didn’t actually work as I thought it did ??

    Sorry, I thought I had it. Hope you can help.

    Thanks,

    Jonas

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Call parent-tag via PHP?’ is closed to new replies.