I want to display author name for "author meta tag" , I used following code to do this, But I always get an empty string for that:
$fname = get_the_author_meta('first_name');
$lname = get_the_author_meta('last_name');
$author = trim( "$fname $lname" );
if ( $author ) { ?>
<meta name="author" content="<?php echo $author; ?>">
<?php } ?>
How can I get the current displayed page/post author name ?
Thank you
You need to check first you if you get correct values from the get_the_author_meta(). So, you will know if the problem come from the trim/condition part, or from wordpress itself.
To do this, add in your code :
echo "Here is my result : ".get_the_author_meta('first_name')." ".get_the_author_meta('last_name');
Once this test done, i'm sure you'll need to edit your question.
Take this answer as a general advice for debugging, more than a solution for your problem.
Ok, got it. The name of the author is actually displayed by php in your HTML page, but it's encapsulated in a meta tag, which is only used in the head part of your page, and don't produce any visible output for the user.
Try to use a div tag instead of the meta one, and make sure you are writing inside the body part of your page.
$fname = get_the_author_meta('first_name');
$lname = get_the_author_meta('last_name');
$author = trim( "$fname $lname" );
if ( $author ) { ?>
<div>Author: <?php echo $author; ?></div>
<?php } ?>
Related
I want to display some html/php code via shortcode. After looking it up, I ended up with this :
add_shortcode('myshortcode', 'frontend_testshortcode');
function frontend_testshortcode() {
$email = get_field("field_email");
$name = get_field("field_name");
return $email;
}
I then insert [myshortcode] and the email is displayed on frontend, no problem.
However, the code I want to insert is much more complex, with dozens of variables and a lot of html (it is already written - until now i've been displaying it via shortcode but using a specific plugin. I want to get rid of the plugin).
If i do the following :
function frontend_testshortcode() {
$email = get_field("field_email");
$name = get_field("field_name");
<h1><?php echo $name; ?></h1>
<p><?php echo $email; ?></p>
}
It won't work. I understand that a shortcode requires "return $variable;" but i can't rewrite the whole code so as it can be inserted in a variable.
What would be the correct way to proceed?
Thank you!
I am trying to show the amount of posts each users on my WP site has posted.
Currently the code I have is:
<?php
$author_id = the_author_meta('ID');
echo count_user_posts('$author_id');
?>
As you can see I am storing the author ID in $author_id and running that in the count_user_posts() echo.
When I run the to strings separately it works however when, I combine them as above it doesn't.
Any ideas?
Regards,
You should use get_the_author_meta for this kind of purposes.
$author_id = get_the_author_meta('ID');
echo count_user_posts($author_id);
the_author_meta should be used only for echoing stuff.
Try to use it like this :
<?php
$author_id = the_author_meta('ID');
echo count_user_posts($author_id); // remove quotes
?>
Hope it works for you now.
I found you need to add your post, page or your_custom_post_type for it to work successfully.
<?php echo 'Posts made: ' .(count_user_posts(get_the_author_meta('ID'),'your_custom_post_type') ); ?>
// or multiple post types
<?php echo 'Posts made: ' . count_user_posts( get_the_author_meta('ID'),['job', 'featured_job', 'free_job'] ); ?>
I'm currently using a conditional meta tag code in wordpress.
Everything is working fine, besides on certain pages.
code in header.php:
<meta name="description" content="<?php echo metadesc($post->ID); ?>" />
<?php }else{ ?>
<meta name="description" content="<?php bloginfo('description'); ?>" />
<?php } ?>
code in functions.php:
function metadesc($pid) {
$p = get_post($pid);
$description = strip_tags($p->post_content);
$description = str_replace ("\n","",$description);
$description = str_replace ("\r","",$description);
if (strlen($description) > 135) {
return htmlspecialchars(substr($description,0,135) . "...");
}else{
return htmlspecialchars($description);
}
}
This is what it shows when I go to the source and look at the meta tag description on the following pages:
home: (description of the home page that is defined in Wordpress general settings (check)
biography: first 135 characters of the page (check)
contact:
<meta name="description" content="[contact-form-7 id="25" title="Contact"]" />
As you can see, I only have a contact form on my contact page and it looks like I need to add a filter to the script so that it ignores script tags and short codes, and that it will place the homepage description instead.
How can I fix this issue?
What if you use the strip_shortcode function try this one
function metadesc($pid) {
$p = get_post($pid);
$description = strip_tags($p->post_content);
$description = str_replace ("\n","",$description);
$description = str_replace ("\r","",$description);
$description =strip_shortcodes($description );
if(empty($description )){
return please get home page content
}
else{
if (strlen($description) > 135) {
return htmlspecialchars(substr($description,0,135) . "...");
}else{
return htmlspecialchars($description);
}
}
}
strip_shortcodes
You're jumping in and out of PHP too often, which leads to coding mistakes and slow execution. Rewrite your first code:
<?php
echo '<meta name="description" content="' .
((some condition)? metadesc($post->ID): bloginfo('description')) . '" />';
?>
Now, if your raw data for the content is [contact-form-7 id="25" title="Contact"] what are you trying to turn this into? How would you like to see it reformatted? This is coming from the metadesc() function? I don't think that HTML entities in a description tag are going to be expanded to their characters, but will be used as-is. So, you might need to output [contact-form-7 id=\"25\" title=\"Contact\"] instead. Regardless, that's a very poor description -- what do you really want there?
Also be aware of whether you're in UTF-8 or a single byte encoding such as Latin-1, which becomes important when using substr() (you don't want to chop in the middle of a multibyte UTF-8 character). Also, if you're adding an ellipsis (...), you would want to take 132 characters and not 135.
I did something similar to this, conditionally taking the excerpt and using it as the description if the viewer is on a single post page & there is an excerpt. Here's the code:
<?php
if (is_single() && $post->post_excerpt != “”) {
$post = $wp_query->post;
$descrip = strip_tags($post->post_excerpt);
echo ‘<meta name=”description” content=”‘.$descrip.’”>’;
}
?>
I also wrote a blog post detailing the whole thing.
I have seen the following thread but it's a bit beyond me...
How can I change the <title> tag dynamically in php based on the URL values
Basically, I have a page index.php (no php in it just named to future proof - maybe now!). It contains numerous lightbox style galleries which can be triggered from an external link by a variable in the URL - e.g. index.php?open=true2, index.php?open=true3, etc.
I would like the index.php title tag - to include existing static data + append additional words based on the URL variable - e.g. if URL open=true2 add "car gallery", if URL open=true3 add "cat gallery", if URL has no variable append nothing to title.
Can anyone assist? I have been searching but either missed the point of posts or it hasn't been covered (to my amateaur level).
Many thanks. Paul.
At the top of your php script put this:
<?php
# define your titles
$titles = array('true2' => 'Car Gallery', 'true3' => 'Cat Gallery');
# if the 'open' var is set then get the appropriate title from the $titles array
# otherwise set to empty string.
$title = (isset($_GET['open']) ? ' - '.$titles[$_GET['open']] : '');
?>
And then use this to include your custom title:
<title>Pauls Great Site<?php echo htmlentities($title); ?></title>
<title>Your Static Stuff <?php echo $your_dyamic_stuff;?></title>
<?php
if( array_key_exists('open', $_GET) ){
$title = $_GET['open'];
}else{
$title = '';
}
?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
The content of the document......
</body>
</html>
http://www.w3schools.com/TAGS/tag_title.asp
http://php.net/manual/en/reserved.variables.get.php
PHP can fetch information from the URL querystring (www.yoursite.com?page=1&cat=dog etc). You need to fetch that information, make sure it's not malicious, and then you could insert it into the title. Here's a simple example - for your application, make sure you sanitise the data and check it isn't malicious:
<?php
$open = "";
// check querystring exists
if (isset($_GET['open'])) {
// if it does, assign it to variable
$open = $_GET['open'];
}
?>
<html><head><title>This is the title: <?php $open ?></title></head>
PHP has lots of functions for escaping data that might contain nasty stuff - if you look up htmlspecialchars and htmlentities you should find information that will help.
Some of the other answers are open to abuse try this instead:
<?php
if(array_key_exists('open', $_GET)){
$title = $_GET['open'];
} else {
$title = '';
}
$title = strip_tags($title);
?>
<html>
<head>
<title><?php echo htmlentities($title); ?></title>
</head>
<body>
<p>The content of the document......</p>
</body>
</html>
Otherwise as #Ben has mentioned. Define you titles in your PHP first to prevent people from being able to directly inject text into your HTML.
FIXED!
I have 2 php pages that both define the variable "title" as something different. However, echoing the variable on both pages results in the first page's variable's value being displayed on both pages. Any idea why and how I could get the variable to change for each page?
first php page:
<?php
$title = "Posts";
echo $title;
?>
This displays "Posts".
second php page:
<?php
$title = "New Posts";
echo $title;
?>
This also for some reason displays "Posts". Shouldn't this page display "New Posts"?
If you are including the second page on the first page before you define $title on the first page, then the included value will be overwritten.
Are all of your variables defined in the global namespace? If so, this problem will be inevitable when you're including PHP files within other PHP files.
You could resolve the problem by properly encapsulating your variables within a class or namespace; for example:
In file one:
<?php
namespace included;
$title = "original title!";
?>
And in file two:
<?php
namespace including;
require_once "file_one.php";
$title = "new title!";
echo \included\$title;
echo \including\$title;
echo $title;
?>
Which will display:
original title!
new title!
new title!