Get Page ID or URL From a Slug in WordPress

PHP, snippet, WordpressLeave a comment

This is interesting, with years experience working with WordPress, I’ve never dealt with this kind of issue. I searched the codex, and found get_page_by_path is the ideal solution instead of directly create query to database.

[php]get_page_by_path( $page_path, $output, $post_type );[/php]

To get a page ID simply add the slug to the function
[php]
// Returns Object page ID
$page_id = grain_get_url_by_slug(‘slug’);
[/php]

I creates a function to simplify the usage

function get_url_by_slug($slug) {
    $page_url_id = get_page_by_path( $slug );
    $page_url_link = get_permalink($page_url_id);
    return $page_url_link;
}

And use it in real life

<p>
	<a href="<?php echo get_url_by_slug('services'); ?> ">
		What can we do for you?
	</a>
</p>

 

How to Give Bold on Hover without Shifting another Element

CSS, snippet8 Comments

Usually, if you have a navigation menu with “bold” on hover effect, it will shift another menus. To be clear check out the example below:

See the another elements are slightly shifting as you hover one menu. To solve the issue, we can give static width to the list, but how if we want to keep it dynamic?

Take a look at the sample below:

Yes, I utilize the text-shadow to make the font looks bold, without affecting another elements.

[css]
text-shadow: 0 0.015em #101010,0 -0.015em #101010,0.01em 0 #101010,-0.01em 0 #101010;
[/css]

Let me know if you have any thought or even another solution.

Update on safari:

(29/03/17)
Here’s the working method for Safari and Webkit browsers:

-webkit-text-fill-color: black;
-webkit-text-stroke-width: 1px;
-webkit-text-stroke-color: black;

 

How to Display and Order Users on a Page

PHP, snippet, Wordpress, , Leave a comment

If you have registered users on certain role, to list them on a single page, you can use WP_User_Query

Here’s the basic loop:
[php]
$user_query = new WP_User_Query( $args );

if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo ‘

‘ . $user->display_name . ‘

‘;
}
} else {
echo ‘No users found.’;
}
[/php]

You can list and order the result by adding array value, in below example I would like to order user by last name, the $arg array value should like:
[php]
$args = array(
‘orderby’ => ‘meta_value’,
‘order’ => ‘DESC’,
‘meta_key’ => ‘last_name’,
‘role’ => ‘subscriber’
);
[/php]

That’s it let me know if you have any questions.