Different post style - php

I want to style wordpress posts differently, so I decide it to based on post id number odd and even to have different CSS class. So I wrote this code to check is id of post odd or even in content.php:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php $num = the_ID(); ?>
<?php if($num % 2 == 0){
echo "<h1> It's even </h1>";
}else{
echo "<h1>It's odd</h1>";
}
?>
.....
</article>
Problem is that it always says "It's even", even it is odd, For example, post number is 33, and it says that is even. So, what is the problem with my approach, is there any better way to achieve what I need ?

=== 0
You're or need to compare same types, not different types.

Assuming the_ID() is from wordpress I'm hoping maybe changing the function would net you the results you want as the code above in theory works from my testing but the_ID() as commented would most likely be the issue.
Try using get_the_ID() as it returns the ID of the post you are on.
the_ID():
Note: This function displays the ID of the post, to return the ID use get_the_ID().
From this I am assuming the_ID() also produces an echo '';
https://developer.wordpress.org/reference/functions/get_the_id/
<?php $num = get_the_ID(); ?>

Related

How to only show category description and title if there's a description available?

Is it possible to only show the code below if there's a category description?
<h2>About <?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></h2>
<?php $catID = get_the_category(); echo category_description ( $catID[0] ); ?>
I have these codes on my single posts in Wordpress but sometimes, I forgot to add descriptions on a new category that I added. So when a user visits the post, they will just see the word About Category and no description at all, making it looks like an incomplete article.
I'm not a developer and I'm also not familiar with PHP. I only added that code on the single.php to show the description. But I want it not to show when there's no description available.
Hope someone can give me the exact code to make it work.
Thanks!
Good practice to assign your values into variables at the top of your script before entering into HTML whenever possible. This will help prevent you making redundant calls and to debug your code better. Once you have your assigned values, you'll want to check if the value is empty. I am assuming the code you presented will always return some kind of value for index 0.
<?php
$cat = get_the_category();
$cat_0 = $cat[0];
$cat_0_name = $cat_0->cat_name;
$cat_0_desc = category_description($cat_0);
?>
<?php if(!empty($cat_0_desc)): ?>
<h2>About <?php echo $cat_0_name; ?></h2>
<?php echo $cat_0_desc; ?>
<?php endif; ?>
I should like to point out that I am choosing to use an alternative syntax for control structures versus the traditional brace control. This will make your code more readable and easily debugged when mixed in with HTML.
If your code is still throwing you errors, I would suggest you check your error logs as something would be happining during the get_the_cateory() call or that it's not returning any values resulting in error with $cat[0].
<?php if ($cat = get_the_category() && count($cat)>0) { ?>
<!-- <?php echo print_r($cat,1); ?> -->
<h2>About <?php echo $cat[0]->cat_name;?></h2>
<?php echo category_description ($cat[0]->cat_id); ?>
<?php } ?>

Get_post_meta Wordpress

Need help with a wordpress get_meta_post.
I need to display a div only if the custom-field promo is found in the get_meta_post. If true this is suppose to echo the :
<?php get_post_meta(get_the_ID('promo', true)
<div class="packagePromoItem">Promotion</div>
?>
Assuming that's your actual code, you have several typos, or major misunderstandings about how PHP works. This should work (using alternative syntax, which I think is a little more readable for this):
<?php $promo = get_post_meta(get_the_ID(), 'promo', true); ?>
<?php if ($promo): ?>
<div class="packagePromoItem">Promotion</div>
<?php endif; ?>
I've also assigned the promo post meta to its own variable so it's easier to follow.
You're using get_the_ID wrong. Get the ID does not accept any parameters and gets the ID of the current post. If you need to check if the post has meta 'promo', then just check if get_post_meta returns null/false.
I'm not sure what you're asking through your example though. If you're trying to echo out the post meta:
<?php if (get_post_meta(get_the_ID(), 'promo', true))) { echo'<div
class="packagePromoItem">' . get_post_meta(get_the_ID(), 'promo', true) .
'</div>';}?>

link to the other website in "the_title()" of the WordPress page

So i have the single-portfolio.php which present one of my project.
This function makes proper title to my projects every time I choose one.
<h1><?php the_title(); ?></h1>
Now whatever project i choose it always transport me to the Angela...
<h1><?php the_title(); ?></h1>
What i want to do is to have proper link to the proper project in the title.
I figured sth like this but it does not work.
<?php
$f = "http://facebook.com/";
$t = "http://twitter.com/";
$l = "http://linkedin.com/";
if (the_title()=='Facebook') {
Echo "<a href=$f> Facebook</a>";
} elseif (the_title()=='Twitter') {
Echo "<a href=$t> Twitter</a>";
} else {
Echo "<a href=$l> Linkedin</a>";
}
?>
What i get on the page is 3 times written Facebook if its Facebook page or 3 times Twitter e.g:
FacebookFacebookFacebook(the only last one "Facebook" is a link)
The problem is that the_title() echos the title, rather than returning it (as documented in the Codex). That means that, for each of your if conditions, the title gets echoed out.
Try using get_the_title() instead - this returns the post title rather than echoing it.
the_title() is a function that returns the title of the current page/post in wordpress. For the FB/Twitter/LinkedIn portion of your code you should just include "Facebook" instead of using the_title()
Use a custom field in your post to store the link in the post. Call the field "url" for example:
now you can read the field in your template and use it:
<?php $url = get_post_meta( get_the_ID(), "url", true ); ?>
<h1><?php the_title(); ?></h1>

