Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 9 of 9
Like Tree1Likes

Thread: Having an issue with Wordpress custom post type loop not showing taxonomies per post

  1. #1
    WDF Staff m3n0tu18's Avatar
    Join Date
    Jul 2011
    Location
    Devon, UK
    Posts
    1,461
    Member #
    28473
    Liked
    264 times

    Having an issue with Wordpress custom post type loop not showing taxonomies per post

    Hi Guys,

    So I am having an issue at the moment where I have a custom post type and custom taxonomy registered to that CPT - although it is working correctly, it is not looping the tags within the custom post loop on the archive page. Instead it creates 2 separate posts (identical posts - Linking to the same URL) with different tags. Any ideas what is wrong with my PHP code for this loop? - Please see below: - I basically need to do 2 things. Either: 1: have get_the_terms() work for a custom post type - no end of Googling has not helped or 2: have a subloop of terms inside the loop.... Any help would be awesome! Thanks guys! Marcus

    Functions.php code
    PHP Code:
    function wptp_create_post_type_recipe() {    $labels = array(    'name' => __('Recipes'),    'singular_name' => __('Recipe'),    'add_new' => __('New Recipe'),    'add_new_item' => __('Add New Recipe'),    'edit_item' => __('Edit Recipe'),    'new_item' => __('New Recipe'),    'view_item' => __('View Recipe'),    'search_items' => __('Search Recipes'),    'not_found' => __('No Recipe Found'),    'not_found_in_trash' => __('No Recipes found in Trash'),    );    $args = array(    'labels' => $labels,    'has_archive' => true,    'public' => true,    'menu_icon' => 'dashicons-groups',    'hierarchical' => false,    'supports' => array(        'title',        'editor',//            'excerpt', //            'custom-fields', //            'thumbnail',//            'page-attributes'    ),//        'taxonomies' => array( 'post_tag', 'category'),     );    register_post_type('recipes', $args);}
    add_action('init''wptp_create_post_type_recipe');
    // register a taxonomy called 'Team Member Category'function wptp_register_taxonomy_other() {    register_taxonomy('team_member_cat', 'teams', array(    'labels' => array(        'name' => 'Departments',        'singular_name' => 'Department',        'search_items' => 'Search Departments',        'all_items' => 'All Departments',        'edit_item' => 'Edit Departments',        'update_item' => 'Update Departments',        'add_new_item' => 'Add New Department',        'new_item_name' => 'New Department',        'menu_name' => 'Departments',    ),    'hierarchical' => true,    'sort' => true,    'args' => array('orderby' => 'term_order'),    'rewrite' => array('slug' => 'departments'),    'show_admin_column' => true        )    );    register_taxonomy('recipes_cat', 'recipes', array(    'labels' => array(        'name' => 'Recipes',        'singular_name' => 'Recipe',        'search_items' => 'Search Recipes',        'all_items' => 'All Recipes',        'edit_item' => 'Edit Recipe',        'update_item' => 'Update Recipe',        'add_new_item' => 'Add New Recipe',        'new_item_name' => 'New Recipe',        'menu_name' => 'Recipes',    ),    'hierarchical' => true,    'sort' => true,    'args' => array('orderby' => 'term_order'),    'rewrite' => array('slug' => 'recipes'),    'show_admin_column' => true        )    );}
    add_action('init''wptp_register_taxonomy_other');
    flush_rewrite_rules(); 
    PHP Code:
    $terms get_terms('recipes_cat', array(            'orderby' => 'date',            'order' => 'ASC',            'hide_empty' => 1,            ));            foreach ($terms as $term):            $args = array(                'post_type' => 'recipes',                'recipes_cat' => $term->slug,                'posts_per_page' => -1,                'post_status' => 'publish',                'orderby' => 'date',                'order' => 'DESC',            );            $query = new WP_Query($args);            while ($query->have_posts()) :                $query->the_post();
                    
    $url get_the_permalink();                $image get_field('background_image');                $title get_the_title();                $date get_the_date();                $news_author get_the_author();                $news_category get_the_category();                $the_title mb_strimwidth($title040'...');
                    
    $maincontent get_the_excerpt();                $content mb_strimwidth($maincontent0100'...');
                    echo 
    '  <li class="col-sm-6 col-lg-4 all ' $term->slug '" name="">                    <a href="' $url '" style="background-image:url(' $image['url'] . ');">                        <div class="filter">                        <h2>' $the_title '</h2>                        <p class="published">Published: ' $date '</p>                        <small class="author_section">Author: ' $news_author '</small>                        <p>' $content '</p>';
                    echo 
    '<small class="tags_section"><strong>Tags:</strong> ';                //                Tag code should go here however you cant use get_the_tags() as it does not seem to work with//                the custom taxonomy - Echoing out $term->name; only shows 1 name and doing a if($terms as $term) doesnt seem//                to work due to it being within a $terms loop already...HELP!!
    //                $term_link = get_the_term_list($term, 'recipes');//                if (is_wp_error($term_link))://                continue;//                //We successfully got a link. Print it out.//                echo '<li><a href="' . $term_link . '">' . $term->name . '</a></li>';//                //                endif;


                    
    echo '</small>';
                    echo 
    '                        <span class="read_more_link">Read More</span>                        </div>                    </a>                    </li>';
                endwhile;
    //            wp_reset_query();            wp_reset_postdata();

                
    endforeach; 
    If you like my comments to your thread please click the LIKE button

    Check out my portfolio: Here!
    View my company Facebook Page
    View my company Website

    <<Plrease ignoer my typo's I have isdexlyia>>

  2.  

  3. #2
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    I think I get what you're trying to do, but I can't read that code the way it's formatted.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  4. #3
    WDF Staff m3n0tu18's Avatar
    Join Date
    Jul 2011
    Location
    Devon, UK
    Posts
    1,461
    Member #
    28473
    Liked
    264 times
    Hi Ronald.

    Yea I have just noticed that too. Looks like the wysiwyg editor php tags compress things. Grrr!! Give me a few and I'll try updating it. M

  5. #4
    WDF Staff m3n0tu18's Avatar
    Join Date
    Jul 2011
    Location
    Devon, UK
    Posts
    1,461
    Member #
    28473
    Liked
    264 times
    Hi Ronald,

    I have attached a txt version of the functions.php and archive loop code. Any ideas what I have done wrong?

    Cheers

    M
    Attached Files Attached Files
    Last edited by m3n0tu18; Sep 12th, 2017 at 10:22 AM.
    If you like my comments to your thread please click the LIKE button

    Check out my portfolio: Here!
    View my company Facebook Page
    View my company Website

    <<Plrease ignoer my typo's I have isdexlyia>>

  6. #5
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Add this to the inside of the loop:
    PHP Code:
    <pre>
    <?php print_r($term); ?>
    </pre>
    That should output the term object so you can see what it looks like. I suspect you have a sub-array that needs to be looped through separately.

    Paste the output here.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  7. #6
    WDF Staff m3n0tu18's Avatar
    Join Date
    Jul 2011
    Location
    Devon, UK
    Posts
    1,461
    Member #
    28473
    Liked
    264 times
    So For the Monkfish Kebabs which has both Fish and Kebabs as tags it is just showing this:


    PHP Code:
    WP_Term Object(    [term_id] => 40    [name] => Kebabs    [slug] => kebabs    [term_group] => 0    [term_taxonomy_id] => 40    [taxonomy] => recipes_cat    [description] =>     [parent] => 0    [count] => 1    [filter] => raw    [term_order] => 0)
    Edit: Not entirely sure why it keeps inlining the code, so here is a screenshot.
    Capture.JPG

    @ Ronald, Current website I am working on can be found here: http://fmno.peachstaging.co.uk/recipes/
    Last edited by m3n0tu18; Sep 13th, 2017 at 03:19 AM.
    If you like my comments to your thread please click the LIKE button

    Check out my portfolio: Here!
    View my company Facebook Page
    View my company Website

    <<Plrease ignoer my typo's I have isdexlyia>>

  8. #7
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Are you trying to output the posts by term, or the terms for a given post?

    If you're wanting to output the terms within the post, I would think that would need to be called within the post loop.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  9. #8
    WDF Staff m3n0tu18's Avatar
    Join Date
    Jul 2011
    Location
    Devon, UK
    Posts
    1,461
    Member #
    28473
    Liked
    264 times
    Quote Originally Posted by Ronald Roe View Post
    Are you trying to output the posts by term, or the terms for a given post?

    If you're wanting to output the terms within the post, I would think that would need to be called within the post loop.
    You know. Ur right!! I couldn't see it before. Currently I am doing $terms as $term as the main loop. But I have to do loop then term loop. I shall give it a try and see what happens



    Sent from my iPhone using Tapatalk
    If you like my comments to your thread please click the LIKE button

    Check out my portfolio: Here!
    View my company Facebook Page
    View my company Website

    <<Plrease ignoer my typo's I have isdexlyia>>

  10. #9
    WDF Staff m3n0tu18's Avatar
    Join Date
    Jul 2011
    Location
    Devon, UK
    Posts
    1,461
    Member #
    28473
    Liked
    264 times
    Hi Guys,

    Thanks Ron! With your help I have now been able to loop the terms inside the post!!!

    Mental Note!! Don't loop the terms outside of the post, it wont work, and will just output a single post term. You need to have the WP loop running first then put the terms loop inside... Everything works correctly now Cheers!
    If you like my comments to your thread please click the LIKE button

    Check out my portfolio: Here!
    View my company Facebook Page
    View my company Website

    <<Plrease ignoer my typo's I have isdexlyia>>


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:01 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com