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();
}
?>
Related
hi everybody just found this code
<?PHP
if (isset($_GET['id'])) {
if (!empty($_GET['id']) && $_GET['id'] != "index") {
if (file_exists($_GET['id'].".php")) {
include ("./".$_GET['id'].".php");
} else {
echo "Not Found section";
}
} else {
include ("start.php");
}
} else {
include ("start.php");
} ?>
Gallery
i want use it , but i have my files in another folder: "FILESPHP", how i can make link to FILESPHP folder? .. thankyou.
Consider using google before.
with getcwd() you get your current directory
you can navigate from there as in every linux
Kind of:
<?PHP
if (isset($_GET['id'])) {
if (!empty($_GET['id']) && $_GET['id'] != "index") {
if (file_exists($_GET['id'].".php")) {
include ("./FILESPHP/".$_GET['id'].".php");
} else {
echo "Not Found section";
}
} else {
include ("FILESPHP/start.php");
}
} else {
include ("FILESPHP/start.php");
} ?>
thank you inetphantom,
when refresh page.. FILESPHP/start.php load very GOOD..
but.. when i call files in FILESPHP.. not works..
the example is: Gallery show "Not Found section"
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";
}
I just wanted to know how to apply php string when you want to get the title:
ex.
<h1 class="page-title"><?php single_cat_title(__('category:','themename')); ?></h1>
so if the title is "Video's", the button in the content will change to "Watch now" instead of "Read more" which is the default.
something like
if title is = "video's" {
echo "<button>Watch now</button>"
}<button>Readmore</button>
You need false as second parameter to just return the value (not print). And then just compare it.
if (single_cat_title("", false) == "video's") {
echo "Watch now";
} else {
echo "Read more";
}
Simple PHP if/else
Here's the code which will work for you
if (single_cat_title("",false) == "video's") {
echo "<button>Watch now</button>"
} else {
echo "<button>Readmore</button>"
}
I am just assuming that title is actually titled video
Here is basic approach you can check video post by it's category id even with the category name. here is the example code.
$category_id = get_cat_ID('Category Name');
if( $category_id == 'your cat id'){
echo 'watch video';
{
else{
echo 'readmore';
}
Hope it helps. cheers!
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();
} ?>
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