Saturday, July 10, 2021

WP - POD - Custom Taxonomy or Post Type - relationship field

If you are wondering how to traverse relationship field of custom post type or custom taxonomy created using PODs here is some code which can help you.

This code is running on the archive page. Archive is custom taxonomy created with PODS. Name of the relationship field is child_category.

Note that this is not copy/paste code. Its just code which works but you need to change it according to your configuration.


$current_term = get_queried_object();

//echo $current_term->term_id;

$pod = pods( 'gif_category', $current_term->term_id );

$related = $pod->field( 'child_category' );

if( !empty($related)){

//var_dump( $related );

    foreach ( $related as $rel ) {

        //echo "<br/>aaaaaaaaaaa";

        //echo $rel['slug'];

        $slug = $rel['slug'];

        //echo $slug;

        //echo get_term_link( $slug, 'gif_category' );

        //echo "<br/>";

        echo "<a href='" . get_term_link( $slug, 'gif_category' ). "'>".$rel['name']."</a>";

        echo ", ";

        //var_dump( $rel );

        //echo get_permalink( $id );

    }


See the value of variable in WordPress - vardump

 In WP happens that you got to some object and you want to find as much as possible about that object.

So the question is: what is the content of WP object.

Trick which works very often is to use function var_dump($variable)

Friday, June 18, 2021

How to create YOAST custom variable

 If you wonder how to create custom YOAST variable here is solution:

You have to add following code into functions.php.

First create empty function which will be used to register all YOAST custom variables:

function register_custom_yoast_variables() {}

Now register that function with YOAST so it knows how to load custom variable. Add following code:

add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables');

Now create function which will return value of your custom variable. For example:

function say_hello() {
    return "hello world";
}

And we are almost ready. Into register_custom_yoast_variables() add code which will actually register function say_hello as custom variable.

Basically add following code:

wpseo_register_var_replacement( '%%say_hello%%', 'say_hello', 'advanced', 'My custom say_hello yoast variable' );

So function register_custom_yoast_variables will now look like this:

function register_custom_yoast_variables() {
    
wpseo_register_var_replacement( '%%say_hello%%', 'say_hello', 'advanced', 'My custom say_hello yoast variable' );
}

Now if you need more custom variables just create php function which will return value and register it in the function register_custom_yoast_variables

Thursday, June 17, 2021

WordPress - How to find parent term of current term

 You have hierarchical categories or custom taxonomy and for SEO reasons or navigation reasons you want to display parent term or the current term.

To get name of the parent term if such is available you can use following code:

Note you have to be on the taxonomy page (archive) for this to work.

function parent_term() { 

$term = get_queried_object();

$parent = "";

if ($term->parent == 0) {

$parent = "";

} else {

$termParent = ($term->parent == 0) ? $term : get_term($term->parent, $term->taxonomy);

$parent = $termParent->name;

}

return $parent;

 

WP - Theme Editor not visible

 If you are asking: Why theme editor is not visible in my WP? or Why I cannot edit functions.php in WordPress? or Why theme editor is missing?

The probable reason is that you have disabled editing files. 

Here are few possibilities how to get your theme editor back:

1. In wp-config.php find following line:

define (‘DISALLOW_FILE_EDIT’, true)

and change it to

define (‘DISALLOW_FILE_EDIT’, false)

2. If you have no access to wp-config.php it means some security plugin blocked this.

In the case you are using iThemes just do following:

- In iThemes Settings go to WordPress Tweaks and enable/disable File Editor - Disable File Editor

Monday, April 26, 2021

WP - Display all tags in a tag cloud

You may have lot of tags but in your WordPress not all tags are displayed in the tag cloud. To display all or more tags there is simple solution.

Go to Appearance->Theme Editor

Then in Theme Files select functions.php.

Add the following at the end:

//Register tag cloud filter callback
add_filter('widget_tag_cloud_args', 'tag_widget_limit');
 
//Limit number of tags inside widget
function tag_widget_limit($args){
 
 //Check if taxonomy option inside widget is set to tags
 if(isset($args['taxonomy']) && $args['taxonomy'] == 'post_tag'){
  $args['number'] = 500; //Limit number of tags
 }
 
 return $args;
}

Save the file.

If you want to display even more tags or less tags then change the number of tags to value which suites you.

If you want this behavior for WP categories then instead of 'post_tag' use 'category'.