If subtitle present issue - php

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();
} ?>

Related

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

How to add php string that get category title in wordpress

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!

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();
}
?>

How can I use PHP to place specific HTML on Certain Pages of my Template?

There are two ways of writing php snippets to place html on the home page and then html on all other pages... This is for Joomla 3.++
<?php
$menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
echo 'HTML for HOME PAGE HERE';
} else {
echo 'HTML for ALL OTHER PAGES HERE';
} ?>
This code allows for just focusing on the home page only:
<?php
$menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
echo 'HTML FOR HOME PAGE HERE';
}
?>
What I would like to do, specifically like the second code snippet, is focus on specific pages on the site....
The reason being is A. I don't want to create a template for every other page type I am creating... And B. I don't want to have to place template elements of styling in my html module positions. I feel the custom HTML or blog / article posts should simply be for content insertion.
So is there a way to instead of calling on the $menu->getDefault()) <<<< make that getPage???
Use JInput to get the page information from the request.
Found the answer. You can do a direct page edit and ifelse edits for multiple pages.
<?php
//This is the code for the page with menu ID 102
$menuID = JSite::getMenu()->getActive()->id ;
if ($menuID == '102')
{
echo '<strong>long</strong>';
}
?>
Here is the code for multiple pages.
<?php
//This is the code for the page with menu ID 6
$menuID = JSite::getMenu()->getActive()->id ;
if ($menuID == '6')
{
echo '';
}
elseif ($menuID == '2') //This is the HTML for page with menu ID 2
{
echo '';
}
elseif ($menuID == '4') //THis is the html for page with menu id 4
{
echo '';
}
else //This is the HTML code for the rest of the pages
{
echo '';
}
?>
Found the answer here.
Joomla if else for menu by ID or url

WP Custom Fields, hide code if field is empty?

I'm using a custom field template plugin for WP. I want to hide
<li>Sqft: [squareft]</li>
if the field is empty. I've tried different codes, these are two I've tried based on suggestions:
<?php if ('squareft' !== '') { ?><li>Sqft: [squareft]</li>
<?php } ?>
And
<?PHP $squareft = ('squareft'); if ($squareft != '') { echo '<li>Sqft: [squareft]
</li>';} if (empty($squareft)) { echo " "; } ?>
I obviously don't have a clue what I'm doing, although I'm learning through trial and error. It uses shortcodes, so [squareft] is what to use to output the field data.
Any help is appreciated.
Update:
I think I've got it working, based on doing this method. Not yet gone live but it's working in my test post.
<?php
global $post;
$bathrooms = get_post_meta($post->ID, 'bathrooms', true);
if ( !empty($bathrooms) ) { echo '<li>Baths: [bathrooms]</li> | ' ; }
?>
Try this (replacement of the if statement in your first code sample):
<?php if (do_shortcode('[squareft]') != '') { ?>
<li>Sqft: [squareft]</li>
<?php } ?>
Or if you know the custom meta field's actual field name (generated by the plugin, probably some kind of prefix + squareft) you can do:
<?php if (get_post_meta($post->ID, 'squareft', true) != '') { ?>
It would help to know the specific plugin you are using.
Using the get custom fields plugin I have used the following to hide content if the field it empty (in this case the 'info' field):
<?php $info = c2c_get_custom('Info');?>
<?php if ( $info == "" ) {echo "";} else {echo "<h2 id=comments>Notes</h2><div id=info>$info</div>" ;} ?>
Not sure if this will help but it might give someone else more ideas.
Are tring this :
<?php if ( (c2c_get_custom('image')) ) { ?>
blah blah blah php code etc...
<?php } ?>
Works perfectly. If field has something it shows, if not no show, it was leaving a broken image when empty, now its good.
<?php if (get_post_meta($post->ID, 'squareft', true) != '') { echo "display output";?>

Categories