使用unctions.php在主页上点击2个帖子的标题

时间:2021-02-24 作者:TralaT

我有下面的代码,它的工作,但我需要添加到这2个可点击的标题,从类别nr 1的最新职位

如何制作?

add_action(\'generate_after_header\', \'work_only_on_front_page\', 999);
   function work_only_on_front_page(){
    if ( is_front_page() ) {

       echo \'
   
<div class="slogan"> 
slogan here
</div> 

I WANT HERE 2 CLICKABLE POST TITLES FROM CAT.1

\';  
   }
}

1 个回复
SO网友:TralaT

here is answer :)

add_action(\'generate_after_header\', \'work_only_on_front_page\', 999);
   function work_only_on_front_page(){
    if ( is_front_page() ) {
        
 echo \'
 
 any text with divs\';

  
        
           $args = array(
       \'posts_per_page\' => 2, // we need only the latest post, so get that post only
       \'cat\' => \'1\' // Use the category id, can also replace with category_name which uses category slug
    );
        
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ) {
        printf( \'<a href="%1$s" class="dwanaglownej">%2$s<br /><i>czytaj ></i></a>\',
            esc_url( get_permalink( $recent[\'ID\'] ) ),
            apply_filters( \'the_title\', $recent[\'post_title\'], $recent[\'ID\'] )
        );
    }
        
 
               
 
  echo \'
 
 any text with divs
\';
   }
}

相关推荐

Functions.php上未定义$_GET和&_REQUEST索引

我最近尝试学习为我的自定义主题创建一个主题选项页面,这是按照stackoverflow和其他资源的教程进行的。但脚本显示错误if ($_GET[\'page\'] == basename(__FILE__)) { if (\'save\' == $_REQUEST[\'formaction\']) { foreach ($options as $value) { if( isset( $_REQUEST[ $value[\'id\']