You need to check if get_the_terms()
is actually returning anything. If a post doesn’t have terms in that taxonomy, then $taxonomy_terms
won’t actually have any terms in it. If $taxonomy_terms
is empty then $taxonomy_terms[0]
won’t actually be a term object so trying to access a property on it (->term_id
) will throw an error:
$taxonomy_exists = taxonomy_exists($custom_taxonomy);
if(empty($last_category) && !empty($custom_taxonomy) && $taxonomy_exists) {
$taxonomy_terms = get_the_terms( $post, $custom_taxonomy );
if ( ! empty( $taxonomy_terms ) ) {
$cat_id = $taxonomy_terms[0]->term_id; // ERROR LINE
$cat_nicename = $taxonomy_terms[0]->slug; // ERROR LINE
$cat_link = get_term_link($taxonomy_terms[0]->term_id, $custom_taxonomy); // ERROR LINE
$cat_name = $taxonomy_terms[0]->name;
}
}
Related Posts:
- How exactly do automatic updates work?
- How to create custom 401, 403 and 500 error pages?
- Why have on every line
- add_action in namespace not working
- Is it possible to disable a function of a parent theme?
- Variables declared in header not available in other includes
- WordPress Ajax always returns a 404 error
- Displaying a variable stored in functions.php inside widget
- Custom Meta Field – Youtube embed
- Failed to open stream / no such file or directory
- Need to get specific data from array
- Use different javascript files for each page on website
- How to get all existing post types
- WooCommerce – Fixed quantity of a product [closed]
- Settings API – sanitize_callback is not called and it leads to an incorrect behavior
- Setting up 2 SMTP accounts: 1 for wordpress and 1 for woocommerce
- Function set default image when image not present
- How to get the POST TITLE using the POST ID?
- Importing hard coded custom field into acf field
- Adding country tags automatically
- Add child pages of parent to navbar PHP
- From where the header-text can be changed in WordPress custom header?
- Trying to establish connection to External Database
- Load a page into a div with Ajax
- Login/logout in header
- How to put “Read more” link in Custom Excerpt inside p tag?
- $wpdb->update with multiple parameters gives error
- Improve page speed loading using CDN and async or defer attribute
- Applying A Category to Existing Posts Where Page Title Matches Regex
- Dashboard broken into list of links
- Get users that likes the post
- wordpress visual composer change grid builder post link
- PHP array to JS array to use in google map
- Displaying recent posts on static page with template-part via shortcode
- My custom get_the_excerpt() can’t get excerpt by ID
- deactivating an active plugin using if page
- Single Quotes in .php file doesn’t load website
- element not working in Safari [closed]
- What file have I to create in my custom WordPress theme to show all the post belonging to a specific category?
- Exclude specific post from displaying
- Last class on last headline?
- your php installation be missing the MySQL extension WordPress CPANEL [closed]
- {$key} or $key?
- Display the 3 latest WordPress Posts on a Static Page Outside WordPress
- WordPress mod_rewrite not working on php fpm
- Shortcode to pull posts
- PHP Strict Standards: Only variables should be assigned by reference
- How can I get “Previous” and “Next” to show in the navigation besides the links?
- url_to_postid returns 0
- WordPress And High disk i/o
- How to Fix an Archive.php That Displays All Posts?
- Dynamically replicate custom metabox in custom post type
- random reason on refresh
- How to tell if a user has gone in and created a menu
- Change Query Arguments (filter) with jQuery/Ajax or PHP?
- WordPress Fresh Install Errors (Woocommerce specifically now)
- Tracking Visitor LatLng with WordPress using JS, PHP. How to put data which was extract using JS into DB
- php script in elementor
- how do i add custom widget woocommerce shop page with php
- WordPress redirect to a subpage – how to create a template for subpage?
- How can I redefine WordPress wp-content directory programmatically?
- Trim Titles Only On Some Pages
- Commenting requires wordpress login, wordpress discussion and post comments tick boxes are checked
- code that I can run, or a plug in to show what sql tables something pulls information from
- Automatic email message after manual user approval
- can I extend the WP_Query class to deal with ‘duplicate’ posts created by joining to wp_posts?
- Upgrade to PHP7.3 and Changing Apache from Prefork to Event Breaks WordPress
- Enqueue sripts and styles only if function is called
- Return author name as a tag using get_author
- How to pull date/time in french format for wordpress post?
- WooCommerce subcategories help
- What is wrong with this shortcode? to include external php file in WordPress posts
- Echo Option Value Based On WordPress User Role
- ACF + WP + Slick Slider Repeater Slide Group PHP not Displaying [closed]
- wp_query beginner
- Crop an image after upload on custom account page
- Migrating a live WordPress website to a Local Server: why some elements of the website are missing?
- Store stripe info as user_meta
- Is there a way to have chunks of a page’s content expire?
- INCOMING: Wall of code for form and $_POST, not updating custom field’s value
- WooCommerce – Print Processing orders [closed]
- Loop returns more items than exist?
- Formatting Shortcode to Display in Visual Mode
- wp_nav_menu doesn’t work in a duplicate server
- Adding theme strings to .PO file
- Rewriting to a different page based on existence of query string?
- How to get category pages to look like a certain archive page?
- Host does not allow remote connection, so how do I transfer data to my WordPress site?
- I am extending Walker_Nav_Menu: How can I get the number of children elements of the current item?
- How to call a specific value within the functions.php file?
- How to define template directory in this widget code
- WordPress plugin/code to to to next/previous post in same category
- WordPress White Screen Error
- Warning: filemtime(): stat failed for wp_
- How to transfer session after being redirected to other page
- Keep users logged in Without Remember Me
- How to use js variable to php in wordpress functions.php
- Display total count of products in orders of a specific order status
- Upload multiple files via ajax from an HTML file input
- How to pass javascript var to php var