Writing divs with PHP if/else statements - php

This is coming from a wordpress site I'm working on, but isn't a WP-specific question.
I have an if/else PHP statement that checks whether the user is looking at my site's home page or not. If they are looking at the home page, I want the code to do nothing. If they AREN'T, I want it to display the page title in a config.
The code I currently have is:
<div class="page-header">
<h1>
<?php
if ( is_front_page()){
echo ' ';
}
elseif (is_home()) {
if (get_option('page_for_posts', true)) {
echo get_the_title(get_option('page_for_posts', true));
} else {
_e('Latest Posts', 'roots');
}
} elseif (is_archive()) {
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
if ($term) {
echo $term->name;
} elseif (is_post_type_archive()) {
echo get_queried_object()->labels->name;
} elseif (is_day()) {
printf(__('Daily Archives: %s', 'roots'), get_the_date());
} elseif (is_month()) {
printf(__('Monthly Archives: %s', 'roots'), get_the_date('F Y'));
} elseif (is_year()) {
printf(__('Yearly Archives: %s', 'roots'), get_the_date('Y'));
} elseif (is_author()) {
global $post;
$author_id = $post->post_author;
printf(__('Author Archives: %s', 'roots'), get_the_author_meta('display_name', $author_id));
} else {
single_cat_title();
}
} elseif (is_search()) {
printf(__('Search Results for %s', 'roots'), get_search_query());
} elseif (is_404()) {
_e('File Not Found', 'roots');
} else {
the_title();
}
?>
</h1>
</div>
I know the echo ' '; is probably way off, but I'm an absolute php beginner! At present this creates an empty <div> and <h4> tag, but I'd rather clean it up and create nothing at all on the home page.
How can I best modify the code above to achieve this?

You need to move your code around. If you don't want the leading <div><h1> be printed on the front page, then move the if check before that. Add an else where you then print the <div><h1>, the big if/else code blob, and the closing </div>, etc.
Also read up on switching in and out of PHPs code and html mode.
Albeit, it's probably best explained by just showing:
<?php
if ( is_front_page()){
echo ' ';
}
else {
?>
<div class="page-header">
<h1>
<?php
if (is_home()) {
if (get_option('page_for_posts', true)) {
echo get_the_title(get_option('page_for_posts', true));
/*
...
*/
} else {
the_title();
}
?>
</h1>
</div>
<?php
}
?>
The only interesting thing here is that you enclose the html+code blob between the elses opening { and closing } curly brace.

Put your echos into a variable instead of printing them, then after your if statements do
echo ($var =='') ? '' : ''.$var.'';
Html tags between the last 2 sets of single quotes

Related

Display different content depending on post author

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
}
?>

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";
}

Wordpress if 404 title

I have a wordpress plugin that has the following code inside header.php:
<?php
if (the_subtitle("","", false) == "") {
the_title();
} elseif(is_404()) {
echo "404";
} else {
the_subtitle();
}
?>
Basically what should happen is:
If subtitle present, echo subtitle.
If no subtitle present, echo title.
If 404, echo "404".
But for some reason, when I locate my 404.php page, their is nothing displayed?
If your first check is for subtitle and your 404-page has no subtitle then that part of the if-statement is triggered and all other checks are skipped. By performing the 404 check first things should work as expected.
<?php
if (is_404()) {
echo "404";
} elseif (the_subtitle("","", false) == "") {
the_title();
} else {
the_subtitle();
}
?>

If subtitle present issue

I'm using the newest wordpress.
In my theme, I'm using the following code:
<?php
if (the_subtitle() == "") {
echo the_title();
} else {
echo the_subtitle();
} ?>
Each page has a default title. Some pages have a subtitle. If there IS a subtitle, then the title shouldn't be displayed and the subtitle should take it's place.
But right now for some reason, it's displaying the subtitle and THEN the title?
You don't need to call echo. It already echoes.
<?php
if (the_subtitle("","", false) == "") {
the_title();
} else {
the_subtitle();
} ?>

Categories