Display different content depending on post author - php

I am trying to display a different image on my page depending on who the Wordpress author of the post is.
So far I have tried a few scripts but none of them work. Any help is greatly appreciated. Here is what I am trying to do.
<?php $author = get_the_author(); ?>
<?php
if ( $author('author1') ) {
echo '
<img src="">;
'
} elseif ( $author('author2') ) {
echo '
<img src="">;
'
} else {
// if neither, echo something else
}
?>

The get_the_author() function return the author's display name as a string. So you have to simply compare the result of get_the_author(). There is no array or object as return value.
So I would go with the following solution using a switch instead of if:
<?php $author = get_the_author(); ?>
<?php
switch($author) {
case 'author1':
echo '<img src="">';
break;
case 'auhtor2':
echo '<img src="">';
break;
default:
// if neither, echo something else
}
?>
In case you want to use the if statement you can use the following:
<?php $author = get_the_author(); ?>
<?php
if ($author === 'author1') {
echo '<img src="">';
} elseif ($author === 'author2') {
echo '<img src="">';
} else {
// if neither, echo something else
}
?>

Related

PHP elseif else

With the following code, I would like to plot the image sun.png when sky is clear, cloud when the sky is cloudy and variable in other cases...but something fails...I always get the image variable.png
<?php
if($sky == "clear" ) {
echo '<img src="images/sun.png" width="40">';
}
elseif ($sky == "cloudy" ){
echo '<img src="images/cloud.png" width="40">';
}
else {
echo '<img src="images/variable.png" width="40">';
}
?>
I consult the database using this code #Jack Goodman
$data_query = mysqli_query($conexionbd,'select * from `weather` where `data` = "2017-03-22" and (`num` = "1" or `num` = "2" or `num` = "3")');
while($data = mysqli_fetch_assoc($data_query)){ ?>
At the end I have solved it, my code had a mistake, the right code is
<?php
if($data['sky'] == "clear" ) {
echo '<img src="images/sun.png" width="40">';
}
elseif ($data['sky'] == "cloudy" ){
echo '<img src="images/cloud.png" width="40">';
}
else {
echo '<img src="images/variable.png" width="40">';
}
?>
Your problem is somewhere in the variable sky. I downloaded pictures and tested it with this code by changing the variable's value and it all works fine.
$sky = "cloudy";
if($sky == "clear" )
{
echo '<img src="sun.jpg" width="40">';
}
elseif ($sky == "cloudy" )
{
echo '<img src="cloud.jpg" width="40">';
}
else
{
echo '<img src="variable.png" width="40">';
}
Show me the code where you get the sky's value please.
Might be clearer using a switch statement, then you could have a catch-all for unmet conditions and use it to debug your issue:
switch($sky){
case 'clear':
echo '<img src="images/sun.png" width="40">';
break;
case 'cloudy':
echo '<img src="images/cloud.png" width="40">';
break;
default:
echo '$sky is something unknown';
var_dump($sky);
break;
}

How to extract heading tags in php

in wordpress when I use <?php the_title( '<h3>', '</h3>' ); ?> it works
but if I have a different php output like this one <?php echo $variable['custom_title_option']; ?> how can I do the same as on the_title
also if I use a function like the below example:
function change_hading_titles() {
global $variable;
$heading_tag = $variable['custom_title']; //option name
if ($heading_tag == "h1") {
print '<h1>', '</h1>';
} elseif ($heading_tag == "h2") {
print '<h2>', '</h2>';
} elseif ($heading_tag == "h3") {
print '<h3>', '</h3>';
} elseif ($heading_tag == "h4") {
print '<h4>', '</h4>';
} elseif ($heading_tag == "h5") {
print '<h5>', '</h5>';
} elseif ($heading_tag == "h6") {
print '<h6>', '</h6>';
}
}
add_action('change_hading_titles', 'change_hading_titles');
is it possible to use do_action( 'change_hading_titles' ); to change all my custom titles?
So, I mean to retrieve the function for closing <?php echo $variable['custom_title_option']; ?> with heading tags
Do you mean?
function getTag($tagName, $titleValue){
return "<".$tagName.">".$titleValue."</".$tagName.">";
}
$tag = getTag("h1", "hello");
// $tag = <h1>hello</h1>
..If that is not what your after, please explain a little more clearly, am happy to help further. Either way, this would be a significantly more efficient way of doing that function.

Adding "Media query" to If/else statements in PHP

Im using the following code to echo content in wordpress based on the size of its title.
<?php
$title = the_title('','',false);
if(strlen($title) > 35):
echo content(20);
else:
echo content(45);
endif;
?>
Is there a simple way of projecting a media query before this to echo an output based on the window width so basically for mobiles and devices
As per a reply i still cant get this to work using:
<?php
if ( wp_is_mobile() ) {
echo content (20);
} else {
$title = the_title('','',false);
if(strlen($title) > 35):
echo content(20);
else:
echo content(45);
endif;
}
?>
Even simplifying the code to following doesn't seem to work:
<?php
if ( wp_is_mobile() ) {
echo content(20);
} else {
echo content(45);
}
?>
and simply uses the "else" value: echo content(45) on mobile
WordPress doesn't have any functionalities to detect window width. PHP itself cannot do that.
The most promising solution is to use wp_is_mobile():
if ( !wp_is_mobile() ) {
echo "this";
} else {
echo "that";
}

Changing $_Post Search Results To Words Rather Than Category Number

On my custom search results page I have a line that displays what category and price. The code I'm using is
Displaying results for <?php echo $_POST['category_name']; ?>
The output for this code is "Displaying results for 18120".
I'm looking to display the word "Audio" rather than "18120", as well as about 10 other categories.
My current attempt, shown below, is not working properly.
<?php $cat = $_POST['category_name']; ?>
<?php if ($cat == 18120) echo 'Audio';
elseif ($cat == 18121) echo 'Television';
?>
In the else/if, store the new name, rather than echoing it. Then use that variable in your display:
<?php $cat = $_POST['category_name']; ?>
<?php if ($cat == 18120) $cat = 'Audio';
elseif ($cat == 18121) $cat = 'Television';
?>
Displaying results for <?php echo $cat; ?>
To save aload of else and if why not use the good old switch and case?
switch ($_POST['Category_name']){
case 18120:
$Cat = "Audio";
break;
case 18121:
$Cat = "Television";
break;
case 18122:
$Cat = "Something Else";
break;
}
echo $Cat;
Use get_category() wordpress function:
<?php
$catCode = $_POST['category_name'];
if(is_numeric($catCode)) {
$cat = get_category($catCode);
echo $cat->name;
} else {
echo $catCode;
}
?>
Enjoy your code!

Different page titles with one header file?

How do you do multiple page titles with on header file? Theres one thing though. For the index page, i've got
error_reporting(0);
if ($_GET["error"]=="404") {
include("forum/styles/art_air/web_template/overall_header.php");
include("include/404");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
} else {
include("forum/styles/art_air/web_template/overall_header.php");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
}
So i would have the header before anything else. So how would i manage to make so that
index?error=404 and index have different titles? Thanks in advance.
In overall_header.php
<?php
$title = "Hello, wolrd!";
if ( $_GET["error"] == "404" ) {
$title = "Error";
}
?>
<title><?php echo $title; ?></title>
Use JavaScript and document.title.
Example:
<script language="javascript">document.title = "My Title"</script>
JS can be used in body.
Another method is to set a $GLOBAL variable before including everything.
Example:
error_reporting(0);
$GLOBALS['404'] = 0;
if ($_GET["error"]=="404") {
$GLOBALS['404'] = 1;
include("forum/styles/art_air/web_template/overall_header.php");
include("include/404");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
} else {
$GLOBALS['404'] = 0;
include("forum/styles/art_air/web_template/overall_header.php");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
}
In your overall_header.php:
if($GLOBALS['404'] == 1) echo '<title>404: Not Found</title>';
else echo '<title>My Title</title>';
You could try a switch
<?php
$page = $_SERVER['PHP_SELF'];
switch($page) {
case "index.php":
echo "<title>My Homepage</title>";
break;
case "apples.php":
echo "<title>The Best Apples!</title>";
break;
case "bananas.php":
echo "<title>We Sell Bananas</title>";
break;
}
?>

Categories