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'.

Tuesday, July 23, 2019

My new WordPress project

For long time (actually up to few weeks ago) all my projects were developed using Java, Grails and similar technologies. Majority of web applications had big business logic or need serious database access and display logic. The result of that was that actually I had no experience with number of existing tools online. I didn't worry about that as I thought that everything can be done fast and easy using my favorite tools, Java and Grails :)

I must admit that I was wrong. With my wife we have decided that we want to try personal website related to tattoo, tattoo images and number of things related to tattooing. If you want to have a look on the result you can check withtattoo.com.

After consideration I have decided that I will try to go with WordPress. I had no experience before that. So I just created trial account on wordpress.com and have been playing little bit. Very soon you realize architecture of WP as such and start feeling very good about working with it. Soon you need to decide the look and feel. With such amount of free or commercial themes available it is actually hard work to choose :).

When I found a theme I needed some hosting. After initial surprise of low prices very soon I have found that some serious WP hosting costs similar to Java hosting. Around $30-$50. I have decided for WP liquidweb.com hosting. There are no limitations which plugins you need to use, which themes you can install and basically you have full control. To must surprise this was not the case for some strong WP hosting companies even when you pay more. Additionally I have very good professional experience with LiquidWeb as our virtual servers provider in the company.

So basically after choosing WP theme, hosting you start working on website. Soon different ideas are coming how to accomplish different goals. To my surprise there is almost infinite number of available WP plugins to use and those plugin really speeds up your work. SEO, Social media, similar content and more. All this I know how to implement in Java but real enjoyment is that you can achieve same almost without work (again, this is case for simple website not advanced heavy back-end application).

So just my conclusion. If you are thinking about some project, even if you are good software developer, before you start implementing it consider some existing frameworks and solutions. If you choose well and understand architecture behind it you can really do nice things.

So basically, my first WP project is about tattoos and can be found on address withtattoo.com.