php if in category not detected - php

I am using advanced custom fields with wordpress.
I am trying to make it that if a subfield has a category of 8 it displays its repeater fields. If not it doesn't show the other posts.
However this doesn't seem to work and display the content:
<?php while(has_sub_field('team_profile')): ?>
<?php $cat = 8;
if (in_category($cat)) {
?>
<li class="col-lg-3 teamProfile">
<h2><?php the_sub_field('profile_name'); ?></h2>
<p class="jobTitle"><?php the_sub_field('category'); ?></p>
</li>
<? } ?>
<?php endwhile; ?>

Related

Get most recent post on each category Magento Fishpig

I have a successful Fishpig integration to magento 2.
Now I need to get the blog page to display most recent post on each category.
Ex: If I have 5 categories (without showing uncategorized category), I need to display most recent post from each category.
How can I do this with fishpig?
I tried modifying list.phtml file,
from FishPig/WordPress/view/frontend/templates/post. So far I can only get the most recent posts from any category (if I have most recent posts in one category, I get them. not most recent post from each category).
This is the code I've got so far..
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance() ?>
<?php $posts = $objectManager->create('FishPig\WordPress\Model\ResourceModel\Post\Collection')
->addPostTypeFilter('post')
->setOrderByPostDate()
->addIsViewableFilter()
->setPageSize(5)
->load();
?>
<?php if (count($posts) > 0): ?>
<ul>
<?php foreach($posts as $post): ?>
<li>
<?php echo $this->escapeHtml($post->getPostTitle()) ?>
<?php if ($image = $post->getFeaturedImage()): ?>
<a href="<?php echo $post->getUrl() ?>">
<img src="<?php echo $image->getAvailableImage() ?>" src="<?php echo $this->escapeHtml($post->getPostTitle()) ?>" />
</a>
<?php endif; ?>
<p><?php echo $post->getPostExcerpt(40) ?></p></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
How should I modify this to achieve what I need? Please help
What is it that you want? The module, by default, will show you each category page and list all posts in that category in descending date order. Are you trying to replicate this?
If so, take a look at the Term model. You can load a collection of categories and then call getPostCollection() on it to get posts from that Term/category.
<?php $terms = $objectManager->get('FishPig\WordPress\Model\ResourceModel\Term\CollectionFactory')->create() ?>
<?php $terms->addTaxonomyFilter('category')->load() ?>
<?php if (count($terms) > 0): ?>
<ul>
<?php foreach($terms as $term): ?>
<?php if ((int)$term->getId() === 1): ?><?php /* This is the uncategorized category, so skip */ continue; ?><?php endif; ?>
<?php $posts = $term->getPostCollection()->setPageSize(5)->setOrderByPostDate()->addIsViewableFilter()->load() ?>
<?php if (count($posts) > 0): ?>
<li>
<h2><?php echo $term->getName() ?></h2>
<ul>
<?php foreach($posts as $post): ?>
<li>
<?php /* Standard post code here */
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Blog post layouts wordpress

I'm trying to create a different layout on my blog posts page depending on when the post is created. so blog post 1 will have one class while post two will have another and post 3 will have a final class. hard to explain but here is my code hopefully it will make more sense:
<?php
if(have_posts()):
while(have_posts()): the_post();
$counter = 1;
?>
<?php if($counter == 1){ ?>
<div class="new-row">
<div class="boxes picture-box contact-smaller-box">
<?php the_post_thumbnail(); ?>
</div><!--/.picture box-->
<div class="boxes white-box contact-smaller-box">
<div class="box-inner right-side">
<h2><?php the_title(); ?></h2>
<p><?php echo substr(get_the_excerpt(), 0,70); ?></p>
Read More
</div><!--/.box inner-->
<div class="arrow arrow-left"><img src="..."></div>
</div><!--/.white box-->
</div><!--/.new row-->
<?php $counter = $counter++; ?>
<?php } if($counter == 2){?>
<!-- second section of the blog post content -->
<div class="new-row">
<div class="boxes pink-box contact-smaller-box">
<div class="box-inner">
<h2><?php the_title(); ?></h2>
<p><?php echo substr(get_the_excerpt(), 0,70); ?></p>
Read More
</div><!--/.box inner-->
<div class="arrow arrow-right"><img src="..."></div>
</div><!--/.pink box-->
<div class="boxes picture-box contact-smaller-box">
<img src="..." alt="hearing aids for adults">
</div><!--/.picture box-->
</div><!--/.new row-->
<?php } ?>
<?php
endwhile;
endif;
?>
At the moment both of my test posts are showing as the first type of post, so im thinking my counter is not working correctly, or i have my divs in the wrong position to make this work. im new at working with php so i dont know where i am going wrong. any advice would be brilliant.
After about 4 hours of research and testing different things out i have found a solution (credit):
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
Add your Custom Post Divs Here for the 1st post.
<?php elseif ($count == 2) : ?>
Add your Custom Post Divs Here for the 2nd post.
<?php elseif ($count == 3) : ?>
Add your Custom Post Divs Here for the 3rd post.
<?php elseif ($count == 4) : ?>
Add your Custom Post Divs Here for the 4th post.
<?php else : ?>
Add your Custom Post Divs Here for the rest of the posts.
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
hope it helps someone else out.

