[英]Loop posts by category on a menu item from functions PHP Wordpress
首先,90%的代码工作只需要一点点推动:D
一切都在functions.php内完成
我在标题菜单中添加了一个项目,它需要动态显示特定类别中的一些帖子。
我正在使用下面的功能获取帖子,这些功能是根据我的需要从这里获取和修改的
function get_latest_post_thumbnails_urls() {
// Set an empty variable which will hold our array of URL's
$output = [];
// Set our query args
$args = [
'posts_per_page' => 3,
'fields' => 'ids', // Only get post ID's
'meta_query' => [ // Get posts which has thumbnails only
[
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
]
],
'cat' => '5'
// Any additional parameters you might need
];
$q = get_posts( $args );
// ALWAYS make sure we have posts, else return $output
if ( !$q )
return $output;
// Ok, we have posts, lets loop through them and create an array of URL's
foreach ( $q as $id )
$output[] = wp_get_attachment_url( get_post_thumbnail_id( $id ) );
// Return our array
return $output;
}
然后我使用下面的函数添加菜单项( 从这里改编 ),但变量在循环期间没有显示在正确的位置。 <li>
需要在<ul>
。
add_filter('wp_nav_menu_items', 'add_admin_link', 10, 2);
function add_admin_link($items, $args){
if( $args->theme_location == 'main_menu' ){
$items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children dropdown menu-item-1441">
<a class="sf-with-ul" title="Eventos" href="/eventos/"><span class="link-inner">EVENTOS <span class="nav-arrow top-level fa fa-angle-down"></span></span></a>
<ul class="sub-menu" style="display: none;">'.
$urls = get_latest_post_thumbnails_urls();
if ( $urls ) {
foreach ( $urls as $url ) {
// Do something with your thumbnail url
echo '<li>'. $url .'</li>';// For testing, remove this
}
}
'</ul>
</li>';
}
return $items;
}
正如您在图像中看到的那样,该函数获取所有正确的信息,但它在菜单项之前显示它。
////////////////新代码帮助//////////////////
add_filter('wp_nav_menu_items', 'add_admin_link', 10, 2);
function add_admin_link($items, $args){
if( $args->theme_location == 'main_menu' ){
//add your first few elements to the $items variable
$items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children dropdown menu-item-1441">';
$items .= '<a class="sf-with-ul" title="Eventos" href="/eventos/"><span class="link-inner">EVENTOS <span class="nav-arrow top-level fa fa-angle-down"></span></span></a>';
$items .= '<ul class="sub-menu" style="display: none;">';
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => '5'
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
$items .= '<h2>'. the_title() .'</h2>';
}
};
//close the dom elements
$items .= '</ul>';
$items .= '</li>';
}
//return the final product
您正在处理的错误正在发生,因为您在返回菜单项之前回显了$url
变量。
要解决这个问题,不要回显循环中的url,而是将它们添加到$items
变量(使用.=
就像之前所做的那样),然后像你已经做的那样返回$items
。
add_filter('wp_nav_menu_items', 'add_admin_link', 10, 2);
function add_admin_link($items, $args){
if( $args->theme_location == 'main_menu' ){
//add your first few elements to the $items variable
$items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children dropdown menu-item-1441">';
$items .= '<a class="sf-with-ul" title="Eventos" href="/eventos/"><span class="link-inner">EVENTOS <span class="nav-arrow top-level fa fa-angle-down"></span></span></a>';
$items .= '<ul class="sub-menu" style="display: none;">';
$urls = get_latest_post_thumbnails_urls();
if ( $urls ) {
//loop through your url's
foreach ( $urls as $url ) {
//add each url onto the $items variable
$items .= '<li>'. $url .'</li>';
}
}
//close the dom elements
$items .= '</ul>';
$items .= '</li>';
}
//return the final product
return $items;
}
您还可以通过在菜单过滤器中运行WP_Query,将两个函数组合成单个函数。
这样做可以更容易地在菜单项中添加其他帖子信息。
这是一个添加特色图像的示例,包含在帖子的链接中,帖子标题被添加为链接的标题属性:
add_filter('wp_nav_menu_items', 'add_admin_link', 10, 2);
function add_admin_link($items, $args){
if( $args->theme_location == 'main_menu' ){
//add your first few elements to the $items variable
$items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children dropdown menu-item-1441">';
$items .= '<a class="sf-with-ul" title="Eventos" href="/eventos/"><span class="link-inner">EVENTOS <span class="nav-arrow top-level fa fa-angle-down"></span></span></a>';
$items .= '<ul class="sub-menu" style="display: none;">';
$urls = get_latest_post_thumbnails_urls();
//build you arguments for the wp query
$args = array(
'post_type' => 'post',
'posts_per_page'=> -1,
'tax_query' => array( //only get posts in category 5
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => 5,
),
),
'meta_query' => array( //only get posts that have a thumbnail
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
),
)
);
//create the query and loop through it
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
$thumbnail_id = get_post_thumbnail_id(); //get the thumbnail id
$image_src = wp_get_attachment_image_src($thumbnail_id, 'full')[0]; //you can use either full/large/medium/thumbnail
$items .= '<li><a href="'.get_the_permalink().'" title="'.get_the_title().'"><img src="'.$image_src.'"></a></a></li>';
}
}
wp_reset_postdata();
//close the dom elements
$items .= '</ul>';
$items .= '</li>';
}
//return the final product
return $items;
}
用于循环发布字段ID的函数
function get_latest_post_thumbnails_urls()
{
// Set an empty variable which will hold our array of URL's
$output = [];
// Set our query args
$args = [
'posts_per_page' => -1,
'fields' => 'ids', // Only get post ID's
'meta_query' => [ // Get posts which has thumbnails only
[
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
]
],
'cat' => '5'
// Any additional parameters you might need
];
$q = get_posts( $args );
// ALWAYS make sure we have posts, else return $output
if ( !$q )
return $output;
// Ok, we have posts, lets loop through them and create an array of URL's
foreach ( $q as $id )
$output[] = $id;
// Return our array
return $output;
}
用于显示菜单项的功能
function add_admin_link($items, $args){
if( $args->theme_location == 'main_menu' ){
//add your first few elements to the $items variable
$items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children dropdown menu-item-1441">';
$items .= '<a class="sf-with-ul" title="Eventos" href="/eventos/"><span class="link-inner">EVENTOS <span class="nav-arrow top-level fa fa-angle-down"></span></span></a>';
$items .= '<ul class="sub-menu" style="display: none;">';
$urls = get_latest_post_thumbnails_urls();
if ( $urls ) {
//loop through your url's
foreach ( $urls as $url ) {
//add each url onto the $items variable
$items .= '<li><a href="'. get_permalink($url) .'"><span class="link-inner">'. get_the_title($url) .'</span></a></li>';
}
}
//close the dom elements
$items .= '</ul>';
$items .= '</li>';
}
//return the final product
return $items;
}
用于在菜单中定位菜单项的功能
add_filter( 'wp_nav_menu_objects', 'restructure_menu_links', 10, 2 );
function restructure_menu_links( $reorgitems, $args ) {
$new_links = array();
$label = add_admin_link($items, $args); // add your custom menu item content here
// Create a nav_menu_item object
$item = array(
'title' => $label,
'menu_item_parent' => 0,
'ID' => 'hidden',
'db_id' => '',
'url' => $link,
'classes' => array( 'menu-item' )
);
$new_links[] = (object) $item; // Add the new menu item to our array
// insert item
$location = 2; // insert at 3rd place
array_splice( $reorgitems, $location, 0, $new_links );
return $reorgitems;
}
我知道这不是完美的但我感谢@Frits的帮助我最终让它按照我的方式工作_ :)
你的好处是让它变得更好;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.