get_home_url() just returns current URL - wordpress

My problem is simple, I need a "home" link for my WP template, and the function get_home_url() is simply returning the current url. So if I'm on "example.com/?cat=2", that's the url I get for my home button .
Any ideas what I'm doing wrong? I'm pulling my hair out.

It's just home_url(). So:
echo home_url();
Will output the homepage of your Wordpress installation.

Have you tried:
echo get_bloginfo('url')

You need to echo that function, so it should be:
<?php echo get_home_url(); ?>

Related

Adding shortcode to Wordpress Template File

I am attempting to add the following shortcode to a template file at my site using do shortcode and I can't seem to get it working correctly for some reason. Any idea how this needs to be formatted to working correctly?
[nws_alerts zip='<? echo $decoded2['ob']['zip']; ?>' state="" display="basic"]
You can't have PHP inside the shortcode like that. It would need to be called like this:
<?php
echo do_shortcode( '[nws_alerts zip=' . $decoded2["ob"]["zip"] . ' state="" display="basic"]' );
?>

How to get Home Page title in Wordpress

I want to show home page title in breadcrumb. I have tried the get_the_title() function, but it requires page id as parameter. I believe it will break when I change the front page to other page.
Is there a function that is more change prone?
this is how I solved my problem
$home_title = get_the_title( get_option('page_on_front') );
that way, when I change home page with diferent page, the code won't break. It also works on multisite.
Please try this..
<?php echo bloginfo('title'); ?>
<?php echo bloginfo('title'); ?>
This will echo you title of your website given in settings.

How to print current_user name and Logout button in wordpress

I want to display this
Current user name - Logout( redirect to home page )
This is my current code:
echo $current_user->display_name . "\n";
echo Logout
But when i click on a link, i get only "" in address bar. The function is not being recognized.
Thanks
Try
echo 'Logout';
you are mixing php code and echo
This will give you the current username of logged in admin.
global $current_user;
echo $current_user->user_login;
Function Refrence
You need to include the global variable and initialize it for it to be usable.
Above where you need to call it, add this
global $current_user;
get_currentuserinfo();
echo $current_user->display_name
It will call in the variable and allow access to its data and functions.
Read more about getting use information HERE
For the URL, try this:
echo '<a href="'. wp_logout_url(home_url()) .'" title="Logout" />Logout</a>';

How to echo a permalink in a conditional statement

Hi I hope you can help me with some advice.
I set up a website with a blog. The excerpts are shown on category.php
If there is content the excerpt is followed by a 'read more' link to the post. If there is nothing in content. It does not show 'read more'
To show this link I've written the following piece of code.
<?php
global $post;
if ( empty( $post->post_content ) ) {
echo '';
} else {
echo '<p class="laes-mere">Læs mere... </p>';
}
?>
The 'read more' (Læs mere...) shows up if there is content. This works exactly as I want it to. The problem is that the permalink does not work. I get this error
Not Found
The requested URL /< was not found on this server.
This is the page http://rubowarkitekter.dk/?cat=21
Any advice would be great. Thank you.
Ellen
You have opened the php tags when php tags are already opened try this
echo '<p class="laes-mere">Læs mere... </p>';
You can also get the post link by using get_permalink
echo '<p class="laes-mere">Læs mere... </p>';

Delete post from front end of wordpress

Here's the code I have:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$url = get_bloginfo('url');
if (current_user_can('edit_post', $post->ID)){
echo '<a href="';
echo wp_nonce_url("$url/wp-admin/post.php?post=$postid&action=delete", 'delete-post_' . $post->ID);
echo '">Delete your listing</a>';
}
?>
I'm trying to delete the post outside the wordpress loop.
When I click the link, I get:
"Your attempt to delete this post: “post-name” has failed.
Please try again."
Does anyone know why that would be?
Looking at the reference, it looks like current_user_can() only takes one argument.
Perhaps the root cause here is a permissions issue for the user. You should be checking if a user has the delete_posts capability as seen below. Note that delete_posts checks if the user can delete their own posts. To check if the user can delete the posts of other users, you can use delete_other_posts instead.
if (current_user_can('delete_posts')){
echo '<a href="';
echo wp_nonce_url("$url/wp-admin/post.php?post=$postid&action=delete", 'delete-post_' . $post->ID);
echo '">Delete your listing</a>';
}
(Assuming you are using WP version 2.1 or later)

Resources