Wordpress - going from showing all categories to showing specific

Currently, I have a page that shows posts from all categories (3 categories). Now, I'm trying to make a similar page but I don't want to show one of those categories.
I know that I need it to go from Show all to show categories with id 10,11,15.
Here's my current working php script that shows all.
<div class="clearFloat">
<div id="container">
<div id="content" role="main">
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) :
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
?>
<div class='category-container clearFloat'>
<h2><?php echo $cat->name; ?></h2>
<?php
// create a custom wordpress query
query_posts("cat=$cat_id&posts_per_page=100");
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<div class="course">
<a href="<?php echo get_the_permalink(); ?>">
<h3 class="course-title"><?php the_title(); ?></h3>
</a>
<div class="promo"></div>
<div class="course-content">
<?php the_excerpt(); ?>
</div>
<div class="fadeUp"></div>
<a class="more-link" href="<?php echo get_the_permalink(); ?>">Find out more</a>
</div>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
</div>
<?php endforeach; wp_reset_query();// done the foreach statement ?>
</div><!-- #content -->
</div><!-- #container -->
use include argument in your get categories function
$args = array('include'=>'10,11,15');
$cats = get_categories($args);
Please replace your code:
$cats = get_categories();
with
$cats = get_categories('exclude=10'); //you can replace 10 with id of category you want to exclude
It will solve your problem.

Wordpress: Echo PHP depending on page ID

Good afternoon, I'm trying to display posts using PHP in the template, but I'm using the same template to output different posts depending on the age ID.
I currently have this code which works...
<?php //GET MEMBERS ?>
<?php query_posts('category_name=members&orderby=date'); ?>
<div class="row-fluid">
<ul class="thumbnails">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="span4">
<div class="thumbnail">
<?php // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
?>
<div class="pad">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
But I need to output a different "category" depending on page ID... E.G
if page id is 7 echo "php script"
else page id is 13 echo "different php script"
Thanks, Brad
Wordpress has some built in functions. You can use get_the_ID() to return the ID number to use in your if statements.
Regards

Wordpress category page loop not displaying image and title

I am a long time listener first time caller.
I am working on a wordpress theme and I am having trouble with one of the loops on my category page. I have 2 loops on the category page, the first loop which Im having trouble with, is suppose to show the most recent post from whatever category the user is viewing while the 2 loop displays a grid of the rest of the posts. Both loops work except the first one is not pulling the title or featured images for the post. Here is the code-
<?php global $post; $cat_posts = get_posts('numberposts=1&category='.$catID);
foreach($cat_posts as $post) : ?>
<div class="featuredimagec"> <?php global $post; if(has_post_thumbnail()) {
the_post_thumbnail(); }?>
<p> <?php the_post_thumbnail('large'); ?></p> </div><div class="featuredcontenttex">
<?php $postTitle = get_the_title(); if($title != $postTitle) :?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?>
</a></h1><?php the_excerpt(); ?>
</div>
</div>
<?php endif ;?>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
Any help would be appreciated. Thanks.
Change:
foreach($cat_posts as $post) : ?>
To:
foreach($cat_posts as $post) : setup_postdata($post); ?>

Categories