I'm having a normal wordpress loop, but I want to always hide the most recent post from a specific category from this loop. Any ideas?
<?php
$categories = array();
if ( have_posts() ) {
while ( have_posts() ) {
$category = the_category();
if (in_array($category)) {
the_post(); // code after the 1st one is skipped.
} else {
$categories[] = $category; // when it's the first post, add the category so the next posts will display.
}
} // end while
} // end if
?>
This should be the basic structure of your loop.
Logic: If the category is the first, it doesn't exist in $categories, so it adds it and moved on the the next post, if that category is already in the array it displays the code.
Use a boolean variable to check whether you've already skipped one post in that category, like this:
<?php
$post_has_been_skipped = FALSE;
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
if (!$post_has_been_skipped AND in_category("your-category-slug")) {
$post_has_been_skipped = TRUE;
continue;
}
//
// Post Content here
//
} // end while
} // end if
?>
Related
I'm using ACF to build blocks.
Here's a simple function I've written via functions.php:
<?php
function my_test_function() {
if ( is_page_template( 'template-parts/blocks/hero/hero.php' ) ) {
echo '<p>I appear via the hero.php template.</p>';
}
if ( is_page_template( 'template-parts/blocks/video/video.php' ) ) {
echo '<p>I appear via the video.php template.</p>';
}
}
?>
Here's how I call the function via hero.php and video.php
<?php
my_test_function();
?>
When I test this, nothing appears. I'm guessing the is_page_template function doesn't work in this instance as it's technically not a page template.
Is there another function that might work with ACF block templates?
You could use parse_block() : https://developer.wordpress.org/reference/functions/parse_blocks/ then loop through blocks to check if blockName exist for this page :
function check_if_block_exist($block_handle) {
$post = get_post();
if(has_blocks($post->post_content)) {
$blocks = parse_blocks($post->post_content);
foreach( $blocks as $block ) {
if($block['blockName'] === $block_handle) {
return true;
}
}
return false;
}
}
How can I check if a post is in a specific year? I need to output some code if the single post is in a specific year.
Inside of the wp loop you can get the date of the post with the built in wp functino of the_date('Y')
So in the wp loop just check to see if the date == something like so.
<?php
if ( have_posts() ) {
while ( have_posts() ) {
if(the_date('Y') == '2015'):
the_post();
else:
continue;
endif;
} // end while
} // end if
?>
recently I made a question about content filtering which went ok.
Now I'm trying to achieve the same filtering about title, but when it comes to title it doesnt filter only the_title(); but all titles within wp_nav are being filtered.
The aim is to achieve filtering single post title not the ones within the loop (I know about in_the_loop() )
<?php
class Filter_Title {
public function __construct() {
if( !is_front_page() && !is_home() && !is_single() ) return;
if( !is_singular( array('post','page') ) ) return;
add_filter( 'the_title', array(&$this, 'manage_page_title') );
}
public function manage_page_title($title) {
$title = '';
return $title;
}
}
$filtertitle = new Filter_Title();
?>
This is my mini plugin class.
I just tested this and you can match the post_ID with a param to check if its a post.
Since post ids don't match menu ids this should work. Give it a try.
function remove_post_title($title, $id) {
if (is_single() && in_the_loop() && $id == get_the_ID()) {
$title = '';
}
return $title;
}
add_filter('the_title', 'remove_post_title', 10, 2);
Note: in a plugin you may need to hook the add_filter in another function and run it in wp_head like before.
I'm trying to get a list with all registered sidebars using $wp_registered_sidebars but it returns an empty array.
Here is my code:
foreach ($GLOBALS['wp_registered_sidebars'] as $sidebar)
{
$sidebar_options[$sidebar['id']] = $sidebar['name'];
}
It returns Array()
In a page template it works, but not in my functions.php
Any idea?
Thanks
put this function in your theme functions.php
function get_my_widgets()
{
foreach ($GLOBALS['wp_registered_sidebars'] as $sidebar)
{
$sidebar_options[$sidebar['id']] = $sidebar['name'];
}
}
add_action('init','get_my_widgets');
and call this function as usual as get_my_widgets(); to get the registered sidebar list
add the following function in your theme functions.php
<?php
function my_sidebar_selectbox( $name = '', $current_value = false ) {
global $wp_registered_sidebars;
if ( empty( $wp_registered_sidebars ) )
return;
foreach ( $wp_registered_sidebars as $sidebar ) :
echo $sidebar['name'];
endforeach;
}
?>
and call it as my_sidebar_selectbox(); it is working to me
I just updated a menu on a site of mine to utilize wp_nav_menu. Setting this up with WP was fairly straight forward however I've run into one small snag with the the way wordpress is outputting its parent/ancestor classes for use in highlighting the current page that the content belongs to, particularly with single post pages...
Highlighting the current page with .current_page_item a and .current_page_parent a works perfect as long as its just on a normal page with children, however as soon as you visit a post from events or media, the blog link in the menu is highlighted instead which is incorrect obviously.
*One thing noticeably wrong when looking at Wordpress' output is that the current page classes are not even being generated on the correct li tag that the post belongs to which seems to be the root of the problem.
For future reference, the Events, Media, & Blog pages all use a special query I've written to only grab the respective category for that page, ie.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("category_name=media&paged=$paged");
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post">
</div>
<?php
endwhile;
else:
endif;
Hope thats enough info, if not let me know.
Best,
SB
EDIT - August 3, 2011
Below is a screen shot of what Im referring to when I say that wp_nav_menu is generating the current classes on the wrong li tag. Highlighted in Blue is the menu item that the post actually belongs to. Hightlighted in Grey is the incorrect li tag that wordpress has decided to add the current classes to instead.
http://img688.imageshack.us/img688/4180/picture2zo.png
EDIT - August 4, 2011
Maybe this will help demonstrate how I have the menu setup thus far a little better w/ Hadvig's assistance?
In my functions.php template I have -
<?php
// Add Custom Menu Support
if ( function_exists( 'register_nav_menu' ) ) {
register_nav_menu( 'epr_menu', 'EPR Main Menu' );
}
function my_menu_items_hook($items, $menu, $args) {
if ( 'epr_menu' == $menu->slug ) { // check if it is process your top menu
if ( is_single() ) { // check if single post loaded
if ( in_category('events') || in_category('media') ) {
foreach ( $items as $key => $value ) {
if ( 'blog' == $value->ID ) {
$items[$key]->classes[] = array(); //unset classes for blog item
}
// add class if post from event category
if ( in_category('events') && 'events' == $value->ID ) {
$items[$key]->classes[] = 'current-menu-item';
}
// add class if post from media category
if ( in_category('media') && 'media' == $value->ID ) {
$items[$key]->classes[] = 'current-menu-item';
}
}
}
}
}
return $items;
}
add_action('wp_get_nav_menu_items', 'my_menu_items_hook', 10, 3);
?>
In my header.php template I'm calling the menu like so -
<div id="nav_wrapper">
<ul id="nav">
<?php wp_nav_menu( array( 'container' => '', 'items_wrap' => '%3$s' ) ); ?>
</ul>
</div>
I suppose problem because you set post page as Blog and wordpress set it as parent(in your menu) for all post. You can try to change this behaviour with wp_get_nav_menu_items hook. Example:
function my_menu_items_hook($items, $menu, $args) {
if ( 'my-menu-slug' == $menu->slug ) { // check if it is process your top menu
if ( is_single() ) { // check if single post loaded
if ( in_category(EVENT_CATEGORY_ID) || in_category(MEDIA_CATEGORY_ID) ) {
foreach ( $items as $key => $value ) {
if ( BLOG_PAGE_ID == $value->object_id ) {
$items[$key]->classes[] = array(); //unset classes for blog item
}
// add class if post from event category
if ( in_category(EVENT_CATEGORY_ID) && EVENT_PAGE_ID == $value->object_id ) {
$items[$key]->classes[] = 'current-menu-item';
}
// add class if post from media category
if ( in_category(MEDIA_CATEGORY_ID) && MEDIA_PAGE_ID == $value->object_id ) {
$items[$key]->classes[] = 'current-menu-item';
}
}
}
}
}
return $items;
}
add_action('wp_get_nav_menu_items', 'my_menu_items_hook', 10, 3);
You should replace EVENT_CATEGORY_ID and MEDIA_CATEGORY_ID with your category ids(or names). Also replace EVENT_PAGE_ID and MEDIA_PAGE_ID with your page ids. Replace 'my-menu-slug' with your menu slug.
Of course this would work only if you attach post to only one category from Events, Media or Blog.
Updated.