How to declar a variable using isset() function on Wordpress - wordpress

I have a block of php codes I put in functions.php on Wordpress website, part of which is as shown below:
add_filter('request', 'my_change_term_request', 1, 1 );
function my_change_term_request($query){
$tax_name = 'ait-items'; // specify you taxonomy name here, it can be also 'category' or 'post_tag'
// Request for child terms differs, we should make an additional check
if( isset( $query['attachment'] ) && $query['attachment'] ) :
$include_children = true;
$name = $query['attachment'];
else:
$include_children = false;
if(isset($query['name'])){ $name = $query['name']; }
endif;
$term = get_term_by('slug', $name, $tax_name); // get the current term to make sure it exists
if (isset($name) && $term && !is_wp_error($term)): // check it here
if( $include_children ) {
unset($query['attachment']);
$parent = $term->parent;
while( $parent ) {
$parent_term = get_term( $parent, $tax_name);
$name = $parent_term->slug . '/' . $name;
$parent = $parent_term->parent;
}
} else {
unset($query['name']);
}
switch( $tax_name ):
case 'category':{
$query['category_name'] = $name; // for categories
break;
}
case 'post_tag':{
$query['tag'] = $name; // for post tags
break;
}
default:{
$query[$tax_name] = $name; // for another taxonomies
break;
}
endswitch;
endif;
return $query;
}
This line of codes generates error warning or notice on Wordpress:
$term = get_term_by('slug', $name, $tax_name);
like:
Notice: Undefined variable: name in /wp-content/themes/businessfinder2-child/functions.php on line 595
I have tried to replace the above line with:
$term = get_term_by('slug', if(isset($name)) { echo '$name'; }, $tax_name);
OR:
$term = get_term_by('slug', if(isset($query['name'])) { echo "$name"; }, $tax_name);
None of the above replacements work.
Very appreciate any advice.

$query is empty in nonsigular pages and it just works in singular page like post and page. Add this line in top of Function to prevent the function call in home page and etc.
if(empty($query)) return $query;

I fixed the issue. In case anyone has experience with similar issue, just declare $name as global variable before the error line like this:
global $name;
$term = get_term_by('slug', $name, $tax_name);
or at the beginning of the function.
thanks.

Related

i have to add prefix in some pages of my website and it show the conent of my existing page (mean without prefix page) in wordpress

$items = array( 'page' );
foreach( $items as $item )
{
add_filter( $item . '_link', 'my_link', 99, 2 );
}
function my_link($permalink, $post )
{
$pagearr = array('daily-dairy', 'free-downloads','latest-essay-topics','practice- tests','practice-exercises','video-lectures','contacts','sample1','sample3');
$arr = explode('/',$permalink);
if(in_array($arr[sizeof($arr)-2],$pagearr)){
$permalink = str_replace( get_site_url(), get_site_url() . '/institute', $permalink );
return $permalink;
}else{
return $permalink;
}
}
This code adds a prefix in my page URL but it shows page not found when add the prefix. I have to show the content without the prefix page.
you haven't mentioned here that which hook you are using for these functions, you have written in your code. Look at the code below and tell me if you find it working on your end.
function change_blog_links($post_link, $id=0){
$post = get_post($id);
if( is_object($post) && $post->post_type == 'post' || $post->post_type == 'page'){
return home_url('/YOUR_PREFIX/'. $post->post_name.'/');
}
return $post_link;
}
add_filter('post_link', 'change_blog_links', 1, 3);

How-to get a menu label via $post-> or $page->ID

