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";
}
Related
On my search results page, I have the standard loop that outputs pages and posts from the search query.
However, I want to create a group or section that gets all of the search results that return is_page_template('template-procedure-minimal') as TRUE first (preferably with a heading), and then output the rest of the list as usual.
How can this be done?
<?php get_search_form(); ?>
<hr class="dotted">
<div id="search-results" class="marginbottom2">
<?php if(have_posts()) {
// Results found
global $wp_query;
echo '<p>'.$wp_query->found_posts.' result'.(($wp_query->found_posts > 1) ? 's' : null).' found.</p>';
while(have_posts()) {
the_post();
echo '<li>'.get_the_title().'</li>';
}
get_template_part('nav', 'below');
} else {
// No results
echo '<p>Sorry, your search returned nothing. Did you spell it correctly?</p>';
} ?>
</div>
<p class="lead halfmargin">Can't find what you're looking for?</p>
<p>Contact Us</p>
You can use get_pate_template_slug() function to determine which template is using.
Here is an example (I haven't tested this code):
`
$page_template_posts = array();
// Results found
global $wp_query;
echo '<p>'.$wp_query->found_posts.' result'.(($wp_query->found_posts > 1) ? 's' : null).' found.</p>';
while(have_posts()) {
the_post();
$post_template = get_page_template_slug(get_the_ID());
array_push($page_template_posts[$post_template], get_post(get_the_ID()));
}
foreach ($page_template_posts as $key => $page_template_post){
echo '<div>'. $key .'</div>';
foreach ($page_template_post as $post_obj) {
echo '<li>'.get_the_title($post_obj->ID).'</li>';
}
}
get_template_part('nav', 'below');
} else {
// No results
echo '<p>Sorry, your search returned nothing. Did you spell it correctly?</p>';
} ?>
`
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
}
?>
is it possible to have different headers on each page?
I'm currently using this code which works perfectly
<?php if(is_front_page()):?>
<?php echo do_shortcode('[myiamge1]'); ?>
<?php endif;?>
Now I tried to use this code and it doesnt work
<?php if(is_front_page()):?>
<?php echo do_shortcode('[myiamge1]'); ?>
else { ?> (is_page('Contact')){
echo '<img src="image5.jpg" />';
}
<?php endif;?>
Any ideas?
I know nothing about Wordpress - never used it but the above doesn't look right. Is there a reason whya more traditional style syntax is not adopted - more akin to this:
<?php
if( is_front_page() ){
echo do_shortcode('[myiamge1]');
} elseif( is_page('Contact') ){
echo '<img src="image5.jpg" />';
} else {
/*banana for scale - do something else*/
}
?>
I'd like to give my title <?php echo get_the_title(); ?> conditional statement. If not HOMEPAGE just display whatever it is = <?php echo get_the_title(); ?>. if HOMEPAGE display THIS IS + <?php echo get_the_title(); ?>
so the syntax should be
<?php
$path = $_SERVER['REQUEST_URI'];
$page = basename($path);
$page = basename($path, '.php');
?>
<?php if($page == 'index')
{echo 'This is'. get_the_title(); }
else
{echo get_the_title();}
?>
the problem is I dont really know in wordpress do we use the same way or something smarter or easier.
OH, FORGOT!
Actually, my index/homepage is not real index, it is the page called "HOMEPAGE "
setting from Reading Settings -> a static page, front page => HOMEPAGE
Thanks for any advice.
place your code in functions.php
function title_edit($title){
if(!is_front_page() || get_post_type() != 'page') return $title;
$title = 'Home: '.$title;
return $title;
}
add_filter('the_title','title_edit');
Problem solved thanks #silentboy
<?php if(is_front_page())
echo 'This is'.get_the_title();
else echo get_the_title(); ?>
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