Add a unique ID to Drupal Views list groupings

I'm using Views 3 in Drupal 7 to output a list of fields and using a grouping field to generate separate lists. I need each of the groupings to have a unique ID attribute applied to the < ul > but they don't by default.
As far as I'm aware I would need to edit the views-view-list.tpl.php template but I don't know how to achieve a unique ID per iteration.
Can anyone help please?
easiest way I can think of off the top of my head...
<?php print $wrapper_prefix; ?>
<?php if (!empty($title)) : ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<ul id="<?php echo uniqid(); ?>">
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
</ul>
<?php print $wrapper_suffix; ?>
that would go in your views-view-list.tpl.php file.
For future reference:
Put a div around everyting in view-views-list.tpl.php. You can (ab-)use the $title to generate unique (but consistent) id's.
Do it like this:
<?php $id = str_replace('FOR ALL UNWANTED CHARS','',$title); ?>
<div id="<?php print strtolower($id); ?>">
You can use the $view->dom_id variable. It is a unique id for that views instance.
In your .tpl.php file:
<?php print $view->dom_id; ?>
From comments in modules\views\theme\theme.inc:
<?php
// It is true that the DIV wrapper has classes denoting the name of the view
// and its display ID, but this is not enough to unequivocally match a view
// with its HTML, because one view may appear several times on the page. So
// we set up a hash with the current time, $dom_id, to issue a "unique" identifier for
// each view. This identifier is written to both Drupal.settings and the DIV
// wrapper.
?>

How to return a single result if all posts in loop match condition

this is some basic IF THEN statement stuff but I'm a coding newbie so I'm hoping someone can throw me a bone :-).
I have several posts that have a custom date field. I want to:
1) loop through all those posts and evaluate whether the date fields from everyone of the posts are in the past. If so, I want to display the special message.
2) If just one of the posts is in the past I want to keep evaluating until the end of all posts in the query are reached.
3) If even one post with a date that is in the future is found I have some more content to display from that post instead of the special message.
Currently, the special message is only showing up when the item in the loop has no post content at all. I was also able to get it to return the message after each post was evaluated but then I got a special message for each post evaluated, and I just want to get one message even at any point any one of the posts returns true.
My current code is this:
<?php
$parent = get_cat_name($category[0]->category_parent);
$cur_cat = $cur_cat_slug;
$cur_date = current_time('timestamp',0);
echo $cur_cat_name;
?>
</div>
<div class=dates>
<?php
$categoryvariable=$category; // assign the variable as current category
$query= 'cat=' . $cur_cat_id. '&posts_per_page=100&meta_key=date_value&orderby=meta_value&order=ASC&meta_compare=>=&meta_value=$cur_date'; // concatenate the query
query_posts($query); // run the query
if ( have_posts() ) : while ( have_posts() ) : the_post();
$date_value = get_post_meta($post->ID, 'date_value', true);
if ($date_value>=$cur_date)
{
?>
<a class=dates-link href="<?php the_permalink(); ?>">
<li><?php echo date("D, n/j/Y, g:ia", get_post_meta($post->ID, 'date_value', true)); ?> - <?php $key="course_endtime"; echo get_post_meta($post->ID, $key, true); ?>
</a>
<div class=info>
<table cellpadding=0 cellspacing=3 border=0>
<tr>
<td valign=top><img src="<?php bloginfo('template_directory'); ?>/images/i.jpg"></td>
<td>Click any date for more info about a course and to register online.</td>
</tr>
</table>
</div>
<?php
}
endwhile; else:
?>
<div class=course-content>Sorry, no courses are currently scheduled. See Course Calendar for all upcoming DTT courses.</div>
<? endif;
wp_reset_query();?>
Here's my little bone:
Use a boolean flag to determine whether or not you need to display the special message, and display the message only after the loop.
Do something like this:
Set $specialmessage = true;
before the loop.
Then, inside the loop, when you find a post more recent than the date you're testing against, set it to false and stick the_content in a holder. You can use break to exit the loop if you want.Then,
if($specialmessage)
echo 'special message'
else
echo the_content_placeholder.
Not a coding expert, but my best advice.
Throwing you a bone:
Keep the php and the output-html apart. That way, you can edit the results further before you output anything at all, and it will be a lot easier to change something later.

Categories