Entirely Revised Please Reread
Hello,
The theme I am using displays the page's title as opposed to it's menu label in the breadcrumbs. I am trying to get the breadcrumbs to instead display the associated menu label if it is available and if not then default to the page_title.
I have come up with some code that I think is close. Line 4/// $menu_items = wp_get_nav_menu_items( $slug ); returns null and it should return the nav item that contains $slug of the current post. Obviously, there is something I do not understand.
What I am attempting to do is get the slug of the current post, then using the slug get the nav item post. Then extract the title of the nav item and use that in place of the page title in the breadcrumbs. If the page was not in the nav system then it should default to the page title, as might be the case for a ppc campaign landing page.
if ( is_page() && !$post->post_parent ) {
$title = null;
$slug = mpactMEDIA_get_the_slug( get_the_ID() );
$menu_items = wp_get_nav_menu_items( $slug );
//var_dump((array)$menu_items);
foreach ( (array)$menu_items as $key => $menu_item ) {
$title = $menu_item->post_title;
}
if ( $title ) { echo $delimiter . ' ' . $before . $title . $after; }
else { echo $delimiter . ' ' . $before . get_the_title() . $after; }
}
I'm my functions.php file I have the following function
function mpactMEDIA_get_the_slug( $id=null ){
if( empty($id) ) global $post;
if( empty($post) ) return '';
$id = $post->ID;
endif;
$slug = basename( get_permalink($id) );
return $slug;
}
Thank you in advance,
Tim
I read the question a few times, I got here searching for an answer, ended up making my own.
function get_menu_label_by_post_id($post_id, $menu) {
$menu_title = '';
$nav = wp_get_nav_menu_items($menu);
foreach ( $nav as $item ) {
if ( $post_id == $item->object_id ) {
$menu_title = $item->post_title;
break;
}
}
return ($menu_title !== '') ? $menu_title : get_the_title($post_id);
}
Example usage:
echo get_menu_label_by_post_id($post->ID, 'Primary Nav');
This will return what the menu label is if it finds it, otherwise just the title of the post ID.
Check the documentation for wp_get_nav_menu_items. It doesn't take a page slug as a parameter at all.
If you want to list child pages of a given page, use wp_list_pages and pass a child_of parameter to it.
Also, as a side note, if you know the $post and want the slug, it's just $post->post_name

List WordPress Terms Not Linked

In a WordPress theme, how do you list all terms associated with a post, and of a certain taxonomy, and only show terms that are children of set term. And set a custom separator?
Put this in your functions.php:
/*
* Get Terms as a list
*/
function get_term_list_not_linked($postID, $tax, $parentTermID, $sep) {
$terms = get_the_terms( $postID, $taxonomy);
if ( $terms && ! is_wp_error( $terms ) ) {
$termList = array();
foreach ( $terms as $term ) {
if($term->parent == $parentTermID) {
$term_list[] = $term->name;
}
}
$termList = join( $separator, $termList);
return $termList;
} else {
return null;
}
}
And then in the theme file, you call it like this:
<?php echo get_term_list_not_linked($postID, $taxonomy, $parentTermID, $separator); ?>
Here is an example that gets all terms in the 'video-type' taxonomy, that are direct-children of the term with ID 5, and separate the terms with a comma and space:
<?php echo get_term_list_not_linked($post->ID, 'video-type', 5, ', '); ?>

How to Check Which level category it is for wordpress?

