In Wordpress get category ID within a blog roll - wordpress

I'm not a fan of the default blog roll for a theme that I'm using, so I'm deconstructing it, so far I was able to pull into the blog roll template:
Author: get_the_author_link()
Date with formatting: the_time('F jS, Y');
What I'm having trouble with is pulling in the categor(eis) for each post. I've tried:
get_the_category(); // with or witout the get_the_ID()
But All I get back the array object. I then tried traversing to no avail. What am I doing wrong? Again, this is not an individual post, but a blog roll.

You're looking for get_the_category_list().
This function takes three parameters - separator, parents and post id (in that order).
So, you'll want to do something like this:
<?php echo get_the_category_list(', ', '', get_the_ID()); ?>
This will create a an unordered (ul) list with commas in between the category names. You will most likely need to use some CSS to make the li items float or set them to inline block. Something like this:
<ul>
<li>Cat 1, </li>
<li>Cat 2, </li>
<li>Cat 3</li>
<ul>
You can also use get_the_term_list(). This has a little more flexibility in the final output of the category terms.
get_the_term_list() accepts five parameters: post id, taxonomy, before, separator, after.
For your use you would do this:
<?php echo get_the_term_list( get_the_ID(), 'category', '<span>', ', ', '</span>' ); ?>
This will output something like this:
<span>Cat 1, Cat 2, Cat 3</span>
You can change the wrapper (<span>), I just used that as an example.
Here is documentation on both functions from the Codex:
https://codex.wordpress.org/Function_Reference/get_the_category_list
https://codex.wordpress.org/Function_Reference/get_the_term_list

Related

I can't debug whats wrong with my ACF get_field() to display an image array's url value

So my code is pretty straightforward. I am just getting a field value from the global fields (Options). So this field is on Dashboard -> Options (I have ACFpro) and I am trying to echo out a logo which is an image array.
This is how my code looks like
<ul class="text-white p-0">
<a class="brand " href="/">
<?php
$image = get_field('logo');
echo '<p id="debug">'.($image ['url']).'</p>';
?>
</a>
</ul>
al i am trying to do is pull the url value and insert it to a <p> tag just to make sure it pulls the right value. but instead I am getting this error.
error when trying to echo $image
Any idea on what am I doing wrong?
It is one of two things. You need to add the ID or options to the get_field() as a parameter or you need to adjust the return format. By default it tries to pull the ID of the page get_field() is on.
I would change it to
get_field( 'logo', 'options' );
Or change options to whatever you called your options page.
https://www.advancedcustomfields.com/add-ons/options-page/

How to wrap HTML/PHP concatenation in Wordpress Function

