Get post format - php

I needed to use the category name as a class for the H1, and also needed to exclude category 2. So I botched together the PHP code below. It works fine except the post looses it's formatting. I know that get_post_format() is what applies the format, but when I add it, it doesn't seem to do anything. Any help would be appreciated, thanks.
<?php while ( have_posts() ) : the_post();
$excludedcats = array(2);
$count = 0;
$categories = get_the_category();
foreach($categories as $category)
{
$count++;
if ( !in_array($category->cat_ID, $excludedcats) )
{
echo '<h1 class="category-heading ' . sprintf( __( "heading-%s" ), $category->slug ) . '" >';
if( $count != count($categories) )
{
echo " ";
}
}
}
single_post_title();
echo '</h1>';
the_content();
?>

get_post_format() doesn't apply the format; it just returns the post type, which you can then use to apply the format. In order to have different formats for different post types, you can do something like this:
<?php
get_template_part( 'content', get_post_format() );
?>
And then you need another file, content-[post-type-slug].php (e.g. for image posts, the file would be called content-image.php) where you display the post. You could also just use a switch statement on the return value of get_post_format() but that can get messy.

Related

Insert content after first post in WordPress loop

I want to display a list of categories after the first post in the Loop of index.php (this is the template my WP theme uses to display posts).
I've searched around on the web and found some code (see below) which is supposed to do as I want - inject a list of category titles as links between a list of posts in the Loop.
However, it is not working as expected. It only shows one category title, not all of them. Interestingly, it displays the title of the first post's category (the post that comes before the custom code), but no others.
My Loop code, including the custom code I inserted, is as follows:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part('content'); ?>
// START CUSTOM CODE
<div>
<?php
if( $wp_query->current_post == 0 ) {
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
}
?>
</div>
// END CUSTOM CODE
<?php endwhile; ?>
Hoping someone can help.
Thanks,
Mekong
It is a little unclear to me from your question, but it seems like you want a list of all categories, correct? I think the line "$categories = get_the_category();" is getting categories for the current (in this case first) post only.
If you want a list of all categories that exist in your blog/website try 'get_categories', https://developer.wordpress.org/reference/functions/get_categories/
Try this code, small change in your code...
<?php if (have_posts()) : $i = 1; while (have_posts()) : the_post(); ?>
<?php get_template_part('content'); ?>
<div class="categories">
<?php
if( $i == 1){
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0
) );
foreach ( $categories as $category ) {
printf( '%2$s<br />',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
}
?>
</div>
<?php $i++; endwhile; ?>

How to push/create an associative array?

I am running a loop of posts and these have four properties I need to associate: title, date, link, location:
while (have_posts()) : the_post();
if( !in_array( $post->ID, $already_displayed_ids )) {
array_push( $thisPostid, $post->ID );
$title = get_the_title();
$location = usp_get_meta(false, 'usp-custom-8');
$link = get_permalink();
$contentYear = usp_get_meta(false, 'usp-custom-14');
if ($contentYear >= 0 && $contentYear <= 2019) {
array_push($yearsArray, $contentYear);
if (($wp_query->current_post +1) == ($wp_query->post_count)) {
$yearsArray = array_unique($yearsArray);
sort($yearsArray);
}
}
array_push( $already_displayed_ids, $post->ID );
}
endwhile;
Basically I am running a loop, I check for non duplicate in $already_displayed_ids and I need to associate and push the title, location, date and link of any post I am pushing here array_push($yearsArray, $contentYear);
At the moment I am only able to push $contentYear but there is not link, title or location associated to the pushed post, I thought of creating an associative array in order to push all the specs I need of each posts. $contentYear is a custom field with a date, so I am genertaing a navigation with those values, they are dates. But I need to associate to each date their title, location and link as I currently don't know how to push those specs to associated to the time I am pushing.
This is how I resolved it:
while (have_posts()) : the_post();
if( !in_array( $post->ID, $already_displayed_ids )) {
array_push( $thisPostid, $post->ID );
$contentYear = usp_get_meta(false, 'usp-custom-14');
if ($contentYear >= 0 && $contentYear <= 2019) {
array_push($yearsArray,
array(
'year'=> $contentYear,
'link'=> get_permalink(),
'location'=> usp_get_meta(false, 'usp-custom-8'),
'title'=> get_the_title()
)
);
}
array_push( $already_displayed_ids, $post->ID );
}
endwhile;
Then I can do foreach ($yearsArray as $year) { and echo $year['title'] etc
As far as im aware there is no direct functionality for getting an associative array from posts.
Here is a snippet of what your asking for:
$posts = array();
if (have_post()) :
while (have_posts()) :
array_push($posts,
Array(
'id'=>the_ID(),
'quantity'=>1,
'size'=>$size,
'colour'=>$colour
)
);
endwhile;
$_SESSION['cart'] = $posts[0]; // change 0 index
endif;
Please note the above code is not ideal, and you probably should be doing this. Also you may want to be using cookies and not sessions.

ACF Flexible Content - Not showing anything when taking the data from author page

The content doesn't want to display. I have read and went through the documentation many times. I even asked other developers to see and check the code, and it looked correct for them.
I need this to be on the Author page. I have tested this on a custom post type, a page etc... and it worked. However, this info must come from the author page.
Here are my fields:
Here is where the fields are displayed:
And below is my code. It always displays nothing. Why is that? I have version 5.6.1
```
// check if the flexible content field has rows of data
if( have_rows('social_media') ):
// loop through the rows of data
while ( have_rows('social_media') ) : the_row();
if( get_row_layout() == 'social_media_icons' ):
echo the_sub_field('media_facebook');
endif;
endwhile;
else :
echo "nothing";// no layouts found
endif;
?>
```
Try This:
$page_id = //your page id;
// check if the flexible content field has rows of data
if( have_rows('social_media',$page_id) ):
// loop through the rows of data
while ( have_rows('social_media',$page_id) ) : the_row($page_id);
if( get_row_layout() == 'social_media_icons' ):
echo the_sub_field('media_facebook');
endif;
endwhile;
else :
echo "nothing";// no layouts found
endif;
?>
Remove echo from this line the_sub_field('media_facebook');
// check if the flexible content field has rows of data
if( have_rows('social_media') ):
// loop through the rows of data
while ( have_rows('social_media') ) : the_row();
if( get_row_layout() == 'social_media_icons' ):
the_sub_field('media_facebook');
endif;
endwhile;
else :
echo 'nothing'; // no layouts found
endif;
I have figured it out.
It's an Author page. There are many Authors and each of them is unique.
If we try to pull the field, and we have say 10authors, what field will it pull? A standard loop won't work here.
We need to pass the author id. user_1.
Now, that's static, we want it dynamic, so we can get the author id, and then concatinate it to the user_$id.
Such as:
$author_id = get_the_author_meta('ID');
while ( have_posts() ) : the_post();
if( have_rows('social_media','user_'. $author_id) ):
while ( have_rows('social_media', 'user_'. $author_id) ) : the_row();
if( get_row_layout() == 'social_media_icons' ): ?>
<?php endif; ?>
<?php endwhile;
else :
echo 'no content';
endif;
endwhile; // End of the loop.
This will work, because the ID now is dynamic, and we are getting the current user ID and then concatenating it to the 'user_'.
That code there works 100% as of version 5.6.5, and should work in further as well.

Wordpress Loop - Show posts from custom taxonomy terms in heirarchical order

ok, here's what I'm trying to do:
I've got a custom post type called drinks-menu, a taxonomy called drinks-menu-categories, and a page template called template-drinks-menu.php.
The taxonomy has a bunch of terms that are heirarchical - Wine, with children White and Red; Beer, with children Pilsener, Stout, etc...
I want to use one loop to display all the posts from these terms in the same order that they're ordered by in the admin. Here's the code I've got so far:
<?php
$post_type = 'drinks-menu';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array('post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
echo '<h1>'.$term->name.'</h1>';
if ( $term->description !== '' ) {
echo '<div class="page_summary">'. $term->description .'</div>';
}
echo '<br>';
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=-1&orderby=id&order=DESC" );
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
?>
<?php the_title(); ?>
<?php
if( get_field( "price" ) ): ?>
<?php echo '<span">'; the_field( "price" ); echo ' | '; the_field( "abv" ); echo '</span>';?>
<?php endif;
echo '<em>'; the_excerpt(); echo '</em><br><br>';
endwhile; endif;
endforeach;
endforeach;
?>
This is working well to bring all the posts from the terms onto the page, but not in order. You can see I tried taxonomy=$taxonomy&term=$term-slug&posts_per_page=-1&orderby=id&order=DESC but it's not working, everything shows in alphabetical order.
Any Wordpress gurus who can point me in the right direction? I hope I've been clear about the issue. Thanks :-)
Posts ordered in admin page by "menu_order";
If u want to order posts using query_posts (WP_Query constructor) u should use value of variable "orderby" in upper case -> "ID".

Can't get the categories to show

I am working off the TwentyTwelve theme and I have modified the index file by adding this snippet before the loop
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main" class="clearfix">
<?php
$terms = get_the_category();
$count = count($terms);
echo '<ul id="post-filter">';
echo '<li>All</li>';
if ( $count > 0 ){
foreach ( $terms as $term ) {
$termname = strtolower($term->name);
$termname = str_replace(' ', '-', $termname);
echo '<li>'.$term->name.'</li>';
}
}
echo "</ul>";
?>
<div id="mwrapper">
<?php query_posts('cat=-6,-7'); ?>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="box">....
I'm trying to create a filter that will filter through the blog post. Like the demo here. Currently I have five categories: Agency Notes, Design Notes, Featured, Humor, Uncategorized. And there's at least one post per category but it seems to be pulling in Design Notes only.
I have also tried changing the get_the_category(); to wp_list_categories(); but that ended up showing all the categories.
Source I'm getting the snippet from.
get_the_category() grabs the current post's category/ies information, not the list of categories in the full WP installation.
I think what you're looking for is the get_categories() function (more info here at the codex: http://codex.wordpress.org/Function_Reference/get_categories)
<?php
$categories=get_categories( array( 'order' => 'ASC', 'orderby' => 'name' ) );
$count = count($terms);
[...]
First off, you want to get all categories. get_the_category() does not do this. You probably want get_categories() instead.
$terms = get_categories();
$count = count($terms);
echo '<ul id="post-filter">';
echo '<li>All</li>';
if ( $count > 0 ) {
foreach ( $terms as $term ) {
echo '<li>'.$term->name.'</li>';
}
}
echo "</ul>";
I also made a few modifications: removed the hash and rel attribute. We can use data-attributes instead, which are more semantic.
The next part depends on your post HTML, but I'm assuming they have a class of post and the category they're in. If they do, you can do something like this with jQuery:
$('a', '#post-filter').on('click', function(e) {
e.preventDefault();
$('.post').hide().filter('.' + $(this).attr('data-slug')).show();
});
Which will hide all posts, and only show the ones in the selected category. I'll leave it to you to sort out the animations.

Categories