Let me tell you the scenario first say the structure of the categories in wordpress is like this
Level 1: Top
Level 2: -Nextme_1
Level 3: --Nextme_2
--Nextme_3
Level 4: ---Nextme_4
---Nextme_5
Now I require to check what is the level of the category? Say I catch a category of level 3 so I have to use different template and if its level 4. Then I need to use another template?
Anybody can give me some hint?
Thanks
Rahul
If you don't have many categories you can try to edit their slug from admin, and then in your page you get the category slug this way:
if (is_category()) {
$cat = get_query_var('cat');
$category = get_category($cat);
echo 'your slug is '. $category->slug;
}
Now, when you're editing the categories slugs try naming them after their level: cat-lvl-1, cat-lvl-2. Then in your page you extract the number from category slug using some php string function, and then you check that number:
if ($category->slug == 1 ) {
//load the template for the category of level 1
}
if ($category->slug == 2 ) {
//load the template for the category of level 2
}
and so on.
Later edit:
Try this:
function get_level($category, $level = 0)
{
if ($category->category_parent == 0) {
return $level;
} else {
$level++;
$category = get_category($category->category_parent);
get_level($category, $level);
}
}
if (is_category()) {
$cat = get_query_var('cat');
$yourcat = get_category($cat);
echo get_level($yourcat);
}
You can call the get_ancestors() function to get an array containing the parents of the given object. Then you need to count elements in the result.
function get_the_level($id, $type = 'category') {
return count( get_ancestors($id, $type) );
}
if( is_category() ) {
$level = get_the_level( $cat );
}
elseif( is_product_category() ) {
$level = get_the_level( $wp_query->get_queried_object()->term_id, 'product_cat' );
}
Thanks a lot. This is superb with slight a change the code that you have written is fine but its not returning any value.(i,e the $level) although its calculating correct. A minor change i did and its work fine now with a slight editing of you code given below..
`
function get_level($category, $level = 0)
{
if ($category->category_parent == 0) {
return $level;
} else {
$level++;
$category = get_category($category->category_parent);
return get_level($category, $level);
}
}
if (is_category()) {
$cat = get_query_var('cat');
$yourcat = get_category($cat);
echo get_level($yourcat);
}
`
Thanks #zuzuleinen
I visited this page months back. I came back today, arrow up on the above solution then still went digging. Although it is a good solution, Wordpress often offers better or close.
get_category_parents()
This function does as Rahul has typed basically. It also calls itself which seems the most logical approach and that is why Rahul gets a point from me on this. Do not use $link, return a string of categories, explode() them then count or I suppose we could count the number of times the separator has been used and add 1.
function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
$chain = '';
$parent = get_term( $id, 'category' );
if ( is_wp_error( $parent ) )
return $parent;
if ( $nicename )
$name = $parent->slug;
else
$name = $parent->name;
if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
$visited[] = $parent->parent;
$chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
}
if ( $link )
$chain .= ''.$name.'' . $separator;
else
$chain .= $name.$separator;
return $chain;
}

Make *ALL* Wordpress Categories use their Parent Category Template

I want to change the default template hierarchy behavior, and force all subcategory level pages that don't have their own category template file to refer to their parent category template file. In my other post, Richard M. gave an excellent answer that solved the problem for an individual subcategory. Does anyone know how to abstract it?
function myTemplateSelect()
{
if (is_category()) {
if (is_category(get_cat_id('projects')) || cat_is_ancestor_of(get_cat_id('projects'), get_query_var('cat'))) {
load_template(TEMPLATEPATH . '/category-projects.php');
exit;
}
}
}
add_action('template_redirect', 'myTemplateSelect');
Thanks in advance.
/**
* Iterate up current category hierarchy until a template is found.
*
* #link http://stackoverflow.com/a/3120150/247223
*/
function so_3119961_load_cat_parent_template( $template ) {
if ( basename( $template ) === 'category.php' ) { // No custom template for this specific term, let's find it's parent
$term = get_queried_object();
while ( $term->parent ) {
$term = get_category( $term->parent );
if ( ! $term || is_wp_error( $term ) )
break; // No valid parent
if ( $_template = locate_template( "category-{$term->slug}.php" ) ) {
// Found ya! Let's override $template and get outta here
$template = $_template;
break;
}
}
}
return $template;
}
add_filter( 'category_template', 'so_3119961_load_cat_parent_template' );
This loops up the parent hierarchy until an immediate template is found.
i was wondering how to do the same thing for heirarchical taxonomies. TheDeadMedic's answer seems to work in that case too w/ a few tweaks:
function load_tax_parent_template() {
global $wp_query;
if (!$wp_query->is_tax)
return true; // saves a bit of nesting
// get current category object
$tax = $wp_query->get_queried_object();
// trace back the parent hierarchy and locate a template
while ($tax && !is_wp_error($tax)) {
$template = STYLESHEETPATH . "/taxonomy-{$tax->slug}.php";
if (file_exists($template)) {
load_template($template);
exit;
}
$tax = $tax->parent ? get_term($tax->parent, $tax->taxonomy) : false;
}
}
add_action('template_redirect', 'load_tax_parent_template');
The TEMPLATEPATH variable might not work for child themes - looks in parent theme folder.
Use STYLESHEETPATH instead. e.g.
$template = STYLESHEETPATH . "/category-{$cat->slug}.php";

Resources