I'm making my first basic Wordpress Theme, and trying to create a function to retrieve and display the first 6 posts, which I could use in various areas depending on the page you're on. It is very similar to this tutorial, that I found... after facing the fact it doesn't work in my theme.
Here is a simplified piece of the code (I tried several versions) :
function show_last_items() {
while ( have_posts() && $count < 6) :
the_post();
$count++;
print '<article id="post-'.the_ID().'" class="post-link-display">
<a href="'.the_permalink().'" title="'.the_title_attribute().'">
<div class="date">
<p>'.the_time( 'd/m' ).'</p>
</div>
<div class="title">
<h6>'.the_title().'</h6>
</div>
</a>
</article>';
endwhile;
}
For some reason, while the code inside the function works just fine if I use it directly on a template part (for example sidebar.php), all the HTML seems to be wiped off when used as a function... Or more exactly, it still exists, but in-between the data. So I get something like this displayed :
55http://localhost/myTheme/posts/55/my-post-title/My post title19/03My
post title
56http://localhost/myTheme/posts/56/my-other-post-title/My other post
title19/03My other post title
Yet the <article> tag and every markup in it is still there, empty, after each line that displays the retrieved information. I'd really like to understand why the concatenation doesn't work properly in this case. Is it something about the theme support ? It doesn't seem to me the WP_Widget class would change anything to this issue, but maybe I'm wrong ?
Try this
function show_last_items() {
while ( have_posts() && $count < 6) :
the_post();
$count++;
sprintf('<article id="post-%s" class="post-link-display">
<a href="%s" title="%s">
<div class="date">
<p>%s</p>
</div>
<div class="title">
<h6>%s</h6>
</div>
</a>
</article>', the_ID(), the_permalink(), the_title_attribute(), the_time( 'd/m' ), the_title());
}
Have you tried building a string and then returning it?
$return_string = '';
// while loop
$return_string .= '<article .....';
// end while
return $return_string;

wp_title showing page title but not site title

I couldn't find an answer to this question anywhere and I have a hard time believing I'm the only one who has had this problem.
My title function inside my title tag of header.php, <?php wp_title('|', true, 'right'); ?>, is returning the page title but not the site title.
Example: About Us |
Why would it not be returning the site name??
Looks like wp_title() does not return the site's title. Sorry, hopefully someone will benefit from this answer.
I've never tried combining the entire title inside one function. Instead I use multiple calls:
<?php wp_title(''); ?><?php if(wp_title('', false)) { echo ' |'; } ?> <?php bloginfo('name'); ?>

Get, modify, then print post title in wordpress

Is there a way native way in wordpress to make uppercase the first letter of the first word of the scentence / the_title of the post?
If not, how do I do it in php, while it is wrapped in a <a></a> tag?
Here is the full line of code.
<?php ucfirst(the_title());?>
As you see, I have tried ucfirst, but it does not work. The other thing that I tried is:
<?php
$temp_title = the_title();
$final_title = ucfirst($temp_title);
?>
<?php echo $final_title;?>
ucfirst() returns a string. You need to echo the value returned from ucfirst().
Furthermore, the WordPress function the_title() prints the title directly, rather than returning it as a string. Use get_the_title() instead.
<?php echo ucfirst(get_the_title());?>
I think this is your problem: http://core.trac.wordpress.org/browser/tags/3.8/src/wp-includes/post-template.php#L51. As you can see the_title() is using get_the_title() and then if($echo) it echoes it out. I would experiment and try get_the_title().
First off you had an ending parantheses that didn't have a matching parantheses in your echo $final_title. Second you should try to apply ucfirst to the echo of your title. I removed $finaltitle, because it no longer serves a purpose. I haven't tried the code, but it should work. Please note that ucfirst() only works if the first letter is in the alphabet.
<?php
$temp_title = the_title();
?>
<?php echo ucfirst($temp_title);?>

Need assistance w/ dynamic highlighting an image-sprite navigation menu in wordpress

I have an image based menu on a wordpress site Im building that Id like to be able to dynamically highlight for each section & more importantly highlight while each visitor is viewing a child page or post within its respective page.
I've sorted out how to get each section to highlight just fine when viewing that parent page, however Ive run into some trouble with achieving the same effect when I choose to view a post or subpage.
Since Im utilizing categories to filter content to specific pages I was able to get each section to still highlight when viewing a post by using: <?php if ( is_page('page-name') || is_category('cat-name') || in_category('cat-name')) { echo ' class="xxxx_highlight" '; } ?> yet this also caused a few problems as well unfortunately....What happened was, as soon as there was content posted to both the BLOG & MEDIA section - both sections would highlight simultaneously which is obviously not what were going for here. Couldn't really figure out why that occurred however so I removed the in_category() & is_category()...
Ive been looking through the conditional tags section up and down for the last couple weeks with no avail as to what additional tags are at my disposal in order to achieve this with a sprite menu. What would be ideal rather then targeting "categories" would be if there were a tag to simply look for if a single.php page & subpage of a parent in the menu was in use...couldn't really find anything that worked however so Im hoping some php/wordpress guru can point in the right direction hopefully!
Here is my current markup:
HTML MENU
<ul id="nav">
<li>
</li>
<li>
<a href="/" id="home" <?php if ( is_front_page('events')) { echo ' class="home_highlight" '; } ?>></a><!-- HOME -->
</li>
<li>
<a href="/artists" id="artists" <?php if ( is_page('artists')) { echo ' class="artists_highlight" '; } ?>></a><!-- ARTISTS -->
</li>
<li>
<a href="/media" id="media" <?php if ( is_page('media')) { echo ' class="media_highlight" '; } ?>></a><!-- MEDIA -->
</li>
<li>
<a href="/blog" id="blog" <?php if ( is_home('blog')) { echo ' class="blog_highlight" '; } ?>></a><!-- BLOG -->
</li>
<li>
<a href="/store" id="store" <?php if ( is_page('store')) { echo ' class="store_highlight" '; } ?>></a><!-- STORE -->
</li>
<li>
<a href="/contact" id="contact" <?php if ( is_page('contact')) { echo ' class="contact_highlight" '; } ?>></a><!-- CONTACT -->
</li>
</ul>
What I suggest to you is use echo in "ul", something like
<ul id="nav" class="<?php echo $post->ID; ?>">
..... li's and a's here
So via CSS you just do
.N #home, .M #artists, .X #media { [active styling] } /* where N is home ID, M is artists ID, X is media ID and so on */
So, when you are in page that gives ul class "N" you know that #home will be active. When you are in "M" you know that artists is active.
PS: You could even use a custom field like "classname" for a better menu control..
Just in case someone else possibly runs into the same issue I had here, Ive finally found a solution on my own after several hours of sifting through the wordpress codex...
Turns out the answer to this problem is far easier then I would have imagined. All you need to do is take advantage of wordpresses own <?php body_class($class); ?> tag...Once youve coded this in, go to firebug and inspect the outputted classes that the tag generates depending on what page your at. So now at this point its very easy to use the generated classes (the page-template-xxxx class in particular) and target that class via CSS to highlight what you need in the oldschool way you would with a static site.
Hope that helps out anybody else with the same dilemma I had